1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef THREADENGINE_PTHREAD_H
15 #define THREADENGINE_PTHREAD_H
20 /** The ThreadEngine class has the responsibility of initialising
21 * Thread derived classes. It does this by creating operating system
22 * level threads which are then associated with the class transparently.
23 * This allows Thread classes to be derived without needing to know how
24 * the OS implements threads. You should ensure that any sections of code
25 * that use threads are threadsafe and do not interact with any other
26 * parts of the code which are NOT known threadsafe! If you really MUST
27 * access non-threadsafe code from a Thread, use the Mutex class to wrap
28 * access to the code carefully.
30 class CoreExport ThreadEngine
35 * @param Instance Creator object
41 virtual ~ThreadEngine();
43 /** Create a new thread. This takes an already allocated
44 * Thread* pointer and initializes it to use this threading
45 * engine. On failure, this function may throw a CoreException.
46 * @param thread_to_init Pointer to a newly allocated Thread
49 void Start(Thread* thread_to_init);
51 /** Returns the thread engine's name for display purposes
52 * @return The thread engine name
54 const std::string GetName()
56 return "posix-thread";
60 class CoreExport ThreadData
64 void FreeThread(Thread* toFree);
67 /** The Mutex class represents a mutex, which can be used to keep threads
68 * properly synchronised. Use mutexes sparingly, as they are a good source
69 * of thread deadlocks etc, and should be avoided except where absolutely
70 * neccessary. Note that the internal behaviour of the mutex varies from OS
71 * to OS depending on the thread engine, for example in windows a Mutex
72 * in InspIRCd uses critical sections, as they are faster and simpler to
75 class CoreExport Mutex
78 pthread_mutex_t putex;
84 pthread_mutex_init(&putex, NULL);
86 /** Enter/enable the mutex lock.
90 pthread_mutex_lock(&putex);
92 /** Leave/disable the mutex lock.
96 pthread_mutex_unlock(&putex);
102 pthread_mutex_destroy(&putex);
106 class ThreadQueueData
108 pthread_mutex_t mutex;
113 pthread_mutex_init(&mutex, NULL);
114 pthread_cond_init(&cond, NULL);
119 pthread_mutex_destroy(&mutex);
120 pthread_cond_destroy(&cond);
125 pthread_mutex_lock(&mutex);
130 pthread_mutex_unlock(&mutex);
135 pthread_cond_signal(&cond);
140 pthread_cond_wait(&cond, &mutex);
144 class ThreadSignalSocket;
145 class ThreadSignalData
148 ThreadSignalSocket* sock;