]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_win32.cpp
b32dd244133b3ee0f8cab925c075a8888682b217
[user/henk/code/inspircd.git] / src / threadengines / threadengine_win32.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "threadengines/threadengine_win32.h"
16
17 Win32ThreadEngine::Win32ThreadEngine(InspIRCd* Instance) : ThreadEngine(Instance)
18 {
19 }
20
21 void Win32ThreadEngine::Create(Thread* thread)
22 {
23         Win32ThreadData* data = new Win32ThreadData;
24         thread->state = data;
25
26         DWORD ThreadId = 0;
27         data->handle = CreateThread(NULL,0,Win32ThreadEngine::Entry,thread,0,&ThreadId);
28
29         if (data->handle == NULL)
30         {
31                 thread->state = NULL;
32                 delete data;
33                 throw CoreException(std::string("Unable to create new Win32ThreadEngine: ") + dlerror());
34         }
35 }
36
37 Win32ThreadEngine::~Win32ThreadEngine()
38 {
39 }
40
41 DWORD WINAPI Win32ThreadEngine::Entry(void* parameter)
42 {
43         Thread* pt = reinterpret_cast<Thread*>(parameter);
44         pt->Run();
45         return 0;
46 }
47
48 void Win32ThreadData::FreeThread(Thread* thread)
49 {
50         thread->SetExitFlag();
51         WaitForSingleObject(handle,INFINITE);
52 }
53
54
55 MutexFactory::MutexFactory(InspIRCd* Instance) : ServerInstance(Instance)
56 {
57 }
58
59 Mutex* MutexFactory::CreateMutex()
60 {
61         return new Win32Mutex();
62 }
63
64 Win32Mutex::Win32Mutex() : Mutex()
65 {
66         InitializeCriticalSection(&wutex);
67 }
68
69 Win32Mutex::~Win32Mutex()
70 {
71         DeleteCriticalSection(&wutex);
72 }
73
74 void Win32Mutex::Enable(bool enable)
75 {
76         if (enable)
77                 EnterCriticalSection(&wutex);
78         else
79                 LeaveCriticalSection(&wutex);
80 }