]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/threadengines/threadengine_win32.h
ThreadEngine: remove MutexFactory, mutexes should be constructed using their constructor
[user/henk/code/inspircd.git] / include / threadengines / threadengine_win32.h
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 #ifndef __THREADENGINE_WIN32THREAD__
15 #define __THREADENGINE_WIN32THREAD__
16
17 #include "inspircd_config.h"
18 #include "base.h"
19
20 class InspIRCd;
21 class Thread;
22
23 /** The ThreadEngine class has the responsibility of initialising
24  * Thread derived classes. It does this by creating operating system
25  * level threads which are then associated with the class transparently.
26  * This allows Thread classes to be derived without needing to know how
27  * the OS implements threads. You should ensure that any sections of code
28  * that use threads are threadsafe and do not interact with any other
29  * parts of the code which are NOT known threadsafe! If you really MUST
30  * access non-threadsafe code from a Thread, use the Mutex class to wrap
31  * access to the code carefully.
32  */
33 class CoreExport ThreadEngine : public Extensible
34 {
35  public:
36
37         ThreadEngine(InspIRCd* Instance);
38
39         virtual ~ThreadEngine();
40
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         /** Returns the thread engine's name for display purposes
52          * @return The thread engine name
53          */
54         const std::string GetName()
55         {
56                 return "windows-thread";
57         }
58 };
59
60 class CoreExport ThreadData
61 {
62  public:
63         HANDLE handle;
64         void FreeThread(Thread* toFree);
65 };
66
67 /** The Mutex class represents a mutex, which can be used to keep threads
68  * properly synchronised. Use mutexes sparingly, as they are a good source
69  * of thread deadlocks etc, and should be avoided except where absolutely
70  * neccessary. Note that the internal behaviour of the mutex varies from OS
71  * to OS depending on the thread engine, for example in windows a Mutex
72  * in InspIRCd uses critical sections, as they are faster and simpler to
73  * manage.
74  */
75 class CoreExport Mutex
76 {
77  private:
78         CRITICAL_SECTION wutex;
79  public:
80         Win32Mutex()
81         {
82                 InitializeCriticalSection(&wutex);
83         }
84         void Lock()
85         {
86                 EnterCriticalSection(&wutex);
87         }
88         void Unlock()
89         {
90                 LeaveCriticalSection(&wutex);
91         }
92         ~Win32Mutex()
93         {
94                 DeleteCriticalSection(&wutex);
95         }
96 };
97
98 #endif
99