]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/threadengines/threadengine_pthread.h
91d0eb380a631a9f1762cdc58e5eac852dbfc8cb
[user/henk/code/inspircd.git] / include / threadengines / threadengine_pthread.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_PTHREAD__
15 #define __THREADENGINE_PTHREAD__
16
17 #include <pthread.h>
18 #include "typedefs.h"
19
20 /** The ThreadEngine class has the responsibility of initialising
21  * Thread derived classes. It does this by creating operating system
22  * level threads which are then associated with the class transparently.
23  * This allows Thread classes to be derived without needing to know how
24  * the OS implements threads. You should ensure that any sections of code
25  * that use threads are threadsafe and do not interact with any other
26  * parts of the code which are NOT known threadsafe! If you really MUST
27  * access non-threadsafe code from a Thread, use the Mutex class to wrap
28  * access to the code carefully.
29  */
30 class CoreExport ThreadEngine
31 {
32  public:
33
34         /** Constructor.
35          * @param Instance Creator object
36          */
37         ThreadEngine();
38
39         /** Destructor
40          */
41         virtual ~ThreadEngine();
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 "posix-thread";
57         }
58 };
59
60 class CoreExport ThreadData
61 {
62  public:
63         pthread_t pthread_id;
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         pthread_mutex_t putex;
79  public:
80         /** Constructor.
81          */
82         Mutex()
83         {
84                 pthread_mutex_init(&putex, NULL);
85         }
86         /** Enter/enable the mutex lock.
87          */
88         void Lock()
89         {
90                 pthread_mutex_lock(&putex);
91         }
92         /** Leave/disable the mutex lock.
93          */
94         void Unlock()
95         {
96                 pthread_mutex_unlock(&putex);
97         }
98         /** Destructor
99          */
100         ~Mutex()
101         {
102                 pthread_mutex_destroy(&putex);
103         }
104 };
105
106 class ThreadQueueData
107 {
108         pthread_mutex_t mutex;
109         pthread_cond_t cond;
110  public:
111         ThreadQueueData()
112         {
113                 pthread_mutex_init(&mutex, NULL);
114                 pthread_cond_init(&cond, NULL);
115         }
116
117         ~ThreadQueueData()
118         {
119                 pthread_mutex_destroy(&mutex);
120                 pthread_cond_destroy(&cond);
121         }
122
123         void Lock()
124         {
125                 pthread_mutex_lock(&mutex);
126         }
127
128         void Unlock()
129         {
130                 pthread_mutex_unlock(&mutex);
131         }
132
133         void Wakeup()
134         {
135                 pthread_cond_signal(&cond);
136         }
137
138         void Wait()
139         {
140                 pthread_cond_wait(&cond, &mutex);
141         }
142 };
143
144 class ThreadSignalSocket;
145 class ThreadSignalData
146 {
147  public:
148         ThreadSignalSocket* sock;
149 };
150
151
152 #endif