diff options
author | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-02-22 19:43:47 +0000 |
---|---|---|
committer | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-02-22 19:43:47 +0000 |
commit | e71625d79ce4bf025125a03b1dfaf5cf9086c531 (patch) | |
tree | 1275ab93ba23dfe2103d190a1161915cdbd5cdc0 | |
parent | 010812f2a791b2048a76adbf40771ae166e1a46c (diff) |
Comments
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@9010 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r-- | src/threadengines/threadengine_pthread.cpp | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/threadengines/threadengine_pthread.cpp b/src/threadengines/threadengine_pthread.cpp index 6a48bd025..2f09cc305 100644 --- a/src/threadengines/threadengine_pthread.cpp +++ b/src/threadengines/threadengine_pthread.cpp @@ -28,6 +28,10 @@ void PThreadEngine::Create(Thread* thread_to_init) pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE); pthread_t* MyPThread = new pthread_t; + /* Create a thread in a mutex. This prevents whacking the member value NewThread, + * and also prevents recursive creation of threads by mistake (instead, the thread + * will just deadlock itself) + */ Mutex(true); if (pthread_create(MyPThread, &attribs, PThreadEngine::Entry, (void*)this) != 0) @@ -43,8 +47,12 @@ void PThreadEngine::Create(Thread* thread_to_init) NewThread->Creator = this; NewThread->Extend("pthread", MyPThread); + /* Always unset a mutex if you set it */ Mutex(false); + /* Wait for the PThreadEngine::Run method to take a copy of the + * pointer and clear this member value + */ while (NewThread) usleep(1000); } @@ -55,10 +63,15 @@ PThreadEngine::~PThreadEngine() void PThreadEngine::Run() { + /* Take a copy of the member value, then clear it. Do this + * in a mutex so that we can be sure nothing else is looking + * at it. + */ Mutex(true); Thread* nt = NewThread; NewThread = NULL; Mutex(false); + /* Now we have our own safe copy, call the object on it */ nt->Run(); } |