]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_win32.cpp
5bc88fce0f4f1bbf02b5cfec125a393e89cfa7ca
[user/henk/code/inspircd.git] / src / threadengines / threadengine_win32.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDthreadengine */
15
16 /*********        DEFAULTS       **********/
17 /* $ExtraSources: socketengines/socketengine_pthread.cpp */
18 /* $ExtraObjects: socketengine_pthread.o */
19
20 /* $If: USE_WIN32 */
21 /* $ExtraSources: socketengines/socketengine_win32.cpp */
22 /* $ExtraObjects: socketengine_win32.o */
23 /* $EndIf */
24
25 #include "inspircd.h"
26 #include "threadengines/threadengine_win32.h"
27 #include <pthread.h>
28
29 pthread_mutex_t MyMutex = PTHREAD_MUTEX_INITIALIZER;
30
31 Win32ThreadEngine::Win32ThreadEngine(InspIRCd* Instance) : ThreadEngine(Instance)
32 {
33 }
34
35 void Win32ThreadEngine::Create(Thread* thread_to_init)
36 {
37         pthread_attr_t attribs;
38         pthread_attr_init(&attribs);
39         pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE);
40         pthread_t* MyPThread = new pthread_t;
41
42         if (pthread_create(MyPThread, &attribs, Win32ThreadEngine::Entry, (void*)this) != 0)
43         {
44                 delete MyPThread;
45                 throw CoreException("Unable to create new Win32ThreadEngine: " + std::string(strerror(errno)));
46         }
47
48         pthread_attr_destroy(&attribs);
49
50         NewThread = thread_to_init;
51         NewThread->Creator = this;
52         NewThread->Extend("pthread", MyPThread);
53 }
54
55 Win32ThreadEngine::~Win32ThreadEngine()
56 {
57 }
58
59 void Win32ThreadEngine::Run()
60 {
61         NewThread->Run();
62 }
63
64 bool Win32ThreadEngine::Mutex(bool enable)
65 {
66         if (enable)
67                 pthread_mutex_lock(&MyMutex);
68         else
69                 pthread_mutex_unlock(&MyMutex);
70
71         return false;
72 }
73
74 void* Win32ThreadEngine::Entry(void* parameter)
75 {
76         ThreadEngine * pt = (ThreadEngine*)parameter;
77         pt->Run();
78         return NULL;
79 }
80
81 void Win32ThreadEngine::FreeThread(Thread* thread)
82 {
83         pthread_t* pthread = NULL;
84         if (thread->GetExt("pthread", pthread))
85         {
86                 thread->SetExitFlag();
87                 int rc;
88                 void* status;
89                 rc = pthread_join(*pthread, &status);
90                 delete pthread;
91         }
92 }
93