BOOL fnTerminateThreadInfo(void) { int index = 0; if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreWait(g_tinfoSem); #else WaitOnLocalSemaphore(g_tinfoSem); #endif //MPK_ON for (index = 0; index < NUM_ENTRIES; index++) { if (g_ThreadInfo[index] != NULL) { #ifdef MPK_ON kSemaphoreSignal(g_tinfoSem); #else SignalLocalSemaphore(g_tinfoSem); #endif //MPK_ON return FALSE; } } #ifdef MPK_ON kSemaphoreFree(g_tinfoSem); g_tinfoSem = NULL; #else CloseLocalSemaphore(g_tinfoSem); g_tinfoSem = 0; #endif //MPK_ON } return TRUE; }
BOOL fnRemoveThreadCtx(long lTLSIndex) { ThreadContext* tip = NULL; ThreadContext* prevt = NULL; if (g_tCtxSem) { #ifdef MPK_ON kSemaphoreWait(g_tCtxSem); #else WaitOnLocalSemaphore(g_tCtxSem); #endif //MPK_ON } #ifdef MPK_ON lTLSIndex = labs(kCurrentThread()); #else lTLSIndex = GetThreadID(); #endif //MPK_ON tip = g_ThreadCtx; while(tip) { if (tip->tid == lTLSIndex) { if (prevt == NULL) g_ThreadCtx = tip->next; else prevt->next = tip->next; free(tip); tip=NULL; if (g_tCtxSem) { #ifdef MPK_ON kSemaphoreSignal(g_tCtxSem); #else SignalLocalSemaphore(g_tCtxSem); #endif //MPK_ON } return TRUE; } prevt = tip; tip = tip->next; } if (g_tCtxSem) { #ifdef MPK_ON kSemaphoreSignal(g_tCtxSem); #else SignalLocalSemaphore(g_tCtxSem); #endif //MPK_ON } return FALSE; // entry not found }
BOOL fnGetHashListAddrs(void **addrs, BOOL *dontTouchHashList) { ThreadInfo* tip; int index,tid; if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreWait(g_tinfoSem); #else WaitOnLocalSemaphore(g_tinfoSem); #endif //MPK_ON } #ifdef MPK_ON tid=index = abs(kCurrentThread()); #else tid=index = GetThreadID(); #endif //MPK_ON index = INDEXOF(index); // just take the bottom five bits // see if this is already in the table at the index'th offset // for (tip = g_ThreadInfo[index]; tip != NULL; tip = tip->next) { if (tip->tid == tid) { if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreSignal(g_tinfoSem); #else SignalLocalSemaphore(g_tinfoSem); #endif //MPK_ON } *addrs = tip->m_allocList; *dontTouchHashList = tip->m_dontTouchHashLists; return TRUE; } } if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreSignal(g_tinfoSem); #else SignalLocalSemaphore(g_tinfoSem); #endif //MPK_ON } return FALSE; }
BOOL fnRemoveThreadInfo(int tid) { ThreadInfo* tip = NULL; ThreadInfo* prevt = NULL; int index = INDEXOF(tid); // just take the bottom five bits if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreWait(g_tinfoSem); #else WaitOnLocalSemaphore(g_tinfoSem); #endif //MPK_ON } for (tip = g_ThreadInfo[index]; tip != NULL; tip = tip->next) { if (tip->tid == tid) { if (prevt == NULL) g_ThreadInfo[index] = tip->next; else prevt->next = tip->next; free(tip); tip=NULL; if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreSignal(g_tinfoSem); #else SignalLocalSemaphore(g_tinfoSem); #endif //MPK_ON } return TRUE; } prevt = tip; } if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreSignal(g_tinfoSem); #else SignalLocalSemaphore(g_tinfoSem); #endif //MPK_ON } return FALSE; // entry not found }
ThreadInfo* fnAddThreadInfo(int tid) { ThreadInfo* tip = NULL; int index = 0; if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreWait(g_tinfoSem); #else WaitOnLocalSemaphore(g_tinfoSem); #endif //MPK_ON } // Add a new one to the beginning of the hash entry // tip = (ThreadInfo *) malloc(sizeof(ThreadInfo)); if (tip == NULL) { if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreSignal(g_tinfoSem); #else SignalLocalSemaphore(g_tinfoSem); #endif //MPK_ON } return NULL; } index = INDEXOF(tid); // just take the bottom five bits tip->next = g_ThreadInfo[index]; tip->tid = tid; tip->m_dontTouchHashLists = FALSE; tip->m_allocList = NULL; g_ThreadInfo [index] = tip; if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreSignal(g_tinfoSem); #else SignalLocalSemaphore(g_tinfoSem); #endif //MPK_ON } return tip; }
void* fnGetThreadCtx(long lTLSIndex) { ThreadContext* tip; if (g_tCtxSem) { #ifdef MPK_ON kSemaphoreWait(g_tCtxSem); #else WaitOnLocalSemaphore(g_tCtxSem); #endif //MPK_ON } #ifdef MPK_ON lTLSIndex = labs(kCurrentThread()); #else lTLSIndex = GetThreadID(); #endif //MPK_ON tip = g_ThreadCtx; while(tip) { if (tip->tid == lTLSIndex) { if (g_tCtxSem) { #ifdef MPK_ON kSemaphoreSignal(g_tCtxSem); #else SignalLocalSemaphore(g_tCtxSem); #endif //MPK_ON } return (tip->tInfo); } tip=tip->next; } if (g_tCtxSem) { #ifdef MPK_ON kSemaphoreSignal(g_tCtxSem); #else SignalLocalSemaphore(g_tCtxSem); #endif //MPK_ON } return NULL; }
ThreadInfo* fnGetThreadInfo(int tid) { ThreadInfo* tip; int index = INDEXOF(tid); // just take the bottom five bits if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreWait(g_tinfoSem); #else WaitOnLocalSemaphore(g_tinfoSem); #endif //MPK_ON } // see if this is already in the table at the index'th offset // for (tip = g_ThreadInfo[index]; tip != NULL; tip = tip->next) { if (tip->tid == tid) { if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreSignal(g_tinfoSem); #else SignalLocalSemaphore(g_tinfoSem); #endif //MPK_ON } return tip; } } if (g_tinfoSem) { #ifdef MPK_ON kSemaphoreSignal(g_tinfoSem); #else SignalLocalSemaphore(g_tinfoSem); #endif //MPK_ON } return NULL; }
RCODE FLMAPI f_semWait( F_SEM hSem, FLMUINT uiTimeout) { RCODE rc = NE_FLM_OK; if( uiTimeout == F_WAITFOREVER) { if( kSemaphoreWait( (SEMAPHORE)hSem) != 0) { rc = RC_SET( NE_FLM_ERROR_WAITING_ON_SEMAPHORE); } } else { if( kSemaphoreTimedWait( (SEMAPHORE)hSem, (UINT)uiTimeout) != 0) { rc = RC_SET( NE_FLM_WAIT_TIMEOUT); } } return( rc); }
extern "C" LONG f_nlmEntryPoint( struct LoadDefinitionStructure * moduleHandle, struct ScreenStruct * initScreen, char * commandLine, char * loadDirectoryPath, LONG uninitializedDataLength, LONG fileHandle, LONG (*ReadRoutine) (LONG handle, LONG offset, char * buffer, LONG length), LONG customDataOffset, LONG customDataSize) { char * pszTmp; char * pszArgStart; int iArgC; int iTotalArgChars; int iArgSize; char ** ppszArgV = NULL; char * pszArgs = NULL; char * pszDestArg; bool bFirstPass = true; char cEnd; ARG_DATA * pArgData = NULL; LONG sdRet = 0; char * pszThreadName; char * pszModuleName; int iModuleNameLen; int iThreadNameLen; int iLoadDirPathSize; void * hThread = NULL; (void)initScreen; (void)uninitializedDataLength; (void)fileHandle; (void)ReadRoutine; (void)customDataOffset; (void)customDataSize; if( f_atomicInc( &gv_NetWareStartupCount) != 1) { goto Exit; } gv_MyModuleHandle = moduleHandle; gv_bUnloadCalled = FALSE; // Allocate the needed resource tags if( (gv_lAllocRTag = AllocateResourceTag( gv_MyModuleHandle, "FLAIM Memory", AllocSignature)) == NULL) { sdRet = 1; goto Exit; } // Syncronized start if (moduleHandle->LDFlags & 4) { gv_lFlmSyncSem = kSemaphoreAlloc( (BYTE *)"FLAIM_SYNC", 0); } // Initialize NSS if( RC_BAD( f_nssInitialize())) { sdRet = 1; goto Exit; } pszModuleName = (char *)(&moduleHandle->LDFileName[ 1]); iModuleNameLen = (int)(moduleHandle->LDFileName[ 0]); // First pass: Count the arguments in the command line // and determine how big of a buffer we will need. // Second pass: Put argments into allocated buffer. Parse_Args: iTotalArgChars = 0; iArgC = 0; iLoadDirPathSize = f_strlen( (const char *)loadDirectoryPath); iArgSize = iLoadDirPathSize + iModuleNameLen; if( !bFirstPass) { ppszArgV[ iArgC] = pszDestArg; f_memcpy( pszDestArg, loadDirectoryPath, iLoadDirPathSize); f_memcpy( &pszDestArg[ iLoadDirPathSize], pszModuleName, iModuleNameLen); pszDestArg[ iArgSize] = 0; pszDestArg += (iArgSize + 1); } iArgC++; iTotalArgChars += iArgSize; pszTmp = commandLine; for (;;) { // Skip leading blanks. while( *pszTmp && *pszTmp == ' ') { pszTmp++; } if( *pszTmp == 0) { break; } if( *pszTmp == '"' || *pszTmp == '\'') { cEnd = *pszTmp; pszTmp++; } else { cEnd = ' '; } pszArgStart = pszTmp; iArgSize = 0; // Count the characters in the parameter. while( *pszTmp && *pszTmp != cEnd) { iArgSize++; pszTmp++; } if( !iArgSize && cEnd == ' ') { break; } // If 2nd pass, save the argument. if( !bFirstPass) { ppszArgV[ iArgC] = pszDestArg; if( iArgSize) { f_memcpy( pszDestArg, pszArgStart, iArgSize); } pszDestArg[ iArgSize] = 0; pszDestArg += (iArgSize + 1); } iArgC++; iTotalArgChars += iArgSize; // Skip trailing quote or blank. if( *pszTmp) { pszTmp++; } } if( bFirstPass) { if ((ppszArgV = (char **)Alloc( sizeof( char *) * iArgC, gv_lAllocRTag)) == NULL) { sdRet = 1; goto Exit; } if( (pszArgs = (char *)Alloc( iTotalArgChars + iArgC, gv_lAllocRTag)) == NULL) { sdRet = 1; goto Exit; } pszDestArg = pszArgs; bFirstPass = false; goto Parse_Args; } iThreadNameLen = (int)(moduleHandle->LDName[ 0]); if( (pszThreadName = (char *)Alloc( iThreadNameLen + 1, gv_lAllocRTag)) == NULL) { sdRet = 1; goto Exit; } f_memcpy( pszThreadName, (char *)(&moduleHandle->LDName[ 1]), iThreadNameLen); pszThreadName[ iThreadNameLen] = 0; if( (pArgData = (ARG_DATA *)Alloc( sizeof( ARG_DATA), gv_lAllocRTag)) == NULL) { sdRet = 1; goto Exit; } pArgData->ppszArgV = ppszArgV; pArgData->pszArgs = pszArgs; pArgData->iArgC = iArgC; pArgData->moduleHandle = moduleHandle; pArgData->pszThreadName = pszThreadName; gv_bMainRunning = TRUE; if( (hThread = kCreateThread( (BYTE *)"FTK main", f_nlmMainStub, NULL, 32768, (void *)pArgData)) == NULL) { gv_bMainRunning = FALSE; sdRet = 2; goto Exit; } if( kSetThreadLoadHandle( hThread, (LONG)moduleHandle) != 0) { (void)kDestroyThread( hThread); gv_bMainRunning = FALSE; sdRet = 2; goto Exit; } if( kScheduleThread( hThread) != 0) { (void)kDestroyThread( hThread); gv_bMainRunning = FALSE; sdRet = 2; goto Exit; } // Synchronized start if( moduleHandle->LDFlags & 4) { (void)kSemaphoreWait( gv_lFlmSyncSem); } Exit: if( sdRet != 0) { f_atomicDec( &gv_NetWareStartupCount); if( ppszArgV) { Free( ppszArgV); } if( pszArgs) { Free( pszArgs); } if( pszThreadName) { Free( pszThreadName); } if( pArgData) { Free( pArgData); } if( gv_lFlmSyncSem) { kSemaphoreFree( gv_lFlmSyncSem); gv_lFlmSyncSem = 0; } if( !gv_bUnloadCalled) { KillMe( moduleHandle); } } return( sdRet); }
ThreadContext* fnAddThreadCtx(long lTLSIndex, void *t) { ThreadContext* tip = NULL; ThreadContext* temp = NULL; if (g_tCtxSem) { #ifdef MPK_ON kSemaphoreWait(g_tCtxSem); #else WaitOnLocalSemaphore(g_tCtxSem); #endif //MPK_ON } // add a new one to the beginning of the list // tip = (ThreadContext *) malloc(sizeof(ThreadContext)); if (tip == NULL) { if (g_tCtxSem) { #ifdef MPK_ON kSemaphoreSignal(g_tCtxSem); #else SignalLocalSemaphore(g_tCtxSem); #endif //MPK_ON } return NULL; } #ifdef MPK_ON lTLSIndex = labs(kCurrentThread()); #else lTLSIndex = GetThreadID(); #endif //MPK_ON tip->next = NULL; tip->tid = lTLSIndex; tip->tInfo = t; if(g_ThreadCtx==NULL) { g_ThreadCtx = tip; } else { int count=0; //Traverse to the end temp = g_ThreadCtx; while(temp->next != NULL) { temp = temp->next; count++; } temp->next = tip; } if (g_tCtxSem) { #ifdef MPK_ON kSemaphoreSignal(g_tCtxSem); #else SignalLocalSemaphore(g_tCtxSem); #endif //MPK_ON } return tip; }