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 } }
/* * This is all new and partially untested code to help support _beginthread() and _threadid macro */ _WCRTLINK int *__threadid( void ) { static int BadThreadId = -1L; thread_data *tdata = NULL; if( __NXSlotID != NO_INDEX ){ int ccode = NXKeyGetValue(__NXSlotID, (void **)&tdata); if( 0 != ccode ) return( &BadThreadId ); #if defined( __RUNTIME_CHECKS__ ) && defined( _M_IX86 ) if( tdata == (thread_data *)2 ) return( &BadThreadId ); #else if( tdata == NULL ) return( &BadThreadId ); #endif return( (int *)&(tdata->thread_id) ); } return( &BadThreadId ); }
// lookup thread data thread_data *__GetThreadData( void ) { thread_data *tdata = NULL; #ifdef __OS2__ TID tid; tid = GetCurrentThreadId(); if( tid <= __MaxThreads ) { tdata = __AllocInitThreadData( tdata ); if( tdata != NULL ) { if( __initthread( tdata ) ) { __FreeInitThreadData( tdata ); tdata = NULL; } else { __ThreadData[tid].data = tdata; __ThreadData[tid].allocated_entry = tdata->__allocated; } } } else { thread_data_list *tdl; thread_data_list **pprev; _AccessTDList(); tdata = NULL; pprev = &__thread_data_list; for( tdl = *pprev; tdl != NULL ; tdl = tdl->next ) { if( tdl->tid == tid ) { tdata = tdl->data; break; } pprev = &(tdl->next); } if( tdata == NULL ) { tdata = __AllocInitThreadData( tdata ); if( tdata != NULL ) { if( !__AddThreadData( tid, tdata ) ) { __FreeInitThreadData( tdata ); tdata = NULL; } } } else if( *pprev ) { // promote to front *pprev = tdl->next; tdl->next = __thread_data_list; __thread_data_list = tdl; // check for need to resize thread data if( tdata->__resize ) { tdata = __ReallocThreadData(); } } _ReleaseTDList(); } #elif defined(__NT__) if( __NTAddThread( tdata ) ) { tdata = (thread_data *)TlsGetValue( __TlsIndex ); } #elif defined(_NETWARE_LIBC) if( __LibCAddThread( tdata ) ) { if(0 != NXKeyGetValue(__NXSlotID, (void **) &tdata)) tdata = NULL; } #elif defined(__RDOS__) if( __RdosAddThread( tdata ) ) { tdata = (thread_data *)__tls_get_value( __TlsIndex ); } #endif if( tdata == NULL ) { __fatal_runtime_error( "Thread has no thread-specific data", 1 ); } 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; }
thread_data *__MultipleThread( void ) { #if defined( __NT__ ) /* * Preserve old error code -- important because this code can get * called from _STK. */ DWORD old = GetLastError(); thread_data *tdata; tdata = (thread_data *)TlsGetValue( __TlsIndex ); if( tdata == NULL ) { tdata = __GetThreadData(); } else if( tdata->__resize ) { tdata = __ReallocThreadData(); } SetLastError(old); return( tdata ); #elif defined (_NETWARE_LIBC) /* * Preserve old error code -- important because this code can get * called from _STK. */ int old = GetLastError(); int ccode = 0; thread_data *tdata = NULL; if(0 != (ccode = NXKeyGetValue(__NXSlotID, (void **) &tdata))) tdata = NULL; if( tdata == NULL ) { tdata = __GetThreadData(); } else if( tdata->__resize ) { tdata = __ReallocThreadData(); } SetLastError(old); return( tdata ); #elif defined( __WARP__ ) // 32 bit OS/2 TID tid; thread_data *tdata = NULL; tid = GetCurrentThreadId(); if( tid <= __MaxThreads ) { tdata = __ThreadData[tid].data; } if( tdata == NULL ) { tdata = __GetThreadData(); } else if( tdata->__resize ) { tdata = __ReallocThreadData(); } return( tdata ); #elif defined( __OS2_286__ ) // 16 bit OS/2 return( __ThreadData[GetCurrentThreadId()] ); #elif defined( __QNX__ ) void *tdata; __getmagicvar( &tdata, _m_thread_data ); if( tdata == NULL ) { tdata = __QNXAddThread( tdata ); } return( tdata ); #elif defined( __LINUX__ ) // TODO: Init multiple threads for Linux! return( NULL ); #elif defined( __RDOS__ ) thread_data *tdata; tdata = (thread_data *)__tls_get_value( __TlsIndex ); if( tdata == NULL ) tdata = __GetThreadData(); return( tdata ); #elif defined( __RDOSDEV__ ) return( NULL ); #else return( __ThreadData[GetCurrentThreadId()].data ); #endif }