2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
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.
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
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/>.
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.
36 class CoreExport ThreadEngine
46 virtual ~ThreadEngine();
48 /** Create a new thread. This takes an already allocated
49 * Thread* pointer and initializes it to use this threading
50 * engine. On failure, this function may throw a CoreException.
51 * @param thread_to_init Pointer to a newly allocated Thread
54 void Start(Thread* thread_to_init);
56 /** Returns the thread engine's name for display purposes
57 * @return The thread engine name
59 const std::string GetName()
61 return "posix-thread";
65 class CoreExport ThreadData
69 void FreeThread(Thread* toFree);
72 /** The Mutex class represents a mutex, which can be used to keep threads
73 * properly synchronised. Use mutexes sparingly, as they are a good source
74 * of thread deadlocks etc, and should be avoided except where absolutely
75 * neccessary. Note that the internal behaviour of the mutex varies from OS
76 * to OS depending on the thread engine, for example in windows a Mutex
77 * in InspIRCd uses critical sections, as they are faster and simpler to
80 class CoreExport Mutex
83 pthread_mutex_t putex;
89 pthread_mutex_init(&putex, NULL);
91 /** Enter/enable the mutex lock.
95 pthread_mutex_lock(&putex);
97 /** Leave/disable the mutex lock.
101 pthread_mutex_unlock(&putex);
107 pthread_mutex_destroy(&putex);
111 class ThreadQueueData
113 pthread_mutex_t mutex;
118 pthread_mutex_init(&mutex, NULL);
119 pthread_cond_init(&cond, NULL);
124 pthread_mutex_destroy(&mutex);
125 pthread_cond_destroy(&cond);
130 pthread_mutex_lock(&mutex);
135 pthread_mutex_unlock(&mutex);
140 pthread_cond_signal(&cond);
145 pthread_cond_wait(&cond, &mutex);
149 class ThreadSignalSocket;
150 class ThreadSignalData
153 ThreadSignalSocket* sock;