]> git.saurik.com Git - wxWidgets.git/blame - include/wx/thread.h
Added wxPrivateDropTarget
[wxWidgets.git] / include / wx / thread.h
CommitLineData
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
31typedef 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
40typedef 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 63class WXDLLEXPORT wxMutexInternal;
d524867f
RR
64class WXDLLEXPORT wxMutex
65{
10b959e3 66public:
cb4f1ca4
VZ
67 // constructor & destructor
68 wxMutex();
69 ~wxMutex();
10b959e3 70
cb4f1ca4
VZ
71 // Lock the mutex.
72 wxMutexError Lock();
73 // Try to lock the mutex: if it can't, returns immediately with an error.
74 wxMutexError TryLock();
75 // Unlock the mutex.
76 wxMutexError Unlock();
10b959e3 77
cb4f1ca4
VZ
78 // Returns true if the mutex is locked.
79 bool IsLocked() const { return (m_locked > 0); }
a6b0bd49 80
10b959e3 81protected:
cb4f1ca4 82 friend class wxCondition;
10b959e3 83
cb4f1ca4
VZ
84 // no assignment operator nor copy ctor
85 wxMutex(const wxMutex&);
86 wxMutex& operator=(const wxMutex&);
87
88 int m_locked;
89 wxMutexInternal *p_internal;
10b959e3
JS
90};
91
9d133d87
VZ
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
95class WXDLLEXPORT wxMutexLocker
96{
97public:
98 // lock the mutex in the ctor
99 wxMutexLocker(wxMutex *mutex)
100 { m_isOk = mutex && ((m_mutex = mutex)->Lock() == wxMUTEX_NO_ERROR); }
101
102 // returns TRUE if mutex was successfully locked in ctor
103 bool IsOk() const { return m_isOk; }
104
105 // unlock the mutex in dtor
106 ~wxMutexLocker() { if ( IsOk() ) m_mutex->Unlock(); }
107
108private:
cb4f1ca4
VZ
109 // no assignment operator nor copy ctor
110 wxMutexLocker(const wxMutexLocker&);
111 wxMutexLocker& operator=(const wxMutexLocker&);
112
9d133d87
VZ
113 bool m_isOk;
114 wxMutex *m_mutex;
115};
116
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
121// mutexes
122// ----------------------------------------------------------------------------
123
124// you should consider wxCriticalSectionLocker whenever possible instead of
125// directly working with wxCriticalSection class - it is safer
f6ddc54a
VZ
126#ifdef __WXMSW__
127 class WXDLLEXPORT wxCriticalSectionInternal;
128 #define WXCRITICAL_INLINE
129#else // !MSW
130 #define WXCRITICAL_INLINE inline
131#endif // MSW/!MSW
9d133d87
VZ
132class WXDLLEXPORT wxCriticalSection
133{
134public:
135 // ctor & dtor
f6ddc54a
VZ
136 WXCRITICAL_INLINE wxCriticalSection();
137 WXCRITICAL_INLINE ~wxCriticalSection();
9d133d87
VZ
138
139 // enter the section (the same as locking a mutex)
f6ddc54a 140 void WXCRITICAL_INLINE Enter();
9d133d87 141 // leave the critical section (same as unlocking a mutex)
f6ddc54a 142 void WXCRITICAL_INLINE Leave();
9d133d87
VZ
143
144private:
cb4f1ca4
VZ
145 // no assignment operator nor copy ctor
146 wxCriticalSection(const wxCriticalSection&);
147 wxCriticalSection& operator=(const wxCriticalSection&);
148
f6ddc54a 149#ifdef __WXMSW__
9d133d87 150 wxCriticalSectionInternal *m_critsect;
f6ddc54a
VZ
151#else // !MSW
152 wxMutex m_mutex;
153#endif // MSW/!MSW
9d133d87
VZ
154};
155
156// wxCriticalSectionLocker is the same to critical sections as wxMutexLocker is
157// to th mutexes
158class WXDLLEXPORT wxCriticalSectionLocker
159{
160public:
bee503b0
VZ
161 wxCriticalSectionLocker(wxCriticalSection& critsect) : m_critsect(critsect)
162 { m_critsect.Enter(); }
9d133d87 163 ~wxCriticalSectionLocker()
bee503b0 164 { m_critsect.Leave(); }
9d133d87
VZ
165
166private:
cb4f1ca4
VZ
167 // no assignment operator nor copy ctor
168 wxCriticalSectionLocker(const wxCriticalSectionLocker&);
169 wxCriticalSectionLocker& operator=(const wxCriticalSectionLocker&);
170
bee503b0 171 wxCriticalSection& m_critsect;
9d133d87
VZ
172};
173
a6b0bd49 174// ----------------------------------------------------------------------------
10b959e3 175// Condition handler.
a6b0bd49 176// ----------------------------------------------------------------------------
d524867f 177
10b959e3 178class wxConditionInternal;
d524867f
RR
179class WXDLLEXPORT wxCondition
180{
10b959e3
JS
181public:
182 // constructor & destructor
ee4f8c2a
JS
183 wxCondition();
184 ~wxCondition();
10b959e3 185
ee4f8c2a 186 // Waits indefinitely.
10b959e3
JS
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.
ee4f8c2a 191 void Signal();
10b959e3 192 // Broadcasts to all "Waiters".
ee4f8c2a 193 void Broadcast();
a6b0bd49 194
10b959e3
JS
195private:
196 wxConditionInternal *p_internal;
197};
198
a6b0bd49 199// ----------------------------------------------------------------------------
10b959e3 200// Thread management class
a6b0bd49 201// ----------------------------------------------------------------------------
d524867f 202
10b959e3 203class wxThreadInternal;
d524867f
RR
204class WXDLLEXPORT wxThread
205{
10b959e3
JS
206public:
207 // constructor & destructor.
ee4f8c2a
JS
208 wxThread();
209 virtual ~wxThread();
10b959e3
JS
210
211 // Create a new thread, this method should check there is only one thread
212 // running by object.
ee4f8c2a 213 wxThreadError Create();
10b959e3 214
ee4f8c2a
JS
215 // Destroys the thread immediately if the defer flag isn't true.
216 wxThreadError Destroy();
10b959e3 217
c2dd8380
GL
218 // Pause a running thread
219 wxThreadError Pause();
220
221 // Resume a paused thread
222 wxThreadError Resume();
223
ee4f8c2a 224 // Switches on the defer flag.
10b959e3
JS
225 void DeferDestroy(bool on);
226
227 // Waits for the termination of the thread.
ee4f8c2a 228 void *Join();
10b959e3
JS
229
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.
ee4f8c2a 234 int GetPriority() const;
10b959e3
JS
235
236 // Get the thread ID
ee4f8c2a 237 unsigned long GetID() const;
10b959e3
JS
238
239 // Returns true if the thread is alive.
ee4f8c2a 240 bool IsAlive() const;
c2dd8380
GL
241 // Returns true if the thread is running (not paused, not killed).
242 bool IsRunning() const;
a6b0bd49
VZ
243 // Returns true if the thread is suspended
244 bool IsPaused() const { return IsAlive() && !IsRunning(); }
245
9d133d87 246 // Returns true if current thread is the main thread (aka the GUI thread)
ee4f8c2a 247 static bool IsMain();
10b959e3
JS
248
249 // Called when thread exits.
ee4f8c2a 250 virtual void OnExit();
c2dd8380 251
10b959e3 252protected:
9d133d87
VZ
253 // Returns TRUE if the thread was asked to terminate
254 bool TestDestroy();
255
10b959e3
JS
256 // Exits from the current thread.
257 void Exit(void *status = NULL);
a6b0bd49 258
10b959e3
JS
259private:
260 // Entry point for the thread.
ee4f8c2a 261 virtual void *Entry() = 0;
10b959e3
JS
262
263private:
264 friend class wxThreadInternal;
265
266 wxThreadInternal *p_internal;
267};
268
a6b0bd49 269// ----------------------------------------------------------------------------
d524867f 270// Automatic initialization
a6b0bd49 271// ----------------------------------------------------------------------------
10b959e3 272
9d133d87
VZ
273// GUI mutex handling.
274void WXDLLEXPORT wxMutexGuiEnter();
275void WXDLLEXPORT wxMutexGuiLeave();
d524867f 276
9d133d87 277#else // !wxUSE_THREADS
d524867f 278
9d133d87
VZ
279// no thread support
280inline void WXDLLEXPORT wxMutexGuiEnter() { }
281inline void WXDLLEXPORT wxMutexGuiLeave() { }
d524867f 282
9d133d87 283#endif // wxUSE_THREADS
10b959e3 284
bee503b0
VZ
285// automatically unlock GUI mutex in dtor
286class WXDLLEXPORT wxMutexGuiLocker
287{
288public:
289 wxMutexGuiLocker() { wxMutexGuiEnter(); }
290 ~wxMutexGuiLocker() { wxMutexGuiLeave(); }
291};
292
f6ddc54a
VZ
293// -----------------------------------------------------------------------------
294// implementation only until the end of file
295// -----------------------------------------------------------------------------
296#ifdef wxUSE_THREADS
297#ifdef __WXMSW__
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
300 // thread
a0abb8a8 301 extern void WXDLLEXPORT wxMutexGuiLeaveOrEnter();
f6ddc54a
VZ
302
303 // returns TRUE if the main thread has GUI lock
a0abb8a8 304 extern bool WXDLLEXPORT wxGuiOwnedByMainThread();
f6ddc54a
VZ
305
306 // wakes up the main thread if it's sleeping inside ::GetMessage()
a0abb8a8 307 extern void WXDLLEXPORT wxWakeUpMainThread();
f6ddc54a
VZ
308#else // !MSW
309 // implement wxCriticalSection using mutexes
310 inline wxCriticalSection::wxCriticalSection() { }
311 inline wxCriticalSection::~wxCriticalSection() { }
312
313 inline void wxCriticalSection::Enter() { (void)m_mutex.Lock(); }
314 inline void wxCriticalSection::Leave() { (void)m_mutex.Unlock(); }
315#endif // MSW/!MSW
316#endif // wxUSE_THREADS
317
a6b0bd49 318#endif // __THREADH__