1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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__
15 #define __THREADENGINE_PTHREAD__
18 #include "inspircd_config.h"
24 /** The ThreadEngine class has the responsibility of initialising
25 * Thread derived classes. It does this by creating operating system
26 * level threads which are then associated with the class transparently.
27 * This allows Thread classes to be derived without needing to know how
28 * the OS implements threads. You should ensure that any sections of code
29 * that use threads are threadsafe and do not interact with any other
30 * parts of the code which are NOT known threadsafe! If you really MUST
31 * access non-threadsafe code from a Thread, use the Mutex class to wrap
32 * access to the code carefully.
34 class CoreExport ThreadEngine : public Extensible
39 * @param Instance Creator object
41 ThreadEngine(InspIRCd* Instance);
45 virtual ~ThreadEngine();
47 /** Create a new thread. This takes an already allocated
48 * Thread* pointer and initializes it to use this threading
49 * engine. On failure, this function may throw a CoreException.
50 * @param thread_to_init Pointer to a newly allocated Thread
53 void Start(Thread* thread_to_init);
55 /** Returns the thread engine's name for display purposes
56 * @return The thread engine name
58 const std::string GetName()
60 return "posix-thread";
64 class CoreExport ThreadData
68 void FreeThread(Thread* toFree);
71 /** The Mutex class represents a mutex, which can be used to keep threads
72 * properly synchronised. Use mutexes sparingly, as they are a good source
73 * of thread deadlocks etc, and should be avoided except where absolutely
74 * neccessary. Note that the internal behaviour of the mutex varies from OS
75 * to OS depending on the thread engine, for example in windows a Mutex
76 * in InspIRCd uses critical sections, as they are faster and simpler to
79 class CoreExport Mutex
82 pthread_mutex_t putex;
88 pthread_mutex_init(&putex, NULL);
90 /** Enter/enable the mutex lock.
94 pthread_mutex_lock(&putex);
96 /** Leave/disable the mutex lock.
100 pthread_mutex_unlock(&putex);
106 pthread_mutex_destroy(&putex);