]> git.saurik.com Git - wxWidgets.git/blame - include/wx/thread.h
supports typedefs, generates "See also:" and adds "virtual " for virtual
[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
JS
66public:
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
81protected:
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
91class WXDLLEXPORT wxMutexLocker
92{
93public:
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
104private:
105 bool m_isOk;
106 wxMutex *m_mutex;
107};
108
06cfab17
RR
109#ifdef __WXMSW__
110
9d133d87
VZ
111// ----------------------------------------------------------------------------
112// Critical section: this is the same as mutex but is only visible to the
113// threads of the same process. For the platforms which don't have native
114// support for critical sections, they're implemented entirely in terms of
115// mutexes
116// ----------------------------------------------------------------------------
117
118// you should consider wxCriticalSectionLocker whenever possible instead of
119// directly working with wxCriticalSection class - it is safer
120class WXDLLEXPORT wxCriticalSectionInternal;
121class WXDLLEXPORT wxCriticalSection
122{
123public:
124 // ctor & dtor
125 wxCriticalSection();
126 ~wxCriticalSection();
127
128 // enter the section (the same as locking a mutex)
129 void Enter();
130 // leave the critical section (same as unlocking a mutex)
131 void Leave();
132
133private:
134 wxCriticalSectionInternal *m_critsect;
135};
136
137// wxCriticalSectionLocker is the same to critical sections as wxMutexLocker is
138// to th mutexes
139class WXDLLEXPORT wxCriticalSectionLocker
140{
141public:
142 wxCriticalSectionLocker(wxCriticalSection *critsect)
143 { (m_critsect = critsect)->Enter(); }
144 ~wxCriticalSectionLocker()
145 { m_critsect->Leave(); }
146
147private:
148 wxCriticalSection *m_critsect;
149};
150
06cfab17
RR
151#endif
152
a6b0bd49 153// ----------------------------------------------------------------------------
10b959e3 154// Condition handler.
a6b0bd49 155// ----------------------------------------------------------------------------
d524867f 156
10b959e3 157class wxConditionInternal;
d524867f
RR
158class WXDLLEXPORT wxCondition
159{
10b959e3
JS
160public:
161 // constructor & destructor
ee4f8c2a
JS
162 wxCondition();
163 ~wxCondition();
10b959e3 164
ee4f8c2a 165 // Waits indefinitely.
10b959e3
JS
166 void Wait(wxMutex& mutex);
167 // Waits until a signal is raised or the timeout is elapsed.
168 bool Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec);
169 // Raises a signal: only one "Waiter" is released.
ee4f8c2a 170 void Signal();
10b959e3 171 // Broadcasts to all "Waiters".
ee4f8c2a 172 void Broadcast();
a6b0bd49 173
10b959e3
JS
174private:
175 wxConditionInternal *p_internal;
176};
177
a6b0bd49 178// ----------------------------------------------------------------------------
10b959e3 179// Thread management class
a6b0bd49 180// ----------------------------------------------------------------------------
d524867f 181
10b959e3 182class wxThreadInternal;
d524867f
RR
183class WXDLLEXPORT wxThread
184{
10b959e3
JS
185public:
186 // constructor & destructor.
ee4f8c2a
JS
187 wxThread();
188 virtual ~wxThread();
10b959e3
JS
189
190 // Create a new thread, this method should check there is only one thread
191 // running by object.
ee4f8c2a 192 wxThreadError Create();
10b959e3 193
ee4f8c2a
JS
194 // Destroys the thread immediately if the defer flag isn't true.
195 wxThreadError Destroy();
10b959e3 196
c2dd8380
GL
197 // Pause a running thread
198 wxThreadError Pause();
199
200 // Resume a paused thread
201 wxThreadError Resume();
202
ee4f8c2a 203 // Switches on the defer flag.
10b959e3
JS
204 void DeferDestroy(bool on);
205
206 // Waits for the termination of the thread.
ee4f8c2a 207 void *Join();
10b959e3
JS
208
209 // Sets the priority to "prio". (Warning: The priority can only be set before
210 // the thread is created)
211 void SetPriority(int prio);
212 // Get the current priority.
ee4f8c2a 213 int GetPriority() const;
10b959e3
JS
214
215 // Get the thread ID
ee4f8c2a 216 unsigned long GetID() const;
10b959e3
JS
217
218 // Returns true if the thread is alive.
ee4f8c2a 219 bool IsAlive() const;
c2dd8380
GL
220 // Returns true if the thread is running (not paused, not killed).
221 bool IsRunning() const;
a6b0bd49
VZ
222 // Returns true if the thread is suspended
223 bool IsPaused() const { return IsAlive() && !IsRunning(); }
224
9d133d87 225 // Returns true if current thread is the main thread (aka the GUI thread)
ee4f8c2a 226 static bool IsMain();
10b959e3
JS
227
228 // Called when thread exits.
ee4f8c2a 229 virtual void OnExit();
c2dd8380 230
10b959e3 231protected:
9d133d87
VZ
232 // Returns TRUE if the thread was asked to terminate
233 bool TestDestroy();
234
10b959e3
JS
235 // Exits from the current thread.
236 void Exit(void *status = NULL);
a6b0bd49 237
10b959e3
JS
238private:
239 // Entry point for the thread.
ee4f8c2a 240 virtual void *Entry() = 0;
10b959e3
JS
241
242private:
243 friend class wxThreadInternal;
244
245 wxThreadInternal *p_internal;
246};
247
a6b0bd49 248// ----------------------------------------------------------------------------
d524867f 249// Automatic initialization
a6b0bd49 250// ----------------------------------------------------------------------------
10b959e3 251
9d133d87
VZ
252// GUI mutex handling.
253void WXDLLEXPORT wxMutexGuiEnter();
254void WXDLLEXPORT wxMutexGuiLeave();
d524867f 255
9d133d87 256#else // !wxUSE_THREADS
d524867f 257
9d133d87
VZ
258// no thread support
259inline void WXDLLEXPORT wxMutexGuiEnter() { }
260inline void WXDLLEXPORT wxMutexGuiLeave() { }
d524867f 261
9d133d87 262#endif // wxUSE_THREADS
10b959e3 263
a6b0bd49 264#endif // __THREADH__