tron@2285: /* $Id$ */ tron@2285: rubidium@10455: /** @file thread.h Base of all threads. */ glx@9574: tron@2285: #ifndef THREAD_H tron@2285: #define THREAD_H tron@2285: rubidium@10867: typedef void (*OTTDThreadFunc)(void *); tron@2285: KUDr@9514: /** truebrain@9856: * A Thread Object which works on all our supported OSes. KUDr@9514: */ truebrain@9856: class ThreadObject { truebrain@9856: public: truebrain@9856: /** truebrain@9856: * Virtual destructor to allow 'delete' operator to work properly. truebrain@9856: */ KUDr@9514: virtual ~ThreadObject() {}; truebrain@9856: truebrain@9856: /** truebrain@9856: * Check if the thread is currently running. truebrain@9856: * @return True if the thread is running. truebrain@9856: */ KUDr@9514: virtual bool IsRunning() = 0; truebrain@9856: truebrain@9856: /** truebrain@9856: * Waits for the thread to exit. truebrain@9856: * @return True if the thread has exited. truebrain@9856: */ KUDr@9514: virtual bool WaitForStop() = 0; truebrain@9856: truebrain@9856: /** truebrain@9859: * Exit this thread. truebrain@9859: */ truebrain@9859: virtual bool Exit() = 0; truebrain@9859: truebrain@9859: /** truebrain@9859: * Join this thread. truebrain@9859: */ rubidium@10867: virtual void Join() = 0; truebrain@9859: truebrain@9859: /** truebrain@9856: * Check if this thread is the current active thread. truebrain@9856: * @return True if it is the current active thread. truebrain@9856: */ KUDr@9514: virtual bool IsCurrent() = 0; truebrain@9856: truebrain@9856: /** truebrain@9856: * Get the unique ID of this thread. truebrain@9856: * @return A value unique to each thread. truebrain@9856: */ truebrain@9856: virtual uint GetId() = 0; truebrain@9856: truebrain@9856: /** truebrain@9856: * Create a thread; proc will be called as first function inside the thread, truebrain@9856: * with optinal params. truebrain@9856: * @param proc The procedure to call inside the thread. truebrain@9856: * @param param The params to give with 'proc'. truebrain@9856: * @return True if the thread was started correctly. truebrain@9856: */ truebrain@9856: static ThreadObject *New(OTTDThreadFunc proc, void *param); truebrain@9856: truebrain@9856: /** truebrain@9856: * Convert the current thread to a new ThreadObject. truebrain@9856: * @return A new ThreadObject with the current thread attached to it. truebrain@9856: */ rubidium@10867: static ThreadObject *AttachCurrent(); truebrain@9856: truebrain@9856: /** truebrain@9856: * Find the Id of the current running thread. truebrain@9856: * @return The thread ID of the current active thread. truebrain@9856: */ KUDr@9514: static uint CurrentId(); KUDr@9514: }; KUDr@9514: KUDr@9514: /** truebrain@9856: * Cross-platform Thread Semaphore. Wait() waits for a Set() of someone else. KUDr@9514: */ truebrain@9856: class ThreadSemaphore { truebrain@9856: public: truebrain@9856: static ThreadSemaphore *New(); truebrain@9856: truebrain@9856: /** truebrain@9856: * Virtual Destructor to avoid compiler warnings. truebrain@9856: */ truebrain@9856: virtual ~ThreadSemaphore() {}; truebrain@9856: truebrain@9856: /** truebrain@9856: * Signal all threads that are in Wait() to continue. truebrain@9856: */ KUDr@9514: virtual void Set() = 0; truebrain@9856: truebrain@9856: /** truebrain@9856: * Wait until we are signaled by a call to Set(). truebrain@9856: */ KUDr@9514: virtual void Wait() = 0; KUDr@9514: }; KUDr@9514: Darkvater@2380: #endif /* THREAD_H */