From 7b6eae36661bc798f69b22393b85a4f06d533cf6 Mon Sep 17 00:00:00 2001 From: danieldg Date: Mon, 23 Mar 2009 18:48:32 +0000 Subject: ThreadEngine: remove excessive mutex use on thread creation git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11249 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/threadengines/threadengine_pthread.cpp | 104 +++++++---------------------- src/threadengines/threadengine_win32.cpp | 64 +++++------------- 2 files changed, 39 insertions(+), 129 deletions(-) (limited to 'src/threadengines') diff --git a/src/threadengines/threadengine_pthread.cpp b/src/threadengines/threadengine_pthread.cpp index 21f55108c..9fc9cc4b4 100644 --- a/src/threadengines/threadengine_pthread.cpp +++ b/src/threadengines/threadengine_pthread.cpp @@ -16,101 +16,45 @@ #include #include -pthread_mutex_t MyMutex = PTHREAD_MUTEX_INITIALIZER; - PThreadEngine::PThreadEngine(InspIRCd* Instance) : ThreadEngine(Instance) { } -void PThreadEngine::Create(Thread* thread_to_init) +static void* entry_point(void* parameter) { - pthread_attr_t attribs; - pthread_attr_init(&attribs); - 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) - { - delete MyPThread; - Mutex(false); - throw CoreException("Unable to create new PThreadEngine: " + std::string(strerror(errno))); - } - - pthread_attr_destroy(&attribs); - - NewThread = 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); -} + /* Recommended by nenolod, signal safety on a per-thread basis */ + sigset_t set; + sigemptyset(&set); + sigaddset(&set, SIGPIPE); + pthread_sigmask(SIG_BLOCK, &set, NULL); -PThreadEngine::~PThreadEngine() -{ + Thread* pt = reinterpret_cast(parameter); + pt->Run(); + return parameter; } -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(); -} -bool PThreadEngine::Mutex(bool enable) +void PThreadEngine::Start(Thread* thread) { - if (enable) - pthread_mutex_lock(&MyMutex); - else - pthread_mutex_unlock(&MyMutex); + PThreadData* data = new PThreadData; + thread->state = data; - return false; + if (pthread_create(&data->pthread_id, NULL, entry_point, thread) != 0) + { + thread->state = NULL; + delete data; + throw CoreException("Unable to create new PThreadEngine: " + std::string(strerror(errno))); + } } -void* PThreadEngine::Entry(void* parameter) +PThreadEngine::~PThreadEngine() { - /* Recommended by nenolod, signal safety on a per-thread basis */ - sigset_t set; - sigemptyset(&set); - sigaddset(&set, SIGPIPE); - if(pthread_sigmask(SIG_BLOCK, &set, NULL)) - signal(SIGPIPE, SIG_IGN); - - ThreadEngine * pt = (ThreadEngine*)parameter; - pt->Run(); - return NULL; } -void PThreadEngine::FreeThread(Thread* thread) +void PThreadData::FreeThread(Thread* thread) { - pthread_t* pthread = NULL; - if (thread->GetExt("pthread", pthread)) - { - thread->SetExitFlag(); - int rc; - void* status; - rc = pthread_join(*pthread, &status); - delete pthread; - } + thread->SetExitFlag(true); + pthread_join(pthread_id, NULL); } MutexFactory::MutexFactory(InspIRCd* Instance) : ServerInstance(Instance) @@ -119,10 +63,10 @@ MutexFactory::MutexFactory(InspIRCd* Instance) : ServerInstance(Instance) Mutex* MutexFactory::CreateMutex() { - return new PosixMutex(this->ServerInstance); + return new PosixMutex(); } -PosixMutex::PosixMutex(InspIRCd* Instance) : Mutex(Instance) +PosixMutex::PosixMutex() : Mutex() { pthread_mutex_init(&putex, NULL); } diff --git a/src/threadengines/threadengine_win32.cpp b/src/threadengines/threadengine_win32.cpp index 1a1755f15..b32dd2441 100644 --- a/src/threadengines/threadengine_win32.cpp +++ b/src/threadengines/threadengine_win32.cpp @@ -14,75 +14,41 @@ #include "inspircd.h" #include "threadengines/threadengine_win32.h" -CRITICAL_SECTION MyMutex; - Win32ThreadEngine::Win32ThreadEngine(InspIRCd* Instance) : ThreadEngine(Instance) { - InitializeCriticalSection(&MyMutex); } -void Win32ThreadEngine::Create(Thread* thread_to_init) +void Win32ThreadEngine::Create(Thread* thread) { - Mutex(true); - HANDLE* MyThread = new HANDLE; + Win32ThreadData* data = new Win32ThreadData; + thread->state = data; + DWORD ThreadId = 0; + data->handle = CreateThread(NULL,0,Win32ThreadEngine::Entry,thread,0,&ThreadId); - if (NULL == (*MyThread = CreateThread(NULL,0,Win32ThreadEngine::Entry,this,0,&ThreadId))) + if (data->handle == NULL) { - delete MyThread; - Mutex(false); + thread->state = NULL; + delete data; throw CoreException(std::string("Unable to create new Win32ThreadEngine: ") + dlerror()); } - - NewThread = thread_to_init; - NewThread->Creator = this; - NewThread->Extend("winthread", MyThread); - Mutex(false); - - while (NewThread) - SleepEx(100, false); } Win32ThreadEngine::~Win32ThreadEngine() { - DeleteCriticalSection(&MyMutex); -} - -void Win32ThreadEngine::Run() -{ - Mutex(true); - Thread* nt = NewThread; - NewThread = NULL; - Mutex(false); - nt->Run(); -} - -bool Win32ThreadEngine::Mutex(bool enable) -{ - if (enable) - EnterCriticalSection(&MyMutex); - else - LeaveCriticalSection(&MyMutex); - - return false; } DWORD WINAPI Win32ThreadEngine::Entry(void* parameter) { - ThreadEngine * pt = (ThreadEngine*)parameter; + Thread* pt = reinterpret_cast(parameter); pt->Run(); return 0; } -void Win32ThreadEngine::FreeThread(Thread* thread) +void Win32ThreadData::FreeThread(Thread* thread) { - HANDLE* winthread = NULL; - if (thread->GetExt("winthread", winthread)) - { - thread->SetExitFlag(); - WaitForSingleObject(*winthread,INFINITE); - delete winthread; - } + thread->SetExitFlag(); + WaitForSingleObject(handle,INFINITE); } @@ -92,10 +58,10 @@ MutexFactory::MutexFactory(InspIRCd* Instance) : ServerInstance(Instance) Mutex* MutexFactory::CreateMutex() { - return new Win32Mutex(this->ServerInstance); + return new Win32Mutex(); } -Win32Mutex::Win32Mutex(InspIRCd* Instance) : Mutex(Instance) +Win32Mutex::Win32Mutex() : Mutex() { InitializeCriticalSection(&wutex); } @@ -111,4 +77,4 @@ void Win32Mutex::Enable(bool enable) EnterCriticalSection(&wutex); else LeaveCriticalSection(&wutex); -} \ No newline at end of file +} -- cgit v1.2.3