]>
Commit | Line | Data |
---|---|---|
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 | ||
19 | #include "wx/object.h" | |
20 | #include "wx/setup.h" | |
d524867f | 21 | #include "wx/module.h" |
10b959e3 | 22 | |
a6b0bd49 VZ |
23 | // ---------------------------------------------------------------------------- |
24 | // constants | |
25 | // ---------------------------------------------------------------------------- | |
26 | ||
d524867f RR |
27 | typedef enum |
28 | { | |
a6b0bd49 VZ |
29 | wxMUTEX_NO_ERROR = 0, |
30 | wxMUTEX_DEAD_LOCK, // Mutex has been already locked by THE CALLING thread | |
31 | wxMUTEX_BUSY, // Mutex has been already locked by ONE thread | |
32 | wxMUTEX_UNLOCKED, | |
33 | wxMUTEX_MISC_ERROR | |
10b959e3 JS |
34 | } wxMutexError; |
35 | ||
d524867f RR |
36 | typedef enum |
37 | { | |
a6b0bd49 VZ |
38 | wxTHREAD_NO_ERROR = 0, // No error |
39 | wxTHREAD_NO_RESOURCE, // No resource left to create a new thread | |
40 | wxTHREAD_RUNNING, // The thread is already running | |
41 | wxTHREAD_NOT_RUNNING, // The thread isn't running | |
42 | wxTHREAD_MISC_ERROR // Some other error | |
10b959e3 JS |
43 | } wxThreadError; |
44 | ||
d524867f | 45 | /* defines the interval of priority. */ |
a6b0bd49 | 46 | #define WXTHREAD_MIN_PRIORITY 0 |
10b959e3 | 47 | #define WXTHREAD_DEFAULT_PRIORITY 50 |
a6b0bd49 | 48 | #define WXTHREAD_MAX_PRIORITY 100 |
10b959e3 | 49 | |
d524867f RR |
50 | // ---------------------------------------------------------------------------- |
51 | // GUI mutex handling. | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | void WXDLLEXPORT wxMutexGuiEnter(); | |
55 | void WXDLLEXPORT wxMutexGuiLeave(); | |
56 | ||
a6b0bd49 | 57 | // ---------------------------------------------------------------------------- |
10b959e3 | 58 | // Mutex handler |
a6b0bd49 | 59 | // ---------------------------------------------------------------------------- |
d524867f | 60 | |
c2dd8380 | 61 | class WXDLLEXPORT wxMutexInternal; |
d524867f RR |
62 | class WXDLLEXPORT wxMutex |
63 | { | |
10b959e3 JS |
64 | public: |
65 | // constructor & destructor | |
ee4f8c2a JS |
66 | wxMutex(); |
67 | ~wxMutex(); | |
10b959e3 JS |
68 | |
69 | // Lock the mutex. | |
ee4f8c2a | 70 | wxMutexError Lock(); |
10b959e3 | 71 | // Try to lock the mutex: if it can't, returns immediately with an error. |
ee4f8c2a | 72 | wxMutexError TryLock(); |
10b959e3 | 73 | // Unlock the mutex. |
ee4f8c2a | 74 | wxMutexError Unlock(); |
10b959e3 JS |
75 | |
76 | // Returns true if the mutex is locked. | |
ee4f8c2a | 77 | bool IsLocked() const { return (m_locked > 0); } |
a6b0bd49 | 78 | |
10b959e3 JS |
79 | protected: |
80 | friend class wxCondition; | |
81 | ||
82 | int m_locked; | |
83 | wxMutexInternal *p_internal; | |
84 | }; | |
85 | ||
a6b0bd49 | 86 | // ---------------------------------------------------------------------------- |
10b959e3 | 87 | // Condition handler. |
a6b0bd49 | 88 | // ---------------------------------------------------------------------------- |
d524867f | 89 | |
10b959e3 | 90 | class wxConditionInternal; |
d524867f RR |
91 | class WXDLLEXPORT wxCondition |
92 | { | |
10b959e3 JS |
93 | public: |
94 | // constructor & destructor | |
ee4f8c2a JS |
95 | wxCondition(); |
96 | ~wxCondition(); | |
10b959e3 | 97 | |
ee4f8c2a | 98 | // Waits indefinitely. |
10b959e3 JS |
99 | void Wait(wxMutex& mutex); |
100 | // Waits until a signal is raised or the timeout is elapsed. | |
101 | bool Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec); | |
102 | // Raises a signal: only one "Waiter" is released. | |
ee4f8c2a | 103 | void Signal(); |
10b959e3 | 104 | // Broadcasts to all "Waiters". |
ee4f8c2a | 105 | void Broadcast(); |
a6b0bd49 | 106 | |
10b959e3 JS |
107 | private: |
108 | wxConditionInternal *p_internal; | |
109 | }; | |
110 | ||
a6b0bd49 | 111 | // ---------------------------------------------------------------------------- |
10b959e3 | 112 | // Thread management class |
a6b0bd49 | 113 | // ---------------------------------------------------------------------------- |
d524867f | 114 | |
10b959e3 | 115 | class wxThreadInternal; |
d524867f RR |
116 | class WXDLLEXPORT wxThread |
117 | { | |
10b959e3 JS |
118 | public: |
119 | // constructor & destructor. | |
ee4f8c2a JS |
120 | wxThread(); |
121 | virtual ~wxThread(); | |
10b959e3 JS |
122 | |
123 | // Create a new thread, this method should check there is only one thread | |
124 | // running by object. | |
ee4f8c2a | 125 | wxThreadError Create(); |
10b959e3 | 126 | |
ee4f8c2a JS |
127 | // Destroys the thread immediately if the defer flag isn't true. |
128 | wxThreadError Destroy(); | |
10b959e3 | 129 | |
c2dd8380 GL |
130 | // Pause a running thread |
131 | wxThreadError Pause(); | |
132 | ||
133 | // Resume a paused thread | |
134 | wxThreadError Resume(); | |
135 | ||
ee4f8c2a | 136 | // Switches on the defer flag. |
10b959e3 JS |
137 | void DeferDestroy(bool on); |
138 | ||
139 | // Waits for the termination of the thread. | |
ee4f8c2a | 140 | void *Join(); |
10b959e3 JS |
141 | |
142 | // Sets the priority to "prio". (Warning: The priority can only be set before | |
143 | // the thread is created) | |
144 | void SetPriority(int prio); | |
145 | // Get the current priority. | |
ee4f8c2a | 146 | int GetPriority() const; |
10b959e3 JS |
147 | |
148 | // Get the thread ID | |
ee4f8c2a | 149 | unsigned long GetID() const; |
10b959e3 JS |
150 | |
151 | // Returns true if the thread is alive. | |
ee4f8c2a | 152 | bool IsAlive() const; |
c2dd8380 GL |
153 | // Returns true if the thread is running (not paused, not killed). |
154 | bool IsRunning() const; | |
a6b0bd49 VZ |
155 | // Returns true if the thread is suspended |
156 | bool IsPaused() const { return IsAlive() && !IsRunning(); } | |
157 | ||
10b959e3 | 158 | // Returns true if the thread is the main thread (aka the GUI thread). |
ee4f8c2a | 159 | static bool IsMain(); |
10b959e3 JS |
160 | |
161 | // Called when thread exits. | |
ee4f8c2a | 162 | virtual void OnExit(); |
c2dd8380 | 163 | |
10b959e3 JS |
164 | protected: |
165 | // In case, the DIFFER flag is true, enables another thread to kill this one. | |
ee4f8c2a | 166 | void TestDestroy(); |
10b959e3 JS |
167 | // Exits from the current thread. |
168 | void Exit(void *status = NULL); | |
a6b0bd49 | 169 | |
10b959e3 JS |
170 | private: |
171 | // Entry point for the thread. | |
ee4f8c2a | 172 | virtual void *Entry() = 0; |
10b959e3 JS |
173 | |
174 | private: | |
175 | friend class wxThreadInternal; | |
176 | ||
177 | wxThreadInternal *p_internal; | |
178 | }; | |
179 | ||
a6b0bd49 | 180 | // ---------------------------------------------------------------------------- |
d524867f | 181 | // Automatic initialization |
a6b0bd49 | 182 | // ---------------------------------------------------------------------------- |
10b959e3 | 183 | |
d524867f RR |
184 | class wxThreadModule : public wxModule |
185 | { | |
186 | DECLARE_DYNAMIC_CLASS(wxThreadModule) | |
187 | ||
188 | public: | |
189 | wxThreadModule() {} | |
190 | ||
191 | virtual bool OnInit(); | |
192 | virtual void OnExit(); | |
193 | }; | |
194 | ||
195 | ||
10b959e3 | 196 | |
a6b0bd49 | 197 | #endif // __THREADH__ |