]>
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 | ||
9d133d87 VZ |
19 | // ---------------------------------------------------------------------------- |
20 | // headers | |
21 | // ---------------------------------------------------------------------------- | |
10b959e3 | 22 | #include "wx/setup.h" |
9d133d87 VZ |
23 | |
24 | #if wxUSE_THREADS | |
d524867f | 25 | #include "wx/module.h" |
10b959e3 | 26 | |
a6b0bd49 VZ |
27 | // ---------------------------------------------------------------------------- |
28 | // constants | |
29 | // ---------------------------------------------------------------------------- | |
30 | ||
d524867f RR |
31 | typedef enum |
32 | { | |
a6b0bd49 VZ |
33 | wxMUTEX_NO_ERROR = 0, |
34 | wxMUTEX_DEAD_LOCK, // Mutex has been already locked by THE CALLING thread | |
35 | wxMUTEX_BUSY, // Mutex has been already locked by ONE thread | |
36 | wxMUTEX_UNLOCKED, | |
37 | wxMUTEX_MISC_ERROR | |
10b959e3 JS |
38 | } wxMutexError; |
39 | ||
d524867f RR |
40 | typedef enum |
41 | { | |
a6b0bd49 VZ |
42 | wxTHREAD_NO_ERROR = 0, // No error |
43 | wxTHREAD_NO_RESOURCE, // No resource left to create a new thread | |
44 | wxTHREAD_RUNNING, // The thread is already running | |
45 | wxTHREAD_NOT_RUNNING, // The thread isn't running | |
46 | wxTHREAD_MISC_ERROR // Some other error | |
10b959e3 JS |
47 | } wxThreadError; |
48 | ||
d524867f | 49 | /* defines the interval of priority. */ |
a6b0bd49 | 50 | #define WXTHREAD_MIN_PRIORITY 0 |
10b959e3 | 51 | #define WXTHREAD_DEFAULT_PRIORITY 50 |
a6b0bd49 | 52 | #define WXTHREAD_MAX_PRIORITY 100 |
10b959e3 | 53 | |
d524867f | 54 | // ---------------------------------------------------------------------------- |
9d133d87 VZ |
55 | // A mutex object is a synchronization object whose state is set to signaled |
56 | // when it is not owned by any thread, and nonsignaled when it is owned. Its | |
57 | // name comes from its usefulness in coordinating mutually-exclusive access to | |
58 | // a shared resource. Only one thread at a time can own a mutex object. | |
a6b0bd49 | 59 | // ---------------------------------------------------------------------------- |
d524867f | 60 | |
9d133d87 VZ |
61 | // you should consider wxMutexLocker whenever possible instead of directly |
62 | // working with wxMutex class - it is safer | |
c2dd8380 | 63 | class WXDLLEXPORT wxMutexInternal; |
d524867f RR |
64 | class WXDLLEXPORT wxMutex |
65 | { | |
10b959e3 JS |
66 | public: |
67 | // constructor & destructor | |
ee4f8c2a JS |
68 | wxMutex(); |
69 | ~wxMutex(); | |
10b959e3 JS |
70 | |
71 | // Lock the mutex. | |
ee4f8c2a | 72 | wxMutexError Lock(); |
10b959e3 | 73 | // Try to lock the mutex: if it can't, returns immediately with an error. |
ee4f8c2a | 74 | wxMutexError TryLock(); |
10b959e3 | 75 | // Unlock the mutex. |
ee4f8c2a | 76 | wxMutexError Unlock(); |
10b959e3 JS |
77 | |
78 | // Returns true if the mutex is locked. | |
ee4f8c2a | 79 | bool IsLocked() const { return (m_locked > 0); } |
a6b0bd49 | 80 | |
10b959e3 JS |
81 | protected: |
82 | friend class wxCondition; | |
83 | ||
84 | int m_locked; | |
85 | wxMutexInternal *p_internal; | |
86 | }; | |
87 | ||
9d133d87 VZ |
88 | // a helper class which locks the mutex in the ctor and unlocks it in the dtor: |
89 | // this ensures that mutex is always unlocked, even if the function returns or | |
90 | // throws an exception before it reaches the end | |
91 | class WXDLLEXPORT wxMutexLocker | |
92 | { | |
93 | public: | |
94 | // lock the mutex in the ctor | |
95 | wxMutexLocker(wxMutex *mutex) | |
96 | { m_isOk = mutex && ((m_mutex = mutex)->Lock() == wxMUTEX_NO_ERROR); } | |
97 | ||
98 | // returns TRUE if mutex was successfully locked in ctor | |
99 | bool IsOk() const { return m_isOk; } | |
100 | ||
101 | // unlock the mutex in dtor | |
102 | ~wxMutexLocker() { if ( IsOk() ) m_mutex->Unlock(); } | |
103 | ||
104 | private: | |
105 | bool m_isOk; | |
106 | wxMutex *m_mutex; | |
107 | }; | |
108 | ||
109 | // ---------------------------------------------------------------------------- | |
110 | // Critical section: this is the same as mutex but is only visible to the | |
111 | // threads of the same process. For the platforms which don't have native | |
112 | // support for critical sections, they're implemented entirely in terms of | |
113 | // mutexes | |
114 | // ---------------------------------------------------------------------------- | |
115 | ||
116 | // you should consider wxCriticalSectionLocker whenever possible instead of | |
117 | // directly working with wxCriticalSection class - it is safer | |
118 | class WXDLLEXPORT wxCriticalSectionInternal; | |
119 | class WXDLLEXPORT wxCriticalSection | |
120 | { | |
121 | public: | |
122 | // ctor & dtor | |
123 | wxCriticalSection(); | |
124 | ~wxCriticalSection(); | |
125 | ||
126 | // enter the section (the same as locking a mutex) | |
127 | void Enter(); | |
128 | // leave the critical section (same as unlocking a mutex) | |
129 | void Leave(); | |
130 | ||
131 | private: | |
132 | wxCriticalSectionInternal *m_critsect; | |
133 | }; | |
134 | ||
135 | // wxCriticalSectionLocker is the same to critical sections as wxMutexLocker is | |
136 | // to th mutexes | |
137 | class WXDLLEXPORT wxCriticalSectionLocker | |
138 | { | |
139 | public: | |
140 | wxCriticalSectionLocker(wxCriticalSection *critsect) | |
141 | { (m_critsect = critsect)->Enter(); } | |
142 | ~wxCriticalSectionLocker() | |
143 | { m_critsect->Leave(); } | |
144 | ||
145 | private: | |
146 | wxCriticalSection *m_critsect; | |
147 | }; | |
148 | ||
a6b0bd49 | 149 | // ---------------------------------------------------------------------------- |
10b959e3 | 150 | // Condition handler. |
a6b0bd49 | 151 | // ---------------------------------------------------------------------------- |
d524867f | 152 | |
10b959e3 | 153 | class wxConditionInternal; |
d524867f RR |
154 | class WXDLLEXPORT wxCondition |
155 | { | |
10b959e3 JS |
156 | public: |
157 | // constructor & destructor | |
ee4f8c2a JS |
158 | wxCondition(); |
159 | ~wxCondition(); | |
10b959e3 | 160 | |
ee4f8c2a | 161 | // Waits indefinitely. |
10b959e3 JS |
162 | void Wait(wxMutex& mutex); |
163 | // Waits until a signal is raised or the timeout is elapsed. | |
164 | bool Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec); | |
165 | // Raises a signal: only one "Waiter" is released. | |
ee4f8c2a | 166 | void Signal(); |
10b959e3 | 167 | // Broadcasts to all "Waiters". |
ee4f8c2a | 168 | void Broadcast(); |
a6b0bd49 | 169 | |
10b959e3 JS |
170 | private: |
171 | wxConditionInternal *p_internal; | |
172 | }; | |
173 | ||
a6b0bd49 | 174 | // ---------------------------------------------------------------------------- |
10b959e3 | 175 | // Thread management class |
a6b0bd49 | 176 | // ---------------------------------------------------------------------------- |
d524867f | 177 | |
10b959e3 | 178 | class wxThreadInternal; |
d524867f RR |
179 | class WXDLLEXPORT wxThread |
180 | { | |
10b959e3 JS |
181 | public: |
182 | // constructor & destructor. | |
ee4f8c2a JS |
183 | wxThread(); |
184 | virtual ~wxThread(); | |
10b959e3 JS |
185 | |
186 | // Create a new thread, this method should check there is only one thread | |
187 | // running by object. | |
ee4f8c2a | 188 | wxThreadError Create(); |
10b959e3 | 189 | |
ee4f8c2a JS |
190 | // Destroys the thread immediately if the defer flag isn't true. |
191 | wxThreadError Destroy(); | |
10b959e3 | 192 | |
c2dd8380 GL |
193 | // Pause a running thread |
194 | wxThreadError Pause(); | |
195 | ||
196 | // Resume a paused thread | |
197 | wxThreadError Resume(); | |
198 | ||
ee4f8c2a | 199 | // Switches on the defer flag. |
10b959e3 JS |
200 | void DeferDestroy(bool on); |
201 | ||
202 | // Waits for the termination of the thread. | |
ee4f8c2a | 203 | void *Join(); |
10b959e3 JS |
204 | |
205 | // Sets the priority to "prio". (Warning: The priority can only be set before | |
206 | // the thread is created) | |
207 | void SetPriority(int prio); | |
208 | // Get the current priority. | |
ee4f8c2a | 209 | int GetPriority() const; |
10b959e3 JS |
210 | |
211 | // Get the thread ID | |
ee4f8c2a | 212 | unsigned long GetID() const; |
10b959e3 JS |
213 | |
214 | // Returns true if the thread is alive. | |
ee4f8c2a | 215 | bool IsAlive() const; |
c2dd8380 GL |
216 | // Returns true if the thread is running (not paused, not killed). |
217 | bool IsRunning() const; | |
a6b0bd49 VZ |
218 | // Returns true if the thread is suspended |
219 | bool IsPaused() const { return IsAlive() && !IsRunning(); } | |
220 | ||
9d133d87 | 221 | // Returns true if current thread is the main thread (aka the GUI thread) |
ee4f8c2a | 222 | static bool IsMain(); |
10b959e3 JS |
223 | |
224 | // Called when thread exits. | |
ee4f8c2a | 225 | virtual void OnExit(); |
c2dd8380 | 226 | |
10b959e3 | 227 | protected: |
9d133d87 VZ |
228 | // Returns TRUE if the thread was asked to terminate |
229 | bool TestDestroy(); | |
230 | ||
10b959e3 JS |
231 | // Exits from the current thread. |
232 | void Exit(void *status = NULL); | |
a6b0bd49 | 233 | |
10b959e3 JS |
234 | private: |
235 | // Entry point for the thread. | |
ee4f8c2a | 236 | virtual void *Entry() = 0; |
10b959e3 JS |
237 | |
238 | private: | |
239 | friend class wxThreadInternal; | |
240 | ||
241 | wxThreadInternal *p_internal; | |
242 | }; | |
243 | ||
a6b0bd49 | 244 | // ---------------------------------------------------------------------------- |
d524867f | 245 | // Automatic initialization |
a6b0bd49 | 246 | // ---------------------------------------------------------------------------- |
10b959e3 | 247 | |
9d133d87 VZ |
248 | // GUI mutex handling. |
249 | void WXDLLEXPORT wxMutexGuiEnter(); | |
250 | void WXDLLEXPORT wxMutexGuiLeave(); | |
d524867f | 251 | |
9d133d87 | 252 | #else // !wxUSE_THREADS |
d524867f | 253 | |
9d133d87 VZ |
254 | // no thread support |
255 | inline void WXDLLEXPORT wxMutexGuiEnter() { } | |
256 | inline void WXDLLEXPORT wxMutexGuiLeave() { } | |
d524867f | 257 | |
9d133d87 | 258 | #endif // wxUSE_THREADS |
10b959e3 | 259 | |
a6b0bd49 | 260 | #endif // __THREADH__ |