|
|
@ -29,41 +29,27 @@ namespace NLMISC {
|
|
|
|
/* Key for thread specific storage holding IThread pointer. */
|
|
|
|
/* Key for thread specific storage holding IThread pointer. */
|
|
|
|
static pthread_key_t threadSpecificKey;
|
|
|
|
static pthread_key_t threadSpecificKey;
|
|
|
|
|
|
|
|
|
|
|
|
/* Hand crafted IThread representing the main thread. */
|
|
|
|
/* Special thread type representing the main thread. */
|
|
|
|
struct CPMainThread : public CPThread
|
|
|
|
struct CPMainThread : public CPThread
|
|
|
|
{
|
|
|
|
{
|
|
|
|
CPMainThread() : CPThread(NULL, 0)
|
|
|
|
CPMainThread() : CPThread(NULL, 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(pthread_setspecific(threadSpecificKey, this))
|
|
|
|
if(pthread_key_create(&threadSpecificKey, NULL) != 0)
|
|
|
|
nlerror("cannot set main thread ptr in thread specific storage.");
|
|
|
|
throw EThread("cannot create thread specific storage key.");
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Holds the IThread instance pointer representing the main thread. */
|
|
|
|
if(pthread_setspecific(threadSpecificKey, this) != 0)
|
|
|
|
static CPMainThread* mainThread = NULL;
|
|
|
|
throw EThread("cannot set main thread ptr in thread specific storage.");
|
|
|
|
|
|
|
|
|
|
|
|
/* Init/Release routines for thread specific storage key, main thread object. */
|
|
|
|
|
|
|
|
struct CPThreadStaticInit
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
CPThreadStaticInit()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(pthread_key_create(&threadSpecificKey, NULL))
|
|
|
|
|
|
|
|
nlerror("cannot create thread specific storage key.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mainThread = new CPMainThread;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
~CPThreadStaticInit()
|
|
|
|
~CPMainThread()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(pthread_key_delete(threadSpecificKey))
|
|
|
|
if(pthread_key_delete(threadSpecificKey) != 0)
|
|
|
|
nlerror("cannot delete thread specific storage key.");
|
|
|
|
throw EThread("cannot delete thread specific storage key.");
|
|
|
|
|
|
|
|
|
|
|
|
delete mainThread;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Run init/release routines at static construction/destruction time. */
|
|
|
|
/* Holds the thread instance representing the main thread. */
|
|
|
|
static CPThreadStaticInit pThreadStaticInit = CPThreadStaticInit();
|
|
|
|
static CPMainThread mainThread = CPMainThread();
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* The IThread static creator
|
|
|
|
* The IThread static creator
|
|
|
@ -89,8 +75,8 @@ static void *ProxyFunc( void *arg )
|
|
|
|
CPThread *parent = (CPThread*)arg;
|
|
|
|
CPThread *parent = (CPThread*)arg;
|
|
|
|
|
|
|
|
|
|
|
|
// Set this thread's thread specific storage to IThread instance pointer
|
|
|
|
// Set this thread's thread specific storage to IThread instance pointer
|
|
|
|
if(pthread_setspecific(threadSpecificKey, parent))
|
|
|
|
if(pthread_setspecific(threadSpecificKey, parent) != 0)
|
|
|
|
nlerror("cannot set thread ptr in thread specific storage.");
|
|
|
|
throw EThread("cannot set thread ptr in thread specific storage.");
|
|
|
|
|
|
|
|
|
|
|
|
// Allow to terminate the thread without cancellation point
|
|
|
|
// Allow to terminate the thread without cancellation point
|
|
|
|
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
|
|
|
|
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
|
|
|
|