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