]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/threadengines/threadengine_pthread.h
Fixes by misspell-fixer
[user/henk/code/inspircd.git] / include / threadengines / threadengine_pthread.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2013 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #pragma once
25
26 #include <pthread.h>
27 #include "typedefs.h"
28
29 /** The ThreadEngine class has the responsibility of initialising
30  * Thread derived classes. It does this by creating operating system
31  * level threads which are then associated with the class transparently.
32  * This allows Thread classes to be derived without needing to know how
33  * the OS implements threads. You should ensure that any sections of code
34  * that use threads are threadsafe and do not interact with any other
35  * parts of the code which are NOT known threadsafe! If you really MUST
36  * access non-threadsafe code from a Thread, use the Mutex class to wrap
37  * access to the code carefully.
38  */
39 class CoreExport ThreadEngine
40 {
41  public:
42         /** Per-thread state, present in each Thread object, managed by the ThreadEngine
43          */
44         struct ThreadState
45         {
46                 pthread_t pthread_id;
47         };
48
49         /** Create a new thread. This takes an already allocated
50          * Thread* pointer and initializes it to use this threading
51          * engine. On failure, this function may throw a CoreException.
52          * @param thread_to_init Pointer to a newly allocated Thread
53          * derived object.
54          */
55         void Start(Thread* thread_to_init);
56
57         /** Stop a thread gracefully.
58          * First, this function asks the thread to terminate by calling Thread::SetExitFlag().
59          * Next, it waits until the thread terminates (on the operating system level). Finally,
60          * all OS-level resources associated with the thread are released. The Thread instance
61          * passed to the function is NOT freed.
62          * When this function returns, the thread is stopped and you can destroy it or restart it
63          * at a later point.
64          * Stopping a thread that is not running is a bug.
65          * @param thread The thread to stop.
66          */
67         void Stop(Thread* thread);
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  * necessary. 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  protected:
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 : public Mutex
110 {
111         pthread_cond_t cond;
112  public:
113         ThreadQueueData()
114         {
115                 pthread_cond_init(&cond, NULL);
116         }
117
118         ~ThreadQueueData()
119         {
120                 pthread_cond_destroy(&cond);
121         }
122
123         void Wakeup()
124         {
125                 pthread_cond_signal(&cond);
126         }
127
128         void Wait()
129         {
130                 pthread_cond_wait(&cond, &putex);
131         }
132 };
133
134 class ThreadSignalSocket;
135 class ThreadSignalData
136 {
137  public:
138         ThreadSignalSocket* sock;
139 };