]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/threadengines/threadengine_win32.h
Merge pull request #109 from Justasic/insp20
[user/henk/code/inspircd.git] / include / threadengines / threadengine_win32.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #ifndef THREADENGINE_WIN32_H
22 #define THREADENGINE_WIN32_H
23
24 #include "inspircd_config.h"
25 #include "base.h"
26
27 class Thread;
28
29 /** The ThreadEngine class has the responsibility of initialising
30  * Thread derived classes. It does this by creating operating system
31  * level threads which are then associated with the class transparently.
32  * This allows Thread classes to be derived without needing to know how
33  * the OS implements threads. You should ensure that any sections of code
34  * that use threads are threadsafe and do not interact with any other
35  * parts of the code which are NOT known threadsafe! If you really MUST
36  * access non-threadsafe code from a Thread, use the Mutex class to wrap
37  * access to the code carefully.
38  */
39 class CoreExport ThreadEngine
40 {
41  public:
42
43         ThreadEngine();
44
45         virtual ~ThreadEngine();
46
47         static DWORD WINAPI Entry(void* parameter);
48
49         /** Create a new thread. This takes an already allocated
50           * Thread* pointer and initializes it to use this threading
51           * engine. On failure, this function may throw a CoreException.
52           * @param thread_to_init Pointer to a newly allocated Thread
53           * derived object.
54           */
55         void Start(Thread* thread_to_init);
56
57         /** Returns the thread engine's name for display purposes
58          * @return The thread engine name
59          */
60         const std::string GetName()
61         {
62                 return "windows-thread";
63         }
64 };
65
66 class CoreExport ThreadData
67 {
68  public:
69         HANDLE handle;
70         void FreeThread(Thread* toFree);
71 };
72
73 /** The Mutex class represents a mutex, which can be used to keep threads
74  * properly synchronised. Use mutexes sparingly, as they are a good source
75  * of thread deadlocks etc, and should be avoided except where absolutely
76  * neccessary. Note that the internal behaviour of the mutex varies from OS
77  * to OS depending on the thread engine, for example in windows a Mutex
78  * in InspIRCd uses critical sections, as they are faster and simpler to
79  * manage.
80  */
81 class CoreExport Mutex
82 {
83  private:
84         CRITICAL_SECTION wutex;
85  public:
86         Mutex()
87         {
88                 InitializeCriticalSection(&wutex);
89         }
90         void Lock()
91         {
92                 EnterCriticalSection(&wutex);
93         }
94         void Unlock()
95         {
96                 LeaveCriticalSection(&wutex);
97         }
98         ~Mutex()
99         {
100                 DeleteCriticalSection(&wutex);
101         }
102 };
103
104 class ThreadQueueData
105 {
106         CRITICAL_SECTION mutex;
107         HANDLE event;
108  public:
109         ThreadQueueData()
110         {
111                 InitializeCriticalSection(&mutex);
112                 event = CreateEvent(NULL, false, false, NULL);
113         }
114
115         ~ThreadQueueData()
116         {
117                 DeleteCriticalSection(&mutex);
118         }
119
120         void Lock()
121         {
122                 EnterCriticalSection(&mutex);
123         }
124
125         void Unlock()
126         {
127                 LeaveCriticalSection(&mutex);
128         }
129
130         void Wakeup()
131         {
132                 PulseEvent(event);
133         }
134
135         void Wait()
136         {
137                 LeaveCriticalSection(&mutex);
138                 WaitForSingleObject(event, INFINITE);
139                 EnterCriticalSection(&mutex);
140         }
141 };
142
143 class ThreadSignalData
144 {
145  public:
146         int connFD;
147         ThreadSignalData()
148         {
149                 connFD = -1;
150         }
151 };
152
153 #endif
154