2005-08-05 09:15:41 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file thread.h Base of all threads. */
|
2007-04-04 03:21:14 +00:00
|
|
|
|
2005-08-05 09:15:41 +00:00
|
|
|
#ifndef THREAD_H
|
|
|
|
#define THREAD_H
|
|
|
|
|
2008-06-08 10:51:36 +00:00
|
|
|
typedef void (*OTTDThreadFunc)(void *);
|
2008-06-08 12:06:27 +00:00
|
|
|
typedef void (*OTTDThreadTerminateFunc)(class ThreadObject *self);
|
2005-08-05 09:15:41 +00:00
|
|
|
|
2008-04-14 19:54:33 +00:00
|
|
|
/**
|
|
|
|
* A Thread Object which works on all our supported OSes.
|
|
|
|
*/
|
|
|
|
class ThreadObject {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Virtual destructor to allow 'delete' operator to work properly.
|
|
|
|
*/
|
|
|
|
virtual ~ThreadObject() {};
|
2005-08-05 09:15:41 +00:00
|
|
|
|
2008-04-14 19:54:33 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2008-06-08 10:51:36 +00:00
|
|
|
virtual void Join() = 0;
|
2008-04-14 19:54:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'.
|
2008-06-08 12:06:27 +00:00
|
|
|
* @param terminate_func The function (or NULL) to call when the thread terminates.
|
2008-04-14 19:54:33 +00:00
|
|
|
* @return True if the thread was started correctly.
|
|
|
|
*/
|
2008-06-08 12:06:27 +00:00
|
|
|
static ThreadObject *New(OTTDThreadFunc proc, void *param, OTTDThreadTerminateFunc terminate_func);
|
2008-04-14 19:54:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the current thread to a new ThreadObject.
|
|
|
|
* @return A new ThreadObject with the current thread attached to it.
|
|
|
|
*/
|
2008-06-08 10:51:36 +00:00
|
|
|
static ThreadObject *AttachCurrent();
|
2008-04-14 19:54:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the Id of the current running thread.
|
|
|
|
* @return The thread ID of the current active thread.
|
|
|
|
*/
|
|
|
|
static uint CurrentId();
|
2008-06-08 12:06:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A OTTDThreadTerminateFunc, which cleans up the thread itself
|
|
|
|
* at termination of the thread (so it becomes self-managed).
|
|
|
|
*/
|
|
|
|
static void TerminateCleanup(ThreadObject *self) { delete self; }
|
2008-04-14 19:54:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
};
|
2005-08-05 09:15:41 +00:00
|
|
|
|
2005-09-02 16:05:59 +00:00
|
|
|
#endif /* THREAD_H */
|