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