]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/threadengines/threadengine_win32.cpp
Microsoft, in their "infinite wisdom" decide to have no sensible naming convention...
[user/henk/code/inspircd.git] / src / threadengines / threadengine_win32.cpp
index 5bc88fce0f4f1bbf02b5cfec125a393e89cfa7ca..5666113671fb6d5a56e13912dda73fc78cd4d7d3 100644 (file)
  * ---------------------------------------------------
  */
 
-/* $Core: libIRCDthreadengine */
-
-/*********        DEFAULTS       **********/
-/* $ExtraSources: socketengines/socketengine_pthread.cpp */
-/* $ExtraObjects: socketengine_pthread.o */
-
-/* $If: USE_WIN32 */
-/* $ExtraSources: socketengines/socketengine_win32.cpp */
-/* $ExtraObjects: socketengine_win32.o */
-/* $EndIf */
-
 #include "inspircd.h"
 #include "threadengines/threadengine_win32.h"
-#include <pthread.h>
 
-pthread_mutex_t MyMutex = PTHREAD_MUTEX_INITIALIZER;
+CRITICAL_SECTION MyMutex;
 
 Win32ThreadEngine::Win32ThreadEngine(InspIRCd* Instance) : ThreadEngine(Instance)
 {
+       InitializeCriticalSection(&MyMutex);
 }
 
 void Win32ThreadEngine::Create(Thread* thread_to_init)
 {
-       pthread_attr_t attribs;
-       pthread_attr_init(&attribs);
-       pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE);
-       pthread_t* MyPThread = new pthread_t;
+       Mutex(true);
+       HANDLE* MyThread = new HANDLE;
+       DWORD ThreadId = 0;
 
-       if (pthread_create(MyPThread, &attribs, Win32ThreadEngine::Entry, (void*)this) != 0)
+       if (NULL == (*MyThread = CreateThread(NULL,0,Win32ThreadEngine::Entry,this,0,&ThreadId)))
        {
-               delete MyPThread;
-               throw CoreException("Unable to create new Win32ThreadEngine: " + std::string(strerror(errno)));
+               delete MyThread;
+               Mutex(false);
+               throw CoreException(std::string("Unable to create new Win32ThreadEngine: ") + dlerror());
        }
 
-       pthread_attr_destroy(&attribs);
-
        NewThread = thread_to_init;
        NewThread->Creator = this;
-       NewThread->Extend("pthread", MyPThread);
+       NewThread->Extend("winthread", MyThread);
+       Mutex(false);
+
+       while (NewThread)
+               SleepEx(100, false);
 }
 
 Win32ThreadEngine::~Win32ThreadEngine()
 {
+       DeleteCriticalSection(&MyMutex);
 }
 
 void Win32ThreadEngine::Run()
 {
-       NewThread->Run();
+       Mutex(true);
+       Thread* nt = NewThread;
+       NewThread = NULL;
+       Mutex(false);
+       nt->Run();
 }
 
 bool Win32ThreadEngine::Mutex(bool enable)
 {
        if (enable)
-               pthread_mutex_lock(&MyMutex);
+               EnterCriticalSection(&MyMutex);
        else
-               pthread_mutex_unlock(&MyMutex);
+               LeaveCriticalSection(&MyMutex);
 
        return false;
 }
 
-void* Win32ThreadEngine::Entry(void* parameter)
+DWORD WINAPI Win32ThreadEngine::Entry(void* parameter)
 {
        ThreadEngine * pt = (ThreadEngine*)parameter;
        pt->Run();
-       return NULL;
+       return 0;
 }
 
 void Win32ThreadEngine::FreeThread(Thread* thread)
 {
-       pthread_t* pthread = NULL;
-       if (thread->GetExt("pthread", pthread))
+       HANDLE* winthread = NULL;
+       if (thread->GetExt("winthread", winthread))
        {
                thread->SetExitFlag();
-               int rc;
-               void* status;
-               rc = pthread_join(*pthread, &status);
-               delete pthread;
+               WaitForSingleObject(*winthread,INFINITE);
+               delete winthread;
        }
 }
 
+