1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "thread.h"
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
25 #include "wx/module.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
34 wxMUTEX_DEAD_LOCK
, // Mutex has been already locked by THE CALLING thread
35 wxMUTEX_BUSY
, // Mutex has been already locked by ONE thread
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
49 /* defines the interval of priority. */
50 #define WXTHREAD_MIN_PRIORITY 0
51 #define WXTHREAD_DEFAULT_PRIORITY 50
52 #define WXTHREAD_MAX_PRIORITY 100
54 // ----------------------------------------------------------------------------
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.
59 // ----------------------------------------------------------------------------
61 // you should consider wxMutexLocker whenever possible instead of directly
62 // working with wxMutex class - it is safer
63 class WXDLLEXPORT wxMutexInternal
;
64 class WXDLLEXPORT wxMutex
67 // constructor & destructor
73 // Try to lock the mutex: if it can't, returns immediately with an error.
74 wxMutexError
TryLock();
76 wxMutexError
Unlock();
78 // Returns true if the mutex is locked.
79 bool IsLocked() const { return (m_locked
> 0); }
82 friend class wxCondition
;
84 // no assignment operator nor copy ctor
85 wxMutex(const wxMutex
&);
86 wxMutex
& operator=(const wxMutex
&);
89 wxMutexInternal
*p_internal
;
92 // a helper class which locks the mutex in the ctor and unlocks it in the dtor:
93 // this ensures that mutex is always unlocked, even if the function returns or
94 // throws an exception before it reaches the end
95 class WXDLLEXPORT wxMutexLocker
98 // lock the mutex in the ctor
99 wxMutexLocker(wxMutex
*mutex
)
100 { m_isOk
= mutex
&& ((m_mutex
= mutex
)->Lock() == wxMUTEX_NO_ERROR
); }
102 // returns TRUE if mutex was successfully locked in ctor
103 bool IsOk() const { return m_isOk
; }
105 // unlock the mutex in dtor
106 ~wxMutexLocker() { if ( IsOk() ) m_mutex
->Unlock(); }
109 // no assignment operator nor copy ctor
110 wxMutexLocker(const wxMutexLocker
&);
111 wxMutexLocker
& operator=(const wxMutexLocker
&);
117 // ----------------------------------------------------------------------------
118 // Critical section: this is the same as mutex but is only visible to the
119 // threads of the same process. For the platforms which don't have native
120 // support for critical sections, they're implemented entirely in terms of
122 // ----------------------------------------------------------------------------
124 // you should consider wxCriticalSectionLocker whenever possible instead of
125 // directly working with wxCriticalSection class - it is safer
127 class WXDLLEXPORT wxCriticalSectionInternal
;
128 #define WXCRITICAL_INLINE
130 #define WXCRITICAL_INLINE inline
132 class WXDLLEXPORT wxCriticalSection
136 WXCRITICAL_INLINE
wxCriticalSection();
137 WXCRITICAL_INLINE
~wxCriticalSection();
139 // enter the section (the same as locking a mutex)
140 void WXCRITICAL_INLINE
Enter();
141 // leave the critical section (same as unlocking a mutex)
142 void WXCRITICAL_INLINE
Leave();
145 // no assignment operator nor copy ctor
146 wxCriticalSection(const wxCriticalSection
&);
147 wxCriticalSection
& operator=(const wxCriticalSection
&);
150 wxCriticalSectionInternal
*m_critsect
;
156 // wxCriticalSectionLocker is the same to critical sections as wxMutexLocker is
158 class WXDLLEXPORT wxCriticalSectionLocker
161 wxCriticalSectionLocker(wxCriticalSection
& critsect
) : m_critsect(critsect
)
162 { m_critsect
.Enter(); }
163 ~wxCriticalSectionLocker()
164 { m_critsect
.Leave(); }
167 // no assignment operator nor copy ctor
168 wxCriticalSectionLocker(const wxCriticalSectionLocker
&);
169 wxCriticalSectionLocker
& operator=(const wxCriticalSectionLocker
&);
171 wxCriticalSection
& m_critsect
;
174 // ----------------------------------------------------------------------------
175 // Condition handler.
176 // ----------------------------------------------------------------------------
178 class wxConditionInternal
;
179 class WXDLLEXPORT wxCondition
182 // constructor & destructor
186 // Waits indefinitely.
187 void Wait(wxMutex
& mutex
);
188 // Waits until a signal is raised or the timeout is elapsed.
189 bool Wait(wxMutex
& mutex
, unsigned long sec
, unsigned long nsec
);
190 // Raises a signal: only one "Waiter" is released.
192 // Broadcasts to all "Waiters".
196 wxConditionInternal
*p_internal
;
199 // ----------------------------------------------------------------------------
200 // Thread management class
201 // ----------------------------------------------------------------------------
203 class wxThreadInternal
;
204 class WXDLLEXPORT wxThread
207 // constructor & destructor.
211 // Create a new thread, this method should check there is only one thread
212 // running by object.
213 wxThreadError
Create();
215 // Destroys the thread immediately if the defer flag isn't true.
216 wxThreadError
Destroy();
218 // Pause a running thread
219 wxThreadError
Pause();
221 // Resume a paused thread
222 wxThreadError
Resume();
224 // Switches on the defer flag.
225 void DeferDestroy(bool on
);
227 // Waits for the termination of the thread.
230 // Sets the priority to "prio". (Warning: The priority can only be set before
231 // the thread is created)
232 void SetPriority(int prio
);
233 // Get the current priority.
234 int GetPriority() const;
237 unsigned long GetID() const;
239 // Returns true if the thread is alive.
240 bool IsAlive() const;
241 // Returns true if the thread is running (not paused, not killed).
242 bool IsRunning() const;
243 // Returns true if the thread is suspended
244 bool IsPaused() const { return IsAlive() && !IsRunning(); }
246 // Returns true if current thread is the main thread (aka the GUI thread)
247 static bool IsMain();
249 // Called when thread exits.
250 virtual void OnExit();
253 // Returns TRUE if the thread was asked to terminate
256 // Exits from the current thread.
257 void Exit(void *status
= NULL
);
260 // Entry point for the thread.
261 virtual void *Entry() = 0;
264 friend class wxThreadInternal
;
266 wxThreadInternal
*p_internal
;
269 // ----------------------------------------------------------------------------
270 // Automatic initialization
271 // ----------------------------------------------------------------------------
273 // GUI mutex handling.
274 void WXDLLEXPORT
wxMutexGuiEnter();
275 void WXDLLEXPORT
wxMutexGuiLeave();
277 #else // !wxUSE_THREADS
280 inline void WXDLLEXPORT
wxMutexGuiEnter() { }
281 inline void WXDLLEXPORT
wxMutexGuiLeave() { }
283 #endif // wxUSE_THREADS
285 // automatically unlock GUI mutex in dtor
286 class WXDLLEXPORT wxMutexGuiLocker
289 wxMutexGuiLocker() { wxMutexGuiEnter(); }
290 ~wxMutexGuiLocker() { wxMutexGuiLeave(); }
293 // -----------------------------------------------------------------------------
294 // implementation only until the end of file
295 // -----------------------------------------------------------------------------
298 // unlock GUI if there are threads waiting for and lock it back when
299 // there are no more of them - should be called periodically by the main
301 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter();
303 // returns TRUE if the main thread has GUI lock
304 bool WXDLLEXPORT
wxGuiOwnedByMainThread();
306 // wakes up the main thread if it's sleeping inside ::GetMessage()
307 void WXDLLEXPORT
wxWakeUpMainThread();
309 // implement wxCriticalSection using mutexes
310 inline wxCriticalSection::wxCriticalSection() { }
311 inline wxCriticalSection::~wxCriticalSection() { }
313 inline void wxCriticalSection::Enter() { (void)m_mutex
.Lock(); }
314 inline void wxCriticalSection::Leave() { (void)m_mutex
.Unlock(); }
316 #endif // wxUSE_THREADS
318 #endif // __THREADH__