Changeset View
Changeset View
Standalone View
Standalone View
lib/libthr/thread/thr_malloc.c
Show All 35 Lines | |||||
#include <sys/mman.h> | #include <sys/mman.h> | ||||
#include <rtld_malloc.h> | #include <rtld_malloc.h> | ||||
#include "thr_private.h" | #include "thr_private.h" | ||||
int npagesizes; | int npagesizes; | ||||
size_t *pagesizes; | size_t *pagesizes; | ||||
static size_t pagesizes_d[2]; | static size_t pagesizes_d[2]; | ||||
static struct umutex thr_malloc_umtx; | static struct umutex thr_malloc_umtx; | ||||
static u_int thr_malloc_umtx_level; | |||||
void | void | ||||
__thr_malloc_init(void) | __thr_malloc_init(void) | ||||
{ | { | ||||
if (npagesizes != 0) | if (npagesizes != 0) | ||||
return; | return; | ||||
npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d)); | npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d)); | ||||
if (npagesizes == -1) { | if (npagesizes == -1) { | ||||
npagesizes = 1; | npagesizes = 1; | ||||
pagesizes_d[0] = PAGE_SIZE; | pagesizes_d[0] = PAGE_SIZE; | ||||
} | } | ||||
pagesizes = pagesizes_d; | pagesizes = pagesizes_d; | ||||
_thr_umutex_init(&thr_malloc_umtx); | _thr_umutex_init(&thr_malloc_umtx); | ||||
} | } | ||||
static void | static void | ||||
thr_malloc_lock(struct pthread *curthread) | thr_malloc_lock(struct pthread *curthread) | ||||
{ | { | ||||
uint32_t curtid; | |||||
if (curthread == NULL) | if (curthread == NULL) | ||||
return; | return; | ||||
curthread->locklevel++; | curthread->locklevel++; | ||||
_thr_umutex_lock(&thr_malloc_umtx, TID(curthread)); | curtid = TID(curthread); | ||||
if ((uint32_t)thr_malloc_umtx.m_owner == curtid) | |||||
thr_malloc_umtx_level++; | |||||
else | |||||
_thr_umutex_lock(&thr_malloc_umtx, curtid); | |||||
} | } | ||||
static void | static void | ||||
thr_malloc_unlock(struct pthread *curthread) | thr_malloc_unlock(struct pthread *curthread) | ||||
{ | { | ||||
if (curthread == NULL) | if (curthread == NULL) | ||||
return; | return; | ||||
if (thr_malloc_umtx_level > 0) | |||||
thr_malloc_umtx_level--; | |||||
else | |||||
_thr_umutex_unlock(&thr_malloc_umtx, TID(curthread)); | _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread)); | ||||
curthread->locklevel--; | curthread->locklevel--; | ||||
_thr_ast(curthread); | _thr_ast(curthread); | ||||
} | } | ||||
void * | void * | ||||
__thr_calloc(size_t num, size_t size) | __thr_calloc(size_t num, size_t size) | ||||
{ | { | ||||
struct pthread *curthread; | struct pthread *curthread; | ||||
▲ Show 20 Lines • Show All 59 Lines • Show Last 20 Lines |