mirror of
https://port.numenaute.org/aleajactaest/khanat-code-old.git
synced 2024-11-23 15:26:16 +00:00
Changed: #1227 some simplification, added exceptions instead of nlerror
This commit is contained in:
parent
d73c485f23
commit
1d1af58c52
1 changed files with 16 additions and 30 deletions
|
@ -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.");
|
||||||
|
|
||||||
|
if(pthread_setspecific(threadSpecificKey, this) != 0)
|
||||||
|
throw EThread("cannot set main thread ptr in thread specific storage.");
|
||||||
|
}
|
||||||
|
|
||||||
|
~CPMainThread()
|
||||||
|
{
|
||||||
|
if(pthread_key_delete(threadSpecificKey) != 0)
|
||||||
|
throw EThread("cannot delete thread specific storage key.");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Holds the IThread instance pointer representing the main thread. */
|
/* Holds the thread instance representing the main thread. */
|
||||||
static CPMainThread* mainThread = NULL;
|
static CPMainThread mainThread = CPMainThread();
|
||||||
|
|
||||||
/* 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()
|
|
||||||
{
|
|
||||||
if(pthread_key_delete(threadSpecificKey))
|
|
||||||
nlerror("cannot delete thread specific storage key.");
|
|
||||||
|
|
||||||
delete mainThread;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Run init/release routines at static construction/destruction time. */
|
|
||||||
static CPThreadStaticInit pThreadStaticInit = CPThreadStaticInit();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 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);
|
||||||
|
|
Loading…
Reference in a new issue