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