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
&);
119 // ----------------------------------------------------------------------------
120 // Critical section: this is the same as mutex but is only visible to the
121 // threads of the same process. For the platforms which don't have native
122 // support for critical sections, they're implemented entirely in terms of
124 // ----------------------------------------------------------------------------
126 // you should consider wxCriticalSectionLocker whenever possible instead of
127 // directly working with wxCriticalSection class - it is safer
128 class WXDLLEXPORT wxCriticalSectionInternal
;
129 class WXDLLEXPORT wxCriticalSection
134 ~wxCriticalSection();
136 // enter the section (the same as locking a mutex)
138 // leave the critical section (same as unlocking a mutex)
142 // no assignment operator nor copy ctor
143 wxCriticalSection(const wxCriticalSection
&);
144 wxCriticalSection
& operator=(const wxCriticalSection
&);
146 wxCriticalSectionInternal
*m_critsect
;
149 // wxCriticalSectionLocker is the same to critical sections as wxMutexLocker is
151 class WXDLLEXPORT wxCriticalSectionLocker
154 wxCriticalSectionLocker(wxCriticalSection
& critsect
) : m_critsect(critsect
)
155 { m_critsect
.Enter(); }
156 ~wxCriticalSectionLocker()
157 { m_critsect
.Leave(); }
160 // no assignment operator nor copy ctor
161 wxCriticalSectionLocker(const wxCriticalSectionLocker
&);
162 wxCriticalSectionLocker
& operator=(const wxCriticalSectionLocker
&);
164 wxCriticalSection
& m_critsect
;
169 // ----------------------------------------------------------------------------
170 // Condition handler.
171 // ----------------------------------------------------------------------------
173 class wxConditionInternal
;
174 class WXDLLEXPORT wxCondition
177 // constructor & destructor
181 // Waits indefinitely.
182 void Wait(wxMutex
& mutex
);
183 // Waits until a signal is raised or the timeout is elapsed.
184 bool Wait(wxMutex
& mutex
, unsigned long sec
, unsigned long nsec
);
185 // Raises a signal: only one "Waiter" is released.
187 // Broadcasts to all "Waiters".
191 wxConditionInternal
*p_internal
;
194 // ----------------------------------------------------------------------------
195 // Thread management class
196 // ----------------------------------------------------------------------------
198 class wxThreadInternal
;
199 class WXDLLEXPORT wxThread
202 // constructor & destructor.
206 // Create a new thread, this method should check there is only one thread
207 // running by object.
208 wxThreadError
Create();
210 // Destroys the thread immediately if the defer flag isn't true.
211 wxThreadError
Destroy();
213 // Pause a running thread
214 wxThreadError
Pause();
216 // Resume a paused thread
217 wxThreadError
Resume();
219 // Switches on the defer flag.
220 void DeferDestroy(bool on
);
222 // Waits for the termination of the thread.
225 // Sets the priority to "prio". (Warning: The priority can only be set before
226 // the thread is created)
227 void SetPriority(int prio
);
228 // Get the current priority.
229 int GetPriority() const;
232 unsigned long GetID() const;
234 // Returns true if the thread is alive.
235 bool IsAlive() const;
236 // Returns true if the thread is running (not paused, not killed).
237 bool IsRunning() const;
238 // Returns true if the thread is suspended
239 bool IsPaused() const { return IsAlive() && !IsRunning(); }
241 // Returns true if current thread is the main thread (aka the GUI thread)
242 static bool IsMain();
244 // Called when thread exits.
245 virtual void OnExit();
248 // Returns TRUE if the thread was asked to terminate
251 // Exits from the current thread.
252 void Exit(void *status
= NULL
);
255 // Entry point for the thread.
256 virtual void *Entry() = 0;
259 friend class wxThreadInternal
;
261 wxThreadInternal
*p_internal
;
264 // ----------------------------------------------------------------------------
265 // Automatic initialization
266 // ----------------------------------------------------------------------------
268 // GUI mutex handling.
269 void WXDLLEXPORT
wxMutexGuiEnter();
270 void WXDLLEXPORT
wxMutexGuiLeave();
272 // implementation only
274 // unlock GUI if there are threads waiting for and lock it back when
275 // there are no more of them - should be called periodically by the main
277 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter();
279 // returns TRUE if the main thread has GUI lock
280 inline bool WXDLLEXPORT
wxGuiOwnedByMainThread();
282 // wakes up the main thread if it's sleeping inside ::GetMessage()
283 inline void WXDLLEXPORT
wxWakeUpMainThread();
286 #else // !wxUSE_THREADS
289 inline void WXDLLEXPORT
wxMutexGuiEnter() { }
290 inline void WXDLLEXPORT
wxMutexGuiLeave() { }
292 #endif // wxUSE_THREADS
294 // automatically unlock GUI mutex in dtor
295 class WXDLLEXPORT wxMutexGuiLocker
298 wxMutexGuiLocker() { wxMutexGuiEnter(); }
299 ~wxMutexGuiLocker() { wxMutexGuiLeave(); }
302 #endif // __THREADH__