void __LibCRemoveThread( int close_handle ) /***************************************/ { thread_data *tdata = NULL; if( __NXSlotID != NO_INDEX ) { int ccode = NXKeyGetValue(__NXSlotID, (void **)&tdata); if(0 != ccode) return; #if defined( __RUNTIME_CHECKS__ ) && defined( _M_IX86 ) if( tdata == (thread_data *)2 ) return; #else if( tdata == NULL ) return; #endif __RemoveThreadData( tdata->thread_id ); #if defined( __RUNTIME_CHECKS__ ) && defined( _M_IX86 ) NXKeySetValue(__NXSlotID, (void *) 2); #else NXKeySetValue(__NXSlotID, NULL); #endif } }
int __LibCAddThread( thread_data *tdata ) /***************************************/ { if( __NXSlotID == NO_INDEX ) { return( FALSE ); } tdata = __AllocInitThreadData( tdata ); if( tdata == NULL ) { return( FALSE ); } if( !__AddThreadData( tdata->thread_id, tdata ) ) { lib_free( tdata ); return( FALSE ); } if(0 != NXKeySetValue(__NXSlotID, tdata )) { lib_free( tdata ); return( FALSE ); } return( TRUE ); }
// realloc thread data thread_data *__ReallocThreadData( void ) { TID tid; thread_data *tdata; _AccessTDList(); tid = GetCurrentThreadId(); #ifdef __OS2__ if( tid <= __MaxThreads ) { thread_data_vector *tdv; tdv = &(__ThreadData[tid]); if( tdv->allocated_entry ) { #if defined (_NETWARE_LIBC) /* // we don't want to lose __FirstThreadData as our global // destructors will try and access it as they are called // from a different thread. */ if(__IsFirstThreadData(tdv->data)) { tdata = lib_realloc( tdv->data, __ThreadDataSize ); __RegisterFirstThreadData(tdata); } else #endif tdata = lib_realloc( tdv->data, __ThreadDataSize ); if( tdata == NULL ) { __fatal_runtime_error( "Unable to resize thread-specific data", 1 ); } tdv->data = tdata; } else { tdata = lib_calloc( 1, __ThreadDataSize ); if( tdata == NULL ) { __fatal_runtime_error( "Unable to resize thread-specific data", 1 ); } memcpy( tdata, tdv->data, tdv->data->__data_size ); tdv->allocated_entry = 1; tdv->data = tdata; } } else #endif { thread_data_list *tdl; for( tdl = __thread_data_list ; tdl != NULL ; tdl = tdl->next ) { if( tdl->tid == tid ) { break; } } if( tdl == NULL ) { __fatal_runtime_error( "Thread has no thread-specific data", 1 ); } if( tdl->allocated_entry ) { #if defined(_NETWARE_LIBC) if(tdata = lib_malloc( __ThreadDataSize )) { memcpy(tdata, tdl->data, min(__ThreadDataSize, tdl->data->__data_size)); lib_free(tdl->data); } #else tdata = lib_realloc( tdl->data, __ThreadDataSize ); #endif if( tdata == NULL ) { __fatal_runtime_error( "Unable to resize thread-specific data", 1 ); } tdl->data = tdata; } else { tdata = lib_calloc( 1, __ThreadDataSize ); if( tdata == NULL ) { __fatal_runtime_error( "Unable to resize thread-specific data", 1 ); } memcpy( tdata, tdl->data, tdl->data->__data_size ); tdl->allocated_entry = 1; tdl->data = tdata; } } tdata->__allocated = 1; tdata->__data_size = __ThreadDataSize; tdata->__resize = 0; #if defined(__NT__) TlsSetValue( __TlsIndex, tdata ); #endif #if defined(_NETWARE_LIBC) if(0 != NXKeySetValue(__NXSlotID, tdata)) { lib_free(tdata); tdata = NULL; } #endif _ReleaseTDList(); return( tdata ); }
int GetOrSetUpData(int id, libdata_t **appData, libthreaddata_t **threadData ) { int err; libdata_t *app_data; libthreaddata_t *thread_data; NXKey_t key; NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0); err = 0; thread_data = (libthreaddata_t *) NULL; /* ** Attempt to get our data for the application calling us. This is where we ** store whatever application-specific information we need to carry in support ** of calling applications. */ app_data = (libdata_t *) get_app_data(id); if(!app_data) { /* ** This application hasn't called us before; set up application AND per-thread ** data. Of course, just in case a thread from this same application is calling ** us simultaneously, we better lock our application data-creation mutex. We ** also need to recheck for data after we acquire the lock because WE might be ** that other thread that was too late to create the data and the first thread ** in will have created it. */ NXLock(gLibLock); if(!(app_data = (libdata_t *) get_app_data(id))) { app_data = malloc(sizeof(libdata_t)); if(app_data) { memset(app_data, 0, sizeof(libdata_t)); app_data->tenbytes = malloc(10); app_data->lock = NXMutexAlloc(0, 0, &liblock); if(!app_data->tenbytes || !app_data->lock) { if(app_data->lock) NXMutexFree(app_data->lock); free(app_data); app_data = (libdata_t *) NULL; err = ENOMEM; } if(app_data) { /* ** Here we burn in the application data that we were trying to get by calling ** get_app_data(). Next time we call the first function, we'll get this data ** we're just now setting. We also go on here to establish the per-thread data ** for the calling thread, something we'll have to do on each application ** thread the first time it calls us. */ err = set_app_data(gLibId, app_data); if(err) { free(app_data); app_data = (libdata_t *) NULL; err = ENOMEM; } else { /* create key for thread-specific data... */ err = NXKeyCreate(DisposeThreadData, (void *) NULL, &key); if(err) /* (no more keys left?) */ key = -1; app_data->perthreadkey = key; } } } } NXUnlock(gLibLock); } if(app_data) { key = app_data->perthreadkey; if(key != -1 /* couldn't create a key? no thread data */ && !(err = NXKeyGetValue(key, (void **) &thread_data)) && !thread_data) { /* ** Allocate the per-thread data for the calling thread. Regardless of whether ** there was already application data or not, this may be the first call by a ** a new thread. The fact that we allocation 20 bytes on a pointer is not very ** important, this just helps to demonstrate that we can have arbitrarily ** complex per-thread data. */ thread_data = malloc(sizeof(libthreaddata_t)); if(thread_data) { thread_data->_errno = 0; thread_data->twentybytes = malloc(20); if(!thread_data->twentybytes) { free(thread_data); thread_data = (libthreaddata_t *) NULL; err = ENOMEM; } if((err = NXKeySetValue(key, thread_data))) { free(thread_data->twentybytes); free(thread_data); thread_data = (libthreaddata_t *) NULL; } } } } if(appData) *appData = app_data; if(threadData) *threadData = thread_data; return err; }
void __InitMultipleThread( void ) /*******************************/ { if( __GetThreadPtr != __MultipleThread ) { #if defined( _NETWARE_CLIB ) { /* __ThreadData[ 0 ] is used whenever GetThreadID() returns a pointer not in our __ThreadIDs list - ie. whenever it returns NULL, a pointer to a thread we didn't create, or an invalid pointer */ void *ptr; ptr = lib_calloc( 1, __ThreadDataSize ); if( ptr == NULL ) { __fatal_runtime_error( "Unable to allocate thread-specific data", 1 ); } __ThreadData[ 0 ].data = ptr; __ThreadData[ 0 ].allocated_entry = 1; __ThreadData[ 0 ].data->__allocated = 1; __ThreadData[ 0 ].data->__randnext = 1; __ThreadData[ 0 ].data->__data_size = __ThreadDataSize; if( __initthread( ptr ) ) { lib_free( ptr ); __fatal_runtime_error( "Unable to initialize thread-specific data", 1 ); } ptr = lib_calloc( 1, __ThreadDataSize ); if( ptr == NULL ) { __fatal_runtime_error( "Unable to allocate thread-specific data", 1 ); } __FirstThreadData = ptr; __FirstThreadData->__allocated = 1; __FirstThreadData->__randnext = 1; __FirstThreadData->__data_size = __ThreadDataSize; __ThreadData[ 1 ].data = __FirstThreadData; __ThreadData[ 1 ].allocated_entry = __FirstThreadData->__allocated; __ThreadIDs[ 1 ] = GetThreadID(); if( __initthread( ptr ) ) { lib_free( ptr ); __fatal_runtime_error( "Unable to initialize thread-specific data", 1 ); } } #elif defined( _NETWARE_LIBC ) InitSemaphore.semaphore = 0; /* sema4 is mutex in this case */ InitSemaphore.initialized = 1; //_ThreadExitRtn = &__ThreadExit; - might need this at some point?? // Note: __AddThreadData uses the InitSemaphore, _AccessTDList & _ReleaseTDList __FirstThreadData->thread_id = GetCurrentThreadId(); __AddThreadData( __FirstThreadData->thread_id, __FirstThreadData ); if(0 != NXKeySetValue(__NXSlotID, __FirstThreadData)) { __fatal_runtime_error( "Unable to initialize thread-specific data", 1 ); } #elif defined( __NT__ ) InitSemaphore.semaphore = __NTGetCriticalSection(); InitSemaphore.initialized = 1; _ThreadExitRtn = &__ThreadExit; // Note: __AddThreadData uses the InitSemaphore, _AccessTDList & _ReleaseTDList __AddThreadData( __FirstThreadData->thread_id, __FirstThreadData ); TlsSetValue( __TlsIndex, __FirstThreadData ); #elif defined( __QNX__ ) __qsem_init( &InitSemaphore.semaphore, 1, 1 ); InitSemaphore.initialized = 1; // first thread data already in magic memory #elif defined( __LINUX__ ) // TODO: Init semaphores for Linux #elif defined( __RDOS__ ) InitSemaphore.semaphore = RdosCreateSection(); InitSemaphore.initialized = 1; __AddThreadData( __FirstThreadData->thread_id, __FirstThreadData ); __tls_set_value( __TlsIndex, __FirstThreadData ); #elif defined( __RDOSDEV__ ) RdosInitKernelSection( &InitSemaphore.semaphore ); InitSemaphore.initialized = 1; #elif defined( __OS2__ ) DosCreateMutexSem( NULL, &InitSemaphore.semaphore, 0, FALSE ); InitSemaphore.initialized = 1; __ThreadData[1].data = __FirstThreadData; __ThreadData[1].allocated_entry = __FirstThreadData->__allocated; #else #error Multiple thread support is not defined for this platform #endif #if !defined( _M_I86 ) // Set these up after we have created the InitSemaphore #if !defined (_THIN_LIB) _AccessFileH = &__AccessFileH; _ReleaseFileH = &__ReleaseFileH; _AccessIOB = &__AccessIOB; _ReleaseIOB = &__ReleaseIOB; #endif _AccessTDList = &__AccessTDList; _ReleaseTDList = &__ReleaseTDList; __AccessSema4 = &__AccessSemaphore; __ReleaseSema4 = &__ReleaseSemaphore; __CloseSema4 = &__CloseSemaphore; #if !defined( __NETWARE__ ) _AccessNHeap = &__AccessNHeap; _AccessFHeap = &__AccessFHeap; _ReleaseNHeap = &__ReleaseNHeap; _ReleaseFHeap = &__ReleaseFHeap; #endif #if defined( __NT__ ) _AccessFList = &__AccessFList; _ReleaseFList = &__ReleaseFList; #endif #endif __GetThreadPtr = __MultipleThread; } }