summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAdam <Adam@drink-coca-cola.info>2010-05-05 18:14:06 -0400
committerAdam <Adam@anope.org>2010-06-18 20:58:54 -0400
commit031bc4a8b0dc456aca4d70dc260f626b64cd82b3 (patch)
tree74f9c217963e45f54c787ed83fc6d494232f5cd4 /include
parent503958aa77382a85a041e195d04b7a2ec51589e3 (diff)
Merged branch threadingengine with master - Added a threading engine
Diffstat (limited to 'include')
-rw-r--r--include/services.h1
-rw-r--r--include/threadengine.h116
2 files changed, 117 insertions, 0 deletions
diff --git a/include/services.h b/include/services.h
index 4dd35a751..43e91a21b 100644
--- a/include/services.h
+++ b/include/services.h
@@ -358,6 +358,7 @@ typedef struct exception_ Exception;
typedef struct session_ Session;
#include "extensible.h"
+#include "threadengine.h"
#include "bots.h"
#include "opertype.h"
#include "modes.h"
diff --git a/include/threadengine.h b/include/threadengine.h
new file mode 100644
index 000000000..e2125c6bd
--- /dev/null
+++ b/include/threadengine.h
@@ -0,0 +1,116 @@
+#ifdef _WIN32
+typedef HANDLE ThreadHandle;
+typedef CRITICAL_SECTION MutexHandle;
+typedef HANDLE CondHandle;
+#else
+# include <pthread.h>
+typedef pthread_t ThreadHandle;
+typedef pthread_mutex_t MutexHandle;
+typedef pthread_cond_t CondHandle;
+#endif
+
+class ThreadEngine;
+class Thread;
+
+extern CoreExport ThreadEngine threadEngine;
+
+class ThreadEngine
+{
+ public:
+ /** Threadengines constructor
+ */
+ ThreadEngine();
+
+ /** Threadengines destructor
+ */
+ ~ThreadEngine();
+
+ /** Start a new thread
+ * @param thread A pointer to a newley allocated thread
+ */
+ void Start(Thread *thread);
+};
+
+class Thread : public Extensible
+{
+ private:
+ /* Set to true to tell the thread to finish and we are waiting for it */
+ bool Exit;
+
+ /** Join to the thread, sets the exit state to true
+ */
+ void Join();
+ public:
+ /* Handle for this thread */
+ ThreadHandle Handle;
+
+ /** Threads constructor
+ */
+ Thread();
+
+ /** Threads destructor
+ */
+ virtual ~Thread();
+
+ /** Sets the exit state as true informing the thread we want it to shut down
+ */
+ void SetExitState();
+
+ /** Returns the exit state of the thread
+ * @return true if we want to exit
+ */
+ bool GetExitState() const;
+
+ /** Called to run the thread, should be overloaded
+ */
+ virtual void Run();
+};
+
+class Mutex
+{
+ protected:
+ /* A mutex, used to keep threads in sync */
+ MutexHandle mutex;
+
+ public:
+ /** Constructor
+ */
+ Mutex();
+
+ /** Destructor
+ */
+ ~Mutex();
+
+ /** Attempt to lock the mutex, will hang until a lock can be achieved
+ */
+ void Lock();
+
+ /** Unlock the mutex, it must be locked first
+ */
+ void Unlock();
+};
+
+class Condition : public Mutex
+{
+ private:
+ /* A condition */
+ CondHandle cond;
+
+ public:
+ /** Constructor
+ */
+ Condition();
+
+ /** Destructor
+ */
+ ~Condition();
+
+ /** Called to wakeup the waiter
+ */
+ void Wakeup();
+
+ /** Called to wait for a Wakeup() call
+ */
+ void Wait();
+};
+