]> git.saurik.com Git - wxWidgets.git/blame - include/wx/thread.h
OpenGl works now under GTK
[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
06cfab17
RR
117#ifdef __WXMSW__
118
9d133d87
VZ
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
123// mutexes
124// ----------------------------------------------------------------------------
125
126// you should consider wxCriticalSectionLocker whenever possible instead of
127// directly working with wxCriticalSection class - it is safer
128class WXDLLEXPORT wxCriticalSectionInternal;
129class WXDLLEXPORT wxCriticalSection
130{
131public:
132 // ctor & dtor
133 wxCriticalSection();
134 ~wxCriticalSection();
135
136 // enter the section (the same as locking a mutex)
137 void Enter();
138 // leave the critical section (same as unlocking a mutex)
139 void Leave();
140
141private:
cb4f1ca4
VZ
142 // no assignment operator nor copy ctor
143 wxCriticalSection(const wxCriticalSection&);
144 wxCriticalSection& operator=(const wxCriticalSection&);
145
9d133d87
VZ
146 wxCriticalSectionInternal *m_critsect;
147};
148
149// wxCriticalSectionLocker is the same to critical sections as wxMutexLocker is
150// to th mutexes
151class WXDLLEXPORT wxCriticalSectionLocker
152{
153public:
bee503b0
VZ
154 wxCriticalSectionLocker(wxCriticalSection& critsect) : m_critsect(critsect)
155 { m_critsect.Enter(); }
9d133d87 156 ~wxCriticalSectionLocker()
bee503b0 157 { m_critsect.Leave(); }
9d133d87
VZ
158
159private:
cb4f1ca4
VZ
160 // no assignment operator nor copy ctor
161 wxCriticalSectionLocker(const wxCriticalSectionLocker&);
162 wxCriticalSectionLocker& operator=(const wxCriticalSectionLocker&);
163
bee503b0 164 wxCriticalSection& m_critsect;
9d133d87
VZ
165};
166
06cfab17
RR
167#endif
168
a6b0bd49 169// ----------------------------------------------------------------------------
10b959e3 170// Condition handler.
a6b0bd49 171// ----------------------------------------------------------------------------
d524867f 172
10b959e3 173class wxConditionInternal;
d524867f
RR
174class WXDLLEXPORT wxCondition
175{
10b959e3
JS
176public:
177 // constructor & destructor
ee4f8c2a
JS
178 wxCondition();
179 ~wxCondition();
10b959e3 180
ee4f8c2a 181 // Waits indefinitely.
10b959e3
JS
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.
ee4f8c2a 186 void Signal();
10b959e3 187 // Broadcasts to all "Waiters".
ee4f8c2a 188 void Broadcast();
a6b0bd49 189
10b959e3
JS
190private:
191 wxConditionInternal *p_internal;
192};
193
a6b0bd49 194// ----------------------------------------------------------------------------
10b959e3 195// Thread management class
a6b0bd49 196// ----------------------------------------------------------------------------
d524867f 197
10b959e3 198class wxThreadInternal;
d524867f
RR
199class WXDLLEXPORT wxThread
200{
10b959e3
JS
201public:
202 // constructor & destructor.
ee4f8c2a
JS
203 wxThread();
204 virtual ~wxThread();
10b959e3
JS
205
206 // Create a new thread, this method should check there is only one thread
207 // running by object.
ee4f8c2a 208 wxThreadError Create();
10b959e3 209
ee4f8c2a
JS
210 // Destroys the thread immediately if the defer flag isn't true.
211 wxThreadError Destroy();
10b959e3 212
c2dd8380
GL
213 // Pause a running thread
214 wxThreadError Pause();
215
216 // Resume a paused thread
217 wxThreadError Resume();
218
ee4f8c2a 219 // Switches on the defer flag.
10b959e3
JS
220 void DeferDestroy(bool on);
221
222 // Waits for the termination of the thread.
ee4f8c2a 223 void *Join();
10b959e3
JS
224
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.
ee4f8c2a 229 int GetPriority() const;
10b959e3
JS
230
231 // Get the thread ID
ee4f8c2a 232 unsigned long GetID() const;
10b959e3
JS
233
234 // Returns true if the thread is alive.
ee4f8c2a 235 bool IsAlive() const;
c2dd8380
GL
236 // Returns true if the thread is running (not paused, not killed).
237 bool IsRunning() const;
a6b0bd49
VZ
238 // Returns true if the thread is suspended
239 bool IsPaused() const { return IsAlive() && !IsRunning(); }
240
9d133d87 241 // Returns true if current thread is the main thread (aka the GUI thread)
ee4f8c2a 242 static bool IsMain();
10b959e3
JS
243
244 // Called when thread exits.
ee4f8c2a 245 virtual void OnExit();
c2dd8380 246
10b959e3 247protected:
9d133d87
VZ
248 // Returns TRUE if the thread was asked to terminate
249 bool TestDestroy();
250
10b959e3
JS
251 // Exits from the current thread.
252 void Exit(void *status = NULL);
a6b0bd49 253
10b959e3
JS
254private:
255 // Entry point for the thread.
ee4f8c2a 256 virtual void *Entry() = 0;
10b959e3
JS
257
258private:
259 friend class wxThreadInternal;
260
261 wxThreadInternal *p_internal;
262};
263
a6b0bd49 264// ----------------------------------------------------------------------------
d524867f 265// Automatic initialization
a6b0bd49 266// ----------------------------------------------------------------------------
10b959e3 267
9d133d87
VZ
268// GUI mutex handling.
269void WXDLLEXPORT wxMutexGuiEnter();
270void WXDLLEXPORT wxMutexGuiLeave();
d524867f 271
bee503b0
VZ
272// implementation only
273#ifdef __WXMSW__
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
276 // thread
277 void WXDLLEXPORT wxMutexGuiLeaveOrEnter();
278
279 // returns TRUE if the main thread has GUI lock
280 inline bool WXDLLEXPORT wxGuiOwnedByMainThread();
281
282 // wakes up the main thread if it's sleeping inside ::GetMessage()
283 inline void WXDLLEXPORT wxWakeUpMainThread();
284#endif // MSW
285
9d133d87 286#else // !wxUSE_THREADS
d524867f 287
9d133d87
VZ
288// no thread support
289inline void WXDLLEXPORT wxMutexGuiEnter() { }
290inline void WXDLLEXPORT wxMutexGuiLeave() { }
d524867f 291
9d133d87 292#endif // wxUSE_THREADS
10b959e3 293
bee503b0
VZ
294// automatically unlock GUI mutex in dtor
295class WXDLLEXPORT wxMutexGuiLocker
296{
297public:
298 wxMutexGuiLocker() { wxMutexGuiEnter(); }
299 ~wxMutexGuiLocker() { wxMutexGuiLeave(); }
300};
301
a6b0bd49 302#endif // __THREADH__