extern "C" int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine)(void *), void * arg) #if !defined(__SUNPRO_CC) && !defined(__APPLE__) && !defined(__FreeBSD__) throw () #endif { // Force initialization of the TLAB before our first thread is created. static volatile TheCustomHeapType * t = initializeCustomHeap(); t = t; #if defined(__linux__) || defined(__APPLE__) char fname[] = "pthread_create"; #else char fname[] = "_pthread_create"; #endif // A pointer to the library version of pthread_create. static pthread_create_function real_pthread_create = reinterpret_cast<pthread_create_function> (reinterpret_cast<intptr_t>(dlsym(RTLD_NEXT, fname))); anyThreadCreated = true; pair<threadFunctionType, void *> * args = // new (_heap.malloc(sizeof(pair<threadFunctionType, void*>))) new pair<threadFunctionType, void *> (start_routine, arg); int result = (*real_pthread_create)(thread, attr, startMeUp, args); return result; }
extern "C" int thr_create (void * stack_base, size_t stack_size, void * (*start_routine)(void *), void * arg, long flags, thread_t * new_tid) { // Force initialization of the TLAB before our first thread is created. static volatile TheCustomHeapType * t = initializeCustomHeap(); t = t; char fname[] = "_thr_create"; // Instantiate the pointer to thr_create, if it hasn't been // instantiated yet. // A pointer to the library version of thr_create. static thr_create_function real_thr_create = (thr_create_function) dlsym (RTLD_NEXT, fname); anyThreadCreated = true; typedef pair<threadFunctionType, void *> argsType; argsType * args = new (getCustomHeap()->malloc(sizeof(argsType))) argsType (start_routine, arg); int result = (*real_thr_create)(stack_base, stack_size, startMeUp, args, flags, new_tid); return result; }
TheCustomHeapType * getCustomHeap() { // The pointer to the TLAB itself. TheCustomHeapType * tlab = theTLAB; if (tlab == NULL) { tlab = theTLAB = initializeCustomHeap(); } return tlab; }
TheCustomHeapType * getCustomHeap() { TheCustomHeapType * heap; initTSD(); heap = reinterpret_cast<TheCustomHeapType *>(pthread_getspecific(theHeapKey)); if (heap == NULL) { heap = initializeCustomHeap(); } return heap; }
TheCustomHeapType * getCustomHeap() { // The pointer to the TLAB itself. auto tlab = theTLAB; if (tlab == nullptr) { tlab = initializeCustomHeap(); theTLAB = tlab; } return tlab; }
inline TheCustomHeapType * getCustomHeap (void) { TheCustomHeapType * heap; #if defined(_WIN32) heap = (TheCustomHeapType *) TlsGetValue (LocalTLABIndex); #else createKey(); heap = (TheCustomHeapType *) pthread_getspecific (theHeapKey); #endif if (heap == NULL) { heap = initializeCustomHeap(); } return heap; }
// A special routine we call on thread exit to free up some resources. static void exitRoutine() { TheCustomHeapType * heap = initializeCustomHeap(); // Relinquish the assigned heap. getMainHoardHeap()->releaseHeap(); // Clear the heap (via its destructor). heap->~TheCustomHeapType(); #if !defined(USE_THREAD_KEYWORD) // Reclaim the memory associated with the heap (thread-specific data). pthread_key_delete (theHeapKey); #endif }
static inline void * startMeUp(void * a) { initializeCustomHeap(); getMainHoardHeap()->findUnusedHeap(); auto * z = (pair<threadFunctionType, void *> *) a; auto f = z->first; auto arg = z->second; auto result = (*f)(arg); delete z; exitRoutine(); return result; }
TheCustomHeapType * getCustomHeap() { // The pointer to the TLAB itself. theTLAB = (theTLAB ? theTLAB : initializeCustomHeap()); return theTLAB; }