]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/threadengines/threadengine_pthread.h
79b00e62935428c9ee243b9b69b39b3b04981777
[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 "inspircd_config.h"
19 #include "base.h"
20
21 class InspIRCd;
22 class Thread;
23
24 /** The ThreadEngine class has the responsibility of initialising
25  * Thread derived classes. It does this by creating operating system
26  * level threads which are then associated with the class transparently.
27  * This allows Thread classes to be derived without needing to know how
28  * the OS implements threads. You should ensure that any sections of code
29  * that use threads are threadsafe and do not interact with any other
30  * parts of the code which are NOT known threadsafe! If you really MUST
31  * access non-threadsafe code from a Thread, use the Mutex class to wrap
32  * access to the code carefully.
33  */
34 class CoreExport ThreadEngine
35 {
36  public:
37
38         /** Constructor.
39          * @param Instance Creator object
40          */
41         ThreadEngine(InspIRCd* Instance);
42
43         /** Destructor
44          */
45         virtual ~ThreadEngine();
46
47         /** Create a new thread. This takes an already allocated
48          * Thread* pointer and initializes it to use this threading
49          * engine. On failure, this function may throw a CoreException.
50          * @param thread_to_init Pointer to a newly allocated Thread
51          * derived object.
52          */
53         void Start(Thread* thread_to_init);
54
55         /** Returns the thread engine's name for display purposes
56          * @return The thread engine name
57          */
58         const std::string GetName()
59         {
60                 return "posix-thread";
61         }
62 };
63
64 class CoreExport ThreadData
65 {
66  public:
67         pthread_t pthread_id;
68         void FreeThread(Thread* toFree);
69 };
70
71 /** The Mutex class represents a mutex, which can be used to keep threads
72  * properly synchronised. Use mutexes sparingly, as they are a good source
73  * of thread deadlocks etc, and should be avoided except where absolutely
74  * neccessary. Note that the internal behaviour of the mutex varies from OS
75  * to OS depending on the thread engine, for example in windows a Mutex
76  * in InspIRCd uses critical sections, as they are faster and simpler to
77  * manage.
78  */
79 class CoreExport Mutex
80 {
81  private:
82         pthread_mutex_t putex;
83  public:
84         /** Constructor.
85          */
86         Mutex()
87         {
88                 pthread_mutex_init(&putex, NULL);
89         }
90         /** Enter/enable the mutex lock.
91          */
92         void Lock()
93         {
94                 pthread_mutex_lock(&putex);
95         }
96         /** Leave/disable the mutex lock.
97          */
98         void Unlock()
99         {
100                 pthread_mutex_unlock(&putex);
101         }
102         /** Destructor
103          */
104         ~Mutex()
105         {
106                 pthread_mutex_destroy(&putex);
107         }
108 };
109
110 class ThreadQueueData
111 {
112         pthread_mutex_t mutex;
113         pthread_cond_t cond;
114  public:
115         ThreadQueueData()
116         {
117                 pthread_mutex_init(&mutex, NULL);
118                 pthread_cond_init(&cond, NULL);
119         }
120
121         ~ThreadQueueData()
122         {
123                 pthread_mutex_destroy(&mutex);
124                 pthread_cond_destroy(&cond);
125         }
126
127         void Lock()
128         {
129                 pthread_mutex_lock(&mutex);
130         }
131
132         void Unlock()
133         {
134                 pthread_mutex_unlock(&mutex);
135         }
136
137         void Wakeup()
138         {
139                 pthread_cond_signal(&cond);
140         }
141
142         void Wait()
143         {
144                 pthread_cond_wait(&cond, &mutex);
145         }
146 };
147
148 class ThreadSignalSocket;
149 class ThreadSignalData
150 {
151  public:
152         ThreadSignalSocket* sock;
153 };
154
155
156 #endif