RCODE FLMAPI f_semWait(
	F_SEM			hSem,
	FLMUINT		uiTimeout)
{
	DWORD			dwStatus;

	for( ;;)
	{
		dwStatus = WaitForSingleObjectEx( ((sema_t *)hSem)->hWinSem,
														uiTimeout, true);

		if( dwStatus == WAIT_OBJECT_0)
		{
			f_atomicDec( &((sema_t *)hSem)->uiSignalCount);
			return( NE_FLM_OK);
		}

		if( dwStatus == WAIT_IO_COMPLETION)
		{
			continue;
		}

		break;
	}

	return( RC_SET( NE_FLM_WAIT_TIMEOUT));
}
extern "C" void f_nlmExitPoint(void)
{
	if( f_atomicDec( &gv_NetWareStartupCount) > 0)
	{
		return;
	}
	
	gv_bUnloadCalled = TRUE;

	if( gv_fnExit)
	{
		(*gv_fnExit)();
		gv_fnExit = NULL;
	}

	while( gv_bMainRunning)
	{
		kYieldThread();
	}

	f_nssUninitialize();
	
	if( gv_lFlmSyncSem)
	{
		kSemaphoreFree( gv_lFlmSyncSem);
		gv_lFlmSyncSem = 0;
	}

	if( gv_lAllocRTag)
	{
		ReturnResourceTag( gv_lAllocRTag, 1);
		gv_lAllocRTag = 0;
	}
}
	FLMINT FTKAPI Release( void)
	{
		FLMINT		iRefCnt = f_atomicDec( &m_refCnt);
		
		if( !iRefCnt)
		{
			delete this;
		}
		
		return( iRefCnt);
	}
/******************************************************************************
Desc:
******************************************************************************/
void UnlockModule(void)
{
	f_atomicDec( &gv_lockCount);
}
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);
}
/****************************************************************************
Desc:
****************************************************************************/
FLMINT F_IOBuffer::Release(
	FLMBOOL				bMutexAlreadyLocked)
{
	FLMINT				iRefCnt;
	F_MUTEX				hMutex = F_MUTEX_NULL;
	F_IOBufferMgr *	pBufferMgr = NULL;

	if( m_pBufferMgr && !bMutexAlreadyLocked)
	{
		hMutex = m_pBufferMgr->m_hMutex;
		f_assertMutexNotLocked( hMutex);
		f_mutexLock( hMutex);
	}
	
	if( m_refCnt <= 2)
	{
		if( m_pBufferMgr && m_eList != MGR_LIST_NONE)
		{
			f_assert( m_eList != MGR_LIST_PENDING);
			m_pBufferMgr->unlinkFromList( this);
		}
	}
	
	if( m_refCnt == 2)
	{
		if( m_pAsyncClient)
		{
			m_pAsyncClient->Release();
			m_pAsyncClient = NULL;
		}

		if( (pBufferMgr = m_pBufferMgr) != NULL)
		{
			if( m_pBufferMgr->m_bReuseBuffers)
			{
				m_pBufferMgr->linkToList( &m_pBufferMgr->m_pFirstAvail, this);
			}
			else
			{
				f_assert( m_pBufferMgr->m_uiTotalBuffers);
				f_assert( m_pBufferMgr->m_uiTotalBufferBytes >= m_uiBufferSize);

				f_atomicDec( &m_refCnt);
				m_pBufferMgr->m_uiTotalBuffers--;
				m_pBufferMgr->m_uiTotalBufferBytes -= m_uiBufferSize;
				m_pBufferMgr = NULL;
			}
			
			if( pBufferMgr->m_pAvailNotify)
			{
				f_notifySignal( pBufferMgr->m_pAvailNotify, NE_FLM_OK);
				pBufferMgr->m_pAvailNotify = NULL;
			}
		}
	}
	
	iRefCnt = f_atomicDec( &m_refCnt);

	if( hMutex != F_MUTEX_NULL)
	{
		f_mutexUnlock( hMutex);
	}
	
	if( !iRefCnt)
	{
		delete this;
	}
	
	return( iRefCnt);
}