]>
Commit | Line | Data |
---|---|---|
10b959e3 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: thread.h | |
3 | // Purpose: Thread API | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 04/13/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
c2dd8380 GL |
12 | #ifndef __THREADH__ |
13 | #define __THREADH__ | |
10b959e3 JS |
14 | |
15 | #ifdef __GNUG__ | |
16 | #pragma interface "thread.h" | |
17 | #endif | |
18 | ||
a6b0bd49 VZ |
19 | // ---------------------------------------------------------------------------- |
20 | // headers | |
21 | // ---------------------------------------------------------------------------- | |
10b959e3 JS |
22 | #include "wx/object.h" |
23 | #include "wx/setup.h" | |
24 | ||
a6b0bd49 VZ |
25 | // ---------------------------------------------------------------------------- |
26 | // constants | |
27 | // ---------------------------------------------------------------------------- | |
28 | ||
10b959e3 | 29 | typedef enum { |
a6b0bd49 VZ |
30 | wxMUTEX_NO_ERROR = 0, |
31 | wxMUTEX_DEAD_LOCK, // Mutex has been already locked by THE CALLING thread | |
32 | wxMUTEX_BUSY, // Mutex has been already locked by ONE thread | |
33 | wxMUTEX_UNLOCKED, | |
34 | wxMUTEX_MISC_ERROR | |
10b959e3 JS |
35 | } wxMutexError; |
36 | ||
37 | typedef enum { | |
a6b0bd49 VZ |
38 | wxTHREAD_NO_ERROR = 0, // No error |
39 | wxTHREAD_NO_RESOURCE, // No resource left to create a new thread | |
40 | wxTHREAD_RUNNING, // The thread is already running | |
41 | wxTHREAD_NOT_RUNNING, // The thread isn't running | |
42 | wxTHREAD_MISC_ERROR // Some other error | |
10b959e3 JS |
43 | } wxThreadError; |
44 | ||
45 | // defines the interval of priority. | |
a6b0bd49 | 46 | #define WXTHREAD_MIN_PRIORITY 0 |
10b959e3 | 47 | #define WXTHREAD_DEFAULT_PRIORITY 50 |
a6b0bd49 | 48 | #define WXTHREAD_MAX_PRIORITY 100 |
10b959e3 | 49 | |
a6b0bd49 | 50 | // ---------------------------------------------------------------------------- |
10b959e3 | 51 | // Mutex handler |
a6b0bd49 | 52 | // ---------------------------------------------------------------------------- |
c2dd8380 | 53 | class WXDLLEXPORT wxMutexInternal; |
10b959e3 JS |
54 | class WXDLLEXPORT wxMutex { |
55 | public: | |
56 | // constructor & destructor | |
ee4f8c2a JS |
57 | wxMutex(); |
58 | ~wxMutex(); | |
10b959e3 JS |
59 | |
60 | // Lock the mutex. | |
ee4f8c2a | 61 | wxMutexError Lock(); |
10b959e3 | 62 | // Try to lock the mutex: if it can't, returns immediately with an error. |
ee4f8c2a | 63 | wxMutexError TryLock(); |
10b959e3 | 64 | // Unlock the mutex. |
ee4f8c2a | 65 | wxMutexError Unlock(); |
10b959e3 JS |
66 | |
67 | // Returns true if the mutex is locked. | |
ee4f8c2a | 68 | bool IsLocked() const { return (m_locked > 0); } |
a6b0bd49 | 69 | |
10b959e3 JS |
70 | protected: |
71 | friend class wxCondition; | |
72 | ||
73 | int m_locked; | |
74 | wxMutexInternal *p_internal; | |
75 | }; | |
76 | ||
a6b0bd49 | 77 | // ---------------------------------------------------------------------------- |
10b959e3 | 78 | // Condition handler. |
a6b0bd49 | 79 | // ---------------------------------------------------------------------------- |
10b959e3 JS |
80 | class wxConditionInternal; |
81 | class WXDLLEXPORT wxCondition { | |
82 | public: | |
83 | // constructor & destructor | |
ee4f8c2a JS |
84 | wxCondition(); |
85 | ~wxCondition(); | |
10b959e3 | 86 | |
ee4f8c2a | 87 | // Waits indefinitely. |
10b959e3 JS |
88 | void Wait(wxMutex& mutex); |
89 | // Waits until a signal is raised or the timeout is elapsed. | |
90 | bool Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec); | |
91 | // Raises a signal: only one "Waiter" is released. | |
ee4f8c2a | 92 | void Signal(); |
10b959e3 | 93 | // Broadcasts to all "Waiters". |
ee4f8c2a | 94 | void Broadcast(); |
a6b0bd49 | 95 | |
10b959e3 JS |
96 | private: |
97 | wxConditionInternal *p_internal; | |
98 | }; | |
99 | ||
a6b0bd49 | 100 | // ---------------------------------------------------------------------------- |
10b959e3 | 101 | // Thread management class |
a6b0bd49 | 102 | // ---------------------------------------------------------------------------- |
10b959e3 JS |
103 | class wxThreadInternal; |
104 | class WXDLLEXPORT wxThread { | |
105 | public: | |
106 | // constructor & destructor. | |
ee4f8c2a JS |
107 | wxThread(); |
108 | virtual ~wxThread(); | |
10b959e3 JS |
109 | |
110 | // Create a new thread, this method should check there is only one thread | |
111 | // running by object. | |
ee4f8c2a | 112 | wxThreadError Create(); |
10b959e3 | 113 | |
ee4f8c2a JS |
114 | // Destroys the thread immediately if the defer flag isn't true. |
115 | wxThreadError Destroy(); | |
10b959e3 | 116 | |
c2dd8380 GL |
117 | // Pause a running thread |
118 | wxThreadError Pause(); | |
119 | ||
120 | // Resume a paused thread | |
121 | wxThreadError Resume(); | |
122 | ||
ee4f8c2a | 123 | // Switches on the defer flag. |
10b959e3 JS |
124 | void DeferDestroy(bool on); |
125 | ||
126 | // Waits for the termination of the thread. | |
ee4f8c2a | 127 | void *Join(); |
10b959e3 JS |
128 | |
129 | // Sets the priority to "prio". (Warning: The priority can only be set before | |
130 | // the thread is created) | |
131 | void SetPriority(int prio); | |
132 | // Get the current priority. | |
ee4f8c2a | 133 | int GetPriority() const; |
10b959e3 JS |
134 | |
135 | // Get the thread ID | |
ee4f8c2a | 136 | unsigned long GetID() const; |
10b959e3 JS |
137 | |
138 | // Returns true if the thread is alive. | |
ee4f8c2a | 139 | bool IsAlive() const; |
c2dd8380 GL |
140 | // Returns true if the thread is running (not paused, not killed). |
141 | bool IsRunning() const; | |
a6b0bd49 VZ |
142 | // Returns true if the thread is suspended |
143 | bool IsPaused() const { return IsAlive() && !IsRunning(); } | |
144 | ||
10b959e3 | 145 | // Returns true if the thread is the main thread (aka the GUI thread). |
ee4f8c2a | 146 | static bool IsMain(); |
10b959e3 JS |
147 | |
148 | // Called when thread exits. | |
ee4f8c2a | 149 | virtual void OnExit(); |
c2dd8380 | 150 | |
10b959e3 JS |
151 | protected: |
152 | // In case, the DIFFER flag is true, enables another thread to kill this one. | |
ee4f8c2a | 153 | void TestDestroy(); |
10b959e3 JS |
154 | // Exits from the current thread. |
155 | void Exit(void *status = NULL); | |
a6b0bd49 | 156 | |
10b959e3 JS |
157 | private: |
158 | // Entry point for the thread. | |
ee4f8c2a | 159 | virtual void *Entry() = 0; |
10b959e3 JS |
160 | |
161 | private: | |
162 | friend class wxThreadInternal; | |
163 | ||
164 | wxThreadInternal *p_internal; | |
165 | }; | |
166 | ||
a6b0bd49 VZ |
167 | // ---------------------------------------------------------------------------- |
168 | // Global functions and variables | |
169 | // ---------------------------------------------------------------------------- | |
10b959e3 | 170 | |
75ed1d15 GL |
171 | // GUI mutex handling. |
172 | void WXDLLEXPORT wxMutexGuiEnter(); | |
173 | void WXDLLEXPORT wxMutexGuiLeave(); | |
10b959e3 | 174 | |
a6b0bd49 | 175 | #endif // __THREADH__ |