annotate src/thread.h @ 9476:bba7a506af6d draft

(svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
author rubidium <rubidium@openttd.org>
date Sun, 08 Jun 2008 10:51:36 +0000
parents d48433370037
children e67ab3c32309
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5475
eabf4b86aed6 (svn r7759) -Merge: makefile rewrite. This merge features:
rubidium <rubidium@openttd.org>
parents:
diff changeset
1 /* $Id$ */
eabf4b86aed6 (svn r7759) -Merge: makefile rewrite. This merge features:
rubidium <rubidium@openttd.org>
parents:
diff changeset
2
9111
d48433370037 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium <rubidium@openttd.org>
parents: 8934
diff changeset
3 /** @file thread.h Base of all threads. */
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6248
diff changeset
4
5475
eabf4b86aed6 (svn r7759) -Merge: makefile rewrite. This merge features:
rubidium <rubidium@openttd.org>
parents:
diff changeset
5 #ifndef THREAD_H
eabf4b86aed6 (svn r7759) -Merge: makefile rewrite. This merge features:
rubidium <rubidium@openttd.org>
parents:
diff changeset
6 #define THREAD_H
eabf4b86aed6 (svn r7759) -Merge: makefile rewrite. This merge features:
rubidium <rubidium@openttd.org>
parents:
diff changeset
7
9476
bba7a506af6d (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium <rubidium@openttd.org>
parents: 9111
diff changeset
8 typedef void (*OTTDThreadFunc)(void *);
8934
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
9
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
10 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
11 * A Thread Object which works on all our supported OSes.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
12 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
13 class ThreadObject {
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
14 public:
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
15 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
16 * Virtual destructor to allow 'delete' operator to work properly.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
17 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
18 virtual ~ThreadObject() {};
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
19
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
20 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
21 * Check if the thread is currently running.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
22 * @return True if the thread is running.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
23 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
24 virtual bool IsRunning() = 0;
5475
eabf4b86aed6 (svn r7759) -Merge: makefile rewrite. This merge features:
rubidium <rubidium@openttd.org>
parents:
diff changeset
25
8934
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
26 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
27 * Waits for the thread to exit.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
28 * @return True if the thread has exited.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
29 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
30 virtual bool WaitForStop() = 0;
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
31
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
32 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
33 * Exit this thread.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
34 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
35 virtual bool Exit() = 0;
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
36
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
37 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
38 * Join this thread.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
39 */
9476
bba7a506af6d (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium <rubidium@openttd.org>
parents: 9111
diff changeset
40 virtual void Join() = 0;
8934
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
41
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
42 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
43 * Check if this thread is the current active thread.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
44 * @return True if it is the current active thread.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
45 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
46 virtual bool IsCurrent() = 0;
5475
eabf4b86aed6 (svn r7759) -Merge: makefile rewrite. This merge features:
rubidium <rubidium@openttd.org>
parents:
diff changeset
47
8934
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
48 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
49 * Get the unique ID of this thread.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
50 * @return A value unique to each thread.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
51 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
52 virtual uint GetId() = 0;
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
53
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
54 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
55 * Create a thread; proc will be called as first function inside the thread,
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
56 * with optinal params.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
57 * @param proc The procedure to call inside the thread.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
58 * @param param The params to give with 'proc'.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
59 * @return True if the thread was started correctly.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
60 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
61 static ThreadObject *New(OTTDThreadFunc proc, void *param);
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
62
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
63 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
64 * Convert the current thread to a new ThreadObject.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
65 * @return A new ThreadObject with the current thread attached to it.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
66 */
9476
bba7a506af6d (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium <rubidium@openttd.org>
parents: 9111
diff changeset
67 static ThreadObject *AttachCurrent();
8934
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
68
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
69 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
70 * Find the Id of the current running thread.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
71 * @return The thread ID of the current active thread.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
72 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
73 static uint CurrentId();
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
74 };
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
75
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
76 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
77 * Cross-platform Thread Semaphore. Wait() waits for a Set() of someone else.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
78 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
79 class ThreadSemaphore {
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
80 public:
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
81 static ThreadSemaphore *New();
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
82
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
83 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
84 * Virtual Destructor to avoid compiler warnings.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
85 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
86 virtual ~ThreadSemaphore() {};
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
87
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
88 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
89 * Signal all threads that are in Wait() to continue.
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
90 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
91 virtual void Set() = 0;
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
92
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
93 /**
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
94 * Wait until we are signaled by a call to Set().
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
95 */
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
96 virtual void Wait() = 0;
a56c5ca0f396 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents: 7318
diff changeset
97 };
5475
eabf4b86aed6 (svn r7759) -Merge: makefile rewrite. This merge features:
rubidium <rubidium@openttd.org>
parents:
diff changeset
98
eabf4b86aed6 (svn r7759) -Merge: makefile rewrite. This merge features:
rubidium <rubidium@openttd.org>
parents:
diff changeset
99 #endif /* THREAD_H */