src/thread.h
author truebrain
Wed, 02 Apr 2008 10:55:28 +0000
branchnoai
changeset 9859 81621c6ba0e9
parent 9856 6a0dcee9f8e4
child 10172 f93d3b7df6c8
permissions -rw-r--r--
(svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
[NoAI] -Codechange: rewritten ai_threads.cpp, to work with Fiber class. Reduces code duplication, and should fix all the stupid asserts we had with newgames and dying AIs
-NOTE: now my head spins.. 24 hours of working with threads/fibers is bad for health :p Tnx glx for the testing!
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     1
/* $Id$ */
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     2
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9514
diff changeset
     3
/** @file thread.h */
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9514
diff changeset
     4
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     5
#ifndef THREAD_H
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     6
#define THREAD_H
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     7
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
     8
struct OTTDThread;
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     9
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    10
typedef void * (CDECL *OTTDThreadFunc)(void *);
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    11
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    12
OTTDThread *OTTDCreateThread(OTTDThreadFunc, void *);
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    13
void       *OTTDJoinThread(OTTDThread *);
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5726
diff changeset
    14
void        OTTDExitThread();
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    15
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    16
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    17
/**
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    18
 * A Thread Object which works on all our supported OSes.
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    19
 */
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    20
class ThreadObject {
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    21
public:
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    22
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    23
	 * Virtual destructor to allow 'delete' operator to work properly.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    24
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    25
	virtual ~ThreadObject() {};
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    26
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    27
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    28
	 * Check if the thread is currently running.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    29
	 * @return True if the thread is running.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    30
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    31
	virtual bool IsRunning() = 0;
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    32
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    33
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    34
	 * Waits for the thread to exit.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    35
	 * @return True if the thread has exited.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    36
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    37
	virtual bool WaitForStop() = 0;
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    38
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    39
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    40
	 * Exit this thread.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    41
	 */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    42
	virtual bool Exit() = 0;
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    43
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    44
	/**
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    45
	 * Join this thread.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    46
	 */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    47
	virtual void Join() = 0;
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    48
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    49
	/**
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    50
	 * Check if this thread is the current active thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    51
	 * @return True if it is the current active thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    52
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    53
	virtual bool IsCurrent() = 0;
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    54
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    55
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    56
	 * Get the unique ID of this thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    57
	 * @return A value unique to each thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    58
	 */
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    59
	virtual uint GetId() = 0;
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    60
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    61
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    62
	 * Create a thread; proc will be called as first function inside the thread,
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    63
	 *  with optinal params.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    64
	 * @param proc The procedure to call inside the thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    65
	 * @param param The params to give with 'proc'.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    66
	 * @return True if the thread was started correctly.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    67
	 */
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    68
	static ThreadObject *New(OTTDThreadFunc proc, void *param);
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    69
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    70
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    71
	 * Convert the current thread to a new ThreadObject.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    72
	 * @return A new ThreadObject with the current thread attached to it.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    73
	 */
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    74
	static ThreadObject* AttachCurrent();
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    75
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    76
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    77
	 * Find the Id of the current running thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    78
	 * @return The thread ID of the current active thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    79
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    80
	static uint CurrentId();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    81
};
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    82
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    83
/**
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    84
 * Cross-platform Thread Semaphore. Wait() waits for a Set() of someone else.
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    85
 */
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    86
class ThreadSemaphore {
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    87
public:
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    88
	static ThreadSemaphore *New();
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    89
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    90
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    91
	 * Virtual Destructor to avoid compiler warnings.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    92
	 */
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    93
	virtual ~ThreadSemaphore() {};
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    94
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    95
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    96
	 * Signal all threads that are in Wait() to continue.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    97
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    98
	virtual void Set() = 0;
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    99
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
   100
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
   101
	 * Wait until we are signaled by a call to Set().
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
   102
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
   103
	virtual void Wait() = 0;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
   104
};
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
   105
2380
3b26659b4a9a (svn r2906) Fix some threaded saving problems. Now the thread only interfaces with the main program through a sort of mutex. Communication uses the function OTTD_SendThreadMessage() with the approiate message which is handled in ProcessSentMessage() during the main loop.
Darkvater
parents: 2286
diff changeset
   106
#endif /* THREAD_H */