📄 krnlsync.cpp
字号:
#elif defined(CONFIG_MT_WIN32)
::TerminateThread( tThread, 0 );
Cleanup();
#else
# error Unsupported!
#endif
return false;
};
return true;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
POSIXThread::~POSIXThread()
{
if ( ___pTheMainThread!=this )
{
if ( !IsTerminated() )
{
Terminate( 0 );
InternalWaitForJoin( tThread );
};
Cleanup();
assert( tThread==BAD_THREAD );
};
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: public
Description:
\*____________________________________________________________________________*/
unsigned long POSIXThread::ThreadFunction()
{
assert( fnThread );
PIMUSTBETRUE( Internal_SetData( tTlsAllocatorKey, pAllocator ) );
unsigned long ulRet=(*fnThread)( ulData );
Cleanup();
return ulRet;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, private
Description:
\*____________________________________________________________________________*/
bool POSIXThread::InternalWaitForJoin( THREAD_T tThread )
{
if ( tThread==BAD_THREAD )
{ return true; /* --- thread was never created or has finished --- */ };
void *pV;
#if defined(CONFIG_MT_PTHREAD)
int iRet = ::pthread_join( tThread, &pV );
/* ---
There are two error conditions
--- */
if ( !iRet ) { return true; };
if ( tThread==BAD_THREAD )
{ /* thread terminated just before thr_join() */ return true; };
/* --- error --- */
PIRETERR(iRet);
return false;
#elif defined(CONFIG_MT_SOLARIS)
int iRet = ::thr_join( tThread, &tThread, &pV );
/* ---
There are two error conditions
--- */
if ( !iRet ) { return true; };
if ( tThread==BAD_THREAD )
{ /* thread terminated just before thr_join() */ return true; };
/* --- error --- */
PIRETERR(iRet);
return false;
#elif defined(CONFIG_MT_WIN32)
if ( ::WaitForSingleObject( tThread, INFINITE ) != WAIT_FAILED )
{ return true; };
if ( tThread==BAD_THREAD )
{ /* thread terminated just before join */ return true; };
/* --- this is an error --- */
PIOSERR;
return false;
#else
# error Unsupported
#endif
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: public
Description:
\*____________________________________________________________________________*/
bool POSIXThread::Terminate( unsigned long ulExitCode )
{
if ( tThread==BAD_THREAD )
{ return true; /* already terminated */ };
if ( ___pTheMainThread == this )
{
#if defined(CONFIG_CPP_EXCEPTIONS)
throw PI_NEW( StackUnwindException() );
#else
longjmp( ___tJmpBuf, 37 );
#endif
};
#if defined(CONFIG_MT_PTHREAD)
if ( tThread == ::pthread_self() )
{ ::pthread_exit( (void *)ulExitCode ); };
int iRet = ::pthread_kill( tThread, SIGTERM );
if ( iRet )
{ tThread=BAD_THREAD; }
else
{ PIRETERR(iRet); return false; };
#elif defined(CONFIG_MT_SOLARIS)
if ( tThread == ::thr_self() )
{ ::thr_exit( (void *)ulExitCode ); };
int iRet = ::thr_kill( tThread, SIGTERM );
if ( iRet )
{ tThread=BAD_THREAD; }
else
{ PIRETERR(iRet); return false; };
#elif defined(CONFIG_MT_WIN32)
if ( !::TerminateThread( tThread, 0 ) )
{ PIOSERR; return false; };
#else
# error Unsupported
#endif
return true;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: public
Description:
\*____________________________________________________________________________*/
bool POSIXThread::Suspend()
{
#if defined(CONFIG_MT_PTHREAD)
PIRETERR(PIAPI_NOTSUPPORTED);
return false;
#elif defined(CONFIG_MT_SOLARIS)
int iRet = ::thr_suspend( tThread );
if ( iRet )
{
PIRETERR(iRet);
return false;
}
#elif defined(CONFIG_MT_WIN32)
if ( ::SuspendThread( tThread ) == 0xFFFFFFFF )
{ PIOSERR; return false; };
#else
# error Unsupported!
#endif
return true;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: public
Description:
\*____________________________________________________________________________*/
bool POSIXThread::Resume()
{
#if defined(CONFIG_MT_PTHREAD)
PIRETERR(PIAPI_NOTSUPPORTED);
return false;
#elif defined(CONFIG_MT_SOLARIS)
int iRet = ::thr_continue( tThread );
if ( iRet )
{
PIRETERR(iRet);
return false;
}
#elif defined(CONFIG_MT_WIN32)
if ( ::ResumeThread( tThread ) == 0xFFFFFFFF )
{ PIOSERR; return false; };
#else
# error Unsupported!
#endif
return true;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: private
Description:
\*____________________________________________________________________________*/
bool POSIXThread::InternalSetPriority( int iPriority )
{
int iPOSIXThreadPriority=THREAD_PRIORITY_NORMAL;
switch( iPriority )
{
case PITHREAD_PRIORITY_LOWEST:
iPOSIXThreadPriority=THREAD_PRIORITY_LOWEST; break;
case PITHREAD_PRIORITY_LOW:
iPOSIXThreadPriority=THREAD_PRIORITY_BELOW_NORMAL; break;
case PITHREAD_PRIORITY_HIGH:
iPOSIXThreadPriority=THREAD_PRIORITY_ABOVE_NORMAL; break;
case PITHREAD_PRIORITY_HIGHEST:
iPOSIXThreadPriority=THREAD_PRIORITY_HIGHEST; break;
case PITHREAD_PRIORITY_MED:
default:;
};
#if defined(CONFIG_MT_PTHREAD)
sched_param schedParam;
int iPolicy;
int iRet = ::pthread_getschedparam( tThread, &iPolicy, &schedParam );
if (!iRet)
{
iPolicy = SCHED_FIFO;
schedParam.sched_priority = iPOSIXThreadPriority;
iRet = ::pthread_setschedparam( tThread, iPolicy, &schedParam );
if (!iRet)
{ return true; }
else
{
if ( EPERM == errno )
/*
** Missing privileges to change scheduler params
*/
{ return true; }
else
{
/*
** Another error occured
*/
PIOSERR;
return false;
}
}
}
else
{
PIOSERR;
return false;
}
#elif defined(CONFIG_MT_SOLARIS)
int iRet = ::thr_setprio( tThread, iPOSIXThreadPriority );
if ( iRet )
{
PIRETERR(iRet);
return false;
};
#elif defined(CONFIG_MT_WIN32)
if ( !::SetThreadPriority( tThread, iPOSIXThreadPriority ) )
{
PIOSERR;
return false;
};
#endif
return true;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
bool POSIXThread::SetPriority( int iPriority )
{
return InternalSetPriority( iPriority );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public:
Description:
\*____________________________________________________________________________*/
Semaphore *Platform::AllocLocalMutex()
{
return PI_NEW( POSIXLocalMutex() );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public:
Description:
\*____________________________________________________________________________*/
Semaphore *Platform::AllocLocalSemaphore( int iInitialCount, int iMaxCount )
{
#if defined(CONFIG_MT_PTHREAD)
return AllocGlobalSemaphore( iInitialCount, iMaxCount );
#else
return PI_NEW( POSIXLocalSemaphore( iInitialCount, iMaxCount ) );
#endif
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public:
Description:
\*____________________________________________________________________________*/
Thread *Platform::AllocThread( void *pAllocator, int iStackSize )
{
return PI_NEW( POSIXThread( (Allocator *)pAllocator, iStackSize ) );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public:
Description:
\*____________________________________________________________________________*/
void Platform::YieldThread()
{
#if defined(CONFIG_MT_PTHREAD)
/*
** NOTE: how to make a pthread yield???
*/
# if !defined(CONFIG_NO_YIELD)
::yield();
# else
::sched_yield();
# endif
#elif defined(CONFIG_MT_SOLARIS)
::thr_yield();
#elif defined(CONFIG_MT_WIN32)
::Sleep( 0 ); /* --- This is how Win32 gives up timeslice --- */
#else
# error Unsupported!
#endif
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public:
Description:
\*____________________________________________________________________________*/
int Platform::WaitForThread( Thread *pThread )
{
return
POSIXThread::InternalWaitForJoin( (THREAD_T)pThread->GetSystemHandle() ) ?
0 : -1;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public:
Description:
\*____________________________________________________________________________*/
Thread *Platform::GetCurrentThread()
{
void *pV = 0;
if ( Internal_GetData( tThreadObjectKey, &pV ) )
{
return (Thread *)pV;
};
PIERROR( PIAPI_EINVAL );
return 0;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public:
Description:
\*____________________________________________________________________________*/
Thread *Platform::GetMainThread()
{
return ___pTheMainThread;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public:
Description:
\*____________________________________________________________________________*/
static void fnDummy( void * ) {};
int Platform::AllocThreadKey()
{
THREAD_KEY_T tKey = 0;
#if defined(CONFIG_MT_PTHREAD)
int iRet = ::pthread_key_create( &tKey, fnDummy );
if ( iRet )
{
PIRETERR(iRet);
assert( 0 );
return -1;
}
#elif defined(CONFIG_MT_SOLARIS)
int iRet = ::thr_keycreate( &tKey, fnDummy );
if ( iRet )
{
PIRETERR(iRet);
assert( 0 );
return -1;
}
#elif defined(CONFIG_MT_WIN32)
if ( ( tKey = ::TlsAlloc() )==0xFFFFFFFF )
{ PIOSERR; return -1; };
#else
# error Unsupported!
#endif
return tKey;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public:
Description:
\*____________________________________________________________________________*/
bool Platform::DeleteThreadKey( int iKey )
{
#if defined(CONFIG_MT_PTHREAD)
int iRet = ::pthread_key_delete( iKey );
if ( iRet )
{
PIRETERR(iRet);
return false;
};
#elif defined(CONFIG_MT_SOLARIS)
# if !defined(CONFIG_NO_THR_DELETE)
int iRet = ::thr_keydelete( iKey );
if ( iRet )
{
PIRETERR(iRet);
return false;
};
# else
(void)iKey; /* --- suppress 'arg. not used' warnings --- */
# endif
#elif defined(CONFIG_MT_WIN32)
if ( !::TlsFree( iKey ) )
{ PIOSERR; return false; };
#else
# error Unsupported!
#endif
return true;
}
#endif /* defined(CONFIG_MULTITHREADED) && !defined(CONFIG_MT_USER) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -