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