src/thread.h
author richk
Tue, 17 Jun 2008 10:32:49 +0000
branchNewGRF_ports
changeset 10991 d8811e327d12
parent 10724 68a692eacf22
child 10861 7954a0043e05
permissions -rw-r--r--
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
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
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10211
diff changeset
     3
/** @file thread.h Base of all threads. */
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6574
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
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10724
diff changeset
     8
typedef void (*OTTDThreadFunc)(void *);
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     9
10211
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    10
/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    11
 * A Thread Object which works on all our supported OSes.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    12
 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    13
class ThreadObject {
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    14
public:
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    15
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    16
	 * Virtual destructor to allow 'delete' operator to work properly.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    17
	 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    18
	virtual ~ThreadObject() {};
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    19
10211
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    20
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    21
	 * Check if the thread is currently running.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    22
	 * @return True if the thread is running.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    23
	 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    24
	virtual bool IsRunning() = 0;
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    25
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    26
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    27
	 * Waits for the thread to exit.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    28
	 * @return True if the thread has exited.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    29
	 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    30
	virtual bool WaitForStop() = 0;
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    31
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    32
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    33
	 * Exit this thread.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    34
	 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    35
	virtual bool Exit() = 0;
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    36
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    37
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    38
	 * Join this thread.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    39
	 */
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10724
diff changeset
    40
	virtual void Join() = 0;
10211
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    41
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    42
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    43
	 * Check if this thread is the current active thread.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    44
	 * @return True if it is the current active thread.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    45
	 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    46
	virtual bool IsCurrent() = 0;
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    47
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    48
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    49
	 * Get the unique ID of this thread.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    50
	 * @return A value unique to each thread.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    51
	 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    52
	virtual uint GetId() = 0;
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    53
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    54
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    55
	 * Create a thread; proc will be called as first function inside the thread,
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    56
	 *  with optinal params.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    57
	 * @param proc The procedure to call inside the thread.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    58
	 * @param param The params to give with 'proc'.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    59
	 * @return True if the thread was started correctly.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    60
	 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    61
	static ThreadObject *New(OTTDThreadFunc proc, void *param);
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    62
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    63
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    64
	 * Convert the current thread to a new ThreadObject.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    65
	 * @return A new ThreadObject with the current thread attached to it.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    66
	 */
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10724
diff changeset
    67
	static ThreadObject *AttachCurrent();
10211
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    68
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    69
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    70
	 * Find the Id of the current running thread.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    71
	 * @return The thread ID of the current active thread.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    72
	 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    73
	static uint CurrentId();
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    74
};
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    75
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    76
/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    77
 * Cross-platform Thread Semaphore. Wait() waits for a Set() of someone else.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    78
 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    79
class ThreadSemaphore {
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    80
public:
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    81
	static ThreadSemaphore *New();
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    82
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    83
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    84
	 * Virtual Destructor to avoid compiler warnings.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    85
	 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    86
	virtual ~ThreadSemaphore() {};
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    87
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    88
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    89
	 * Signal all threads that are in Wait() to continue.
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    90
	 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    91
	virtual void Set() = 0;
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    92
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    93
	/**
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    94
	 * Wait until we are signaled by a call to Set().
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    95
	 */
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    96
	virtual void Wait() = 0;
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 6743
diff changeset
    97
};
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    98
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
    99
#endif /* THREAD_H */