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