summaryrefslogtreecommitdiff
path: root/src/thread.h
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-04-14 19:54:33 +0000
committerrubidium <rubidium@openttd.org>2008-04-14 19:54:33 +0000
commit12188e7a5dde4cdd4304b3e738b7905e55f3ad9a (patch)
treea38226d767be0babf008f3304e7a61a4a00af2aa /src/thread.h
parenta8008db23daf504af9f313ff0c53c5b89e756e05 (diff)
downloadopenttd-12188e7a5dde4cdd4304b3e738b7905e55f3ad9a.tar.xz
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
Diffstat (limited to 'src/thread.h')
-rw-r--r--src/thread.h93
1 files changed, 88 insertions, 5 deletions
diff --git a/src/thread.h b/src/thread.h
index 5ec169b0c..2b12b4582 100644
--- a/src/thread.h
+++ b/src/thread.h
@@ -5,12 +5,95 @@
#ifndef THREAD_H
#define THREAD_H
-struct OTTDThread;
+typedef void * (CDECL *OTTDThreadFunc)(void *);
-typedef void * (*OTTDThreadFunc)(void*);
+/**
+ * A Thread Object which works on all our supported OSes.
+ */
+class ThreadObject {
+public:
+ /**
+ * Virtual destructor to allow 'delete' operator to work properly.
+ */
+ virtual ~ThreadObject() {};
-OTTDThread *OTTDCreateThread(OTTDThreadFunc, void*);
-void *OTTDJoinThread(OTTDThread*);
-void OTTDExitThread();
+ /**
+ * Check if the thread is currently running.
+ * @return True if the thread is running.
+ */
+ virtual bool IsRunning() = 0;
+
+ /**
+ * Waits for the thread to exit.
+ * @return True if the thread has exited.
+ */
+ virtual bool WaitForStop() = 0;
+
+ /**
+ * Exit this thread.
+ */
+ virtual bool Exit() = 0;
+
+ /**
+ * Join this thread.
+ */
+ virtual void *Join() = 0;
+
+ /**
+ * Check if this thread is the current active thread.
+ * @return True if it is the current active thread.
+ */
+ virtual bool IsCurrent() = 0;
+
+ /**
+ * Get the unique ID of this thread.
+ * @return A value unique to each thread.
+ */
+ virtual uint GetId() = 0;
+
+ /**
+ * Create a thread; proc will be called as first function inside the thread,
+ * with optinal params.
+ * @param proc The procedure to call inside the thread.
+ * @param param The params to give with 'proc'.
+ * @return True if the thread was started correctly.
+ */
+ static ThreadObject *New(OTTDThreadFunc proc, void *param);
+
+ /**
+ * Convert the current thread to a new ThreadObject.
+ * @return A new ThreadObject with the current thread attached to it.
+ */
+ static ThreadObject* AttachCurrent();
+
+ /**
+ * Find the Id of the current running thread.
+ * @return The thread ID of the current active thread.
+ */
+ static uint CurrentId();
+};
+
+/**
+ * Cross-platform Thread Semaphore. Wait() waits for a Set() of someone else.
+ */
+class ThreadSemaphore {
+public:
+ static ThreadSemaphore *New();
+
+ /**
+ * Virtual Destructor to avoid compiler warnings.
+ */
+ virtual ~ThreadSemaphore() {};
+
+ /**
+ * Signal all threads that are in Wait() to continue.
+ */
+ virtual void Set() = 0;
+
+ /**
+ * Wait until we are signaled by a call to Set().
+ */
+ virtual void Wait() = 0;
+};
#endif /* THREAD_H */