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