]> git.saurik.com Git - wxWidgets.git/blob - include/wx/thrimpl.cpp
Source cleaning: -1/wxID_ANY/wxDefaultCoord, ::, !!/!IsEmpty(), TRUE/true. FALSE...
[wxWidgets.git] / include / wx / thrimpl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: include/wx/thrimpl.cpp
3 // Purpose: common part of wxThread Implementations
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.06.02 (extracted from src/*/thread.cpp files)
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin (2002)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // this file is supposed to be included only by the various thread.cpp
13
14 // ----------------------------------------------------------------------------
15 // wxMutex
16 // ----------------------------------------------------------------------------
17
18 wxMutex::wxMutex(wxMutexType mutexType)
19 {
20 m_internal = new wxMutexInternal(mutexType);
21
22 if ( !m_internal->IsOk() )
23 {
24 delete m_internal;
25 m_internal = NULL;
26 }
27 }
28
29 wxMutex::~wxMutex()
30 {
31 delete m_internal;
32 }
33
34 bool wxMutex::IsOk() const
35 {
36 return m_internal != NULL;
37 }
38
39 wxMutexError wxMutex::Lock()
40 {
41 wxCHECK_MSG( m_internal, wxMUTEX_INVALID,
42 _T("wxMutex::Lock(): not initialized") );
43
44 return m_internal->Lock();
45 }
46
47 wxMutexError wxMutex::TryLock()
48 {
49 wxCHECK_MSG( m_internal, wxMUTEX_INVALID,
50 _T("wxMutex::TryLock(): not initialized") );
51
52 return m_internal->TryLock();
53 }
54
55 wxMutexError wxMutex::Unlock()
56 {
57 wxCHECK_MSG( m_internal, wxMUTEX_INVALID,
58 _T("wxMutex::Unlock(): not initialized") );
59
60 return m_internal->Unlock();
61 }
62
63 // --------------------------------------------------------------------------
64 // wxConditionInternal
65 // --------------------------------------------------------------------------
66
67 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__EMX__)
68 // Win32 and OS/2 don't have explicit support for the POSIX condition
69 // variables and their events/event semaphores have quite different semantics,
70 // so we reimplement the conditions from scratch using the mutexes and
71 // semaphores
72 #if defined(__WXPM__) || defined(__EMX__)
73 void InterlockedIncrement(LONG *num)
74 {
75 ::DosEnterCritSec();
76 (*num)++;
77 ::DosExitCritSec();
78 }
79 #endif
80
81 class wxConditionInternal
82 {
83 public:
84 wxConditionInternal(wxMutex& mutex);
85
86 bool IsOk() const { return m_mutex.IsOk() && m_semaphore.IsOk(); }
87
88 wxCondError Wait();
89 wxCondError WaitTimeout(unsigned long milliseconds);
90
91 wxCondError Signal();
92 wxCondError Broadcast();
93
94 private:
95 // the number of threads currently waiting for this condition
96 LONG m_numWaiters;
97
98 // the critical section protecting m_numWaiters
99 wxCriticalSection m_csWaiters;
100
101 wxMutex& m_mutex;
102 wxSemaphore m_semaphore;
103
104 DECLARE_NO_COPY_CLASS(wxConditionInternal)
105 };
106
107 wxConditionInternal::wxConditionInternal(wxMutex& mutex)
108 : m_mutex(mutex)
109 {
110 // another thread can't access it until we return from ctor, so no need to
111 // protect access to m_numWaiters here
112 m_numWaiters = 0;
113 }
114
115 wxCondError wxConditionInternal::Wait()
116 {
117 // increment the number of waiters
118 ::InterlockedIncrement(&m_numWaiters);
119
120 m_mutex.Unlock();
121
122 // a potential race condition can occur here
123 //
124 // after a thread increments nwaiters, and unlocks the mutex and before the
125 // semaphore.Wait() is called, if another thread can cause a signal to be
126 // generated
127 //
128 // this race condition is handled by using a semaphore and incrementing the
129 // semaphore only if 'nwaiters' is greater that zero since the semaphore,
130 // can 'remember' signals the race condition will not occur
131
132 // wait ( if necessary ) and decrement semaphore
133 wxSemaError err = m_semaphore.Wait();
134 m_mutex.Lock();
135
136 return err == wxSEMA_NO_ERROR ? wxCOND_NO_ERROR : wxCOND_MISC_ERROR;
137 }
138
139 wxCondError wxConditionInternal::WaitTimeout(unsigned long milliseconds)
140 {
141 ::InterlockedIncrement(&m_numWaiters);
142
143 m_mutex.Unlock();
144
145 // a race condition can occur at this point in the code
146 //
147 // please see the comments in Wait(), for details
148
149 wxSemaError err = m_semaphore.WaitTimeout(milliseconds);
150
151 if ( err == wxSEMA_BUSY )
152 {
153 // another potential race condition exists here it is caused when a
154 // 'waiting' thread timesout, and returns from WaitForSingleObject, but
155 // has not yet decremented 'nwaiters'.
156 //
157 // at this point if another thread calls signal() then the semaphore
158 // will be incremented, but the waiting thread will miss it.
159 //
160 // to handle this particular case, the waiting thread calls
161 // WaitForSingleObject again with a timeout of 0, after locking
162 // 'nwaiters_mutex'. this call does not block because of the zero
163 // timeout, but will allow the waiting thread to catch the missed
164 // signals.
165 wxCriticalSectionLocker lock(m_csWaiters);
166
167 err = m_semaphore.WaitTimeout(0);
168
169 if ( err != wxSEMA_NO_ERROR )
170 {
171 m_numWaiters--;
172 }
173 }
174
175 m_mutex.Lock();
176
177 return err == wxSEMA_NO_ERROR ? wxCOND_NO_ERROR : wxCOND_MISC_ERROR;
178 }
179
180 wxCondError wxConditionInternal::Signal()
181 {
182 wxCriticalSectionLocker lock(m_csWaiters);
183
184 if ( m_numWaiters > 0 )
185 {
186 // increment the semaphore by 1
187 if ( m_semaphore.Post() != wxSEMA_NO_ERROR )
188 return wxCOND_MISC_ERROR;
189
190 m_numWaiters--;
191 }
192
193 return wxCOND_NO_ERROR;
194 }
195
196 wxCondError wxConditionInternal::Broadcast()
197 {
198 wxCriticalSectionLocker lock(m_csWaiters);
199
200 while ( m_numWaiters > 0 )
201 {
202 if ( m_semaphore.Post() != wxSEMA_NO_ERROR )
203 return wxCOND_MISC_ERROR;
204
205 m_numWaiters--;
206 }
207
208 return wxCOND_NO_ERROR;
209 }
210 #endif
211
212 // ----------------------------------------------------------------------------
213 // wxCondition
214 // ----------------------------------------------------------------------------
215
216 wxCondition::wxCondition(wxMutex& mutex)
217 {
218 m_internal = new wxConditionInternal(mutex);
219
220 if ( !m_internal->IsOk() )
221 {
222 delete m_internal;
223 m_internal = NULL;
224 }
225 }
226
227 wxCondition::~wxCondition()
228 {
229 delete m_internal;
230 }
231
232 bool wxCondition::IsOk() const
233 {
234 return m_internal != NULL;
235 }
236
237 wxCondError wxCondition::Wait()
238 {
239 wxCHECK_MSG( m_internal, wxCOND_INVALID,
240 _T("wxCondition::Wait(): not initialized") );
241
242 return m_internal->Wait();
243 }
244
245 wxCondError wxCondition::WaitTimeout(unsigned long milliseconds)
246 {
247 wxCHECK_MSG( m_internal, wxCOND_INVALID,
248 _T("wxCondition::Wait(): not initialized") );
249
250 return m_internal->WaitTimeout(milliseconds);
251 }
252
253 wxCondError wxCondition::Signal()
254 {
255 wxCHECK_MSG( m_internal, wxCOND_INVALID,
256 _T("wxCondition::Signal(): not initialized") );
257
258 return m_internal->Signal();
259 }
260
261 wxCondError wxCondition::Broadcast()
262 {
263 wxCHECK_MSG( m_internal, wxCOND_INVALID,
264 _T("wxCondition::Broadcast(): not initialized") );
265
266 return m_internal->Broadcast();
267 }
268
269 // --------------------------------------------------------------------------
270 // wxSemaphore
271 // --------------------------------------------------------------------------
272
273 wxSemaphore::wxSemaphore(int initialcount, int maxcount)
274 {
275 m_internal = new wxSemaphoreInternal( initialcount, maxcount );
276 if ( !m_internal->IsOk() )
277 {
278 delete m_internal;
279 m_internal = NULL;
280 }
281 }
282
283 wxSemaphore::~wxSemaphore()
284 {
285 delete m_internal;
286 }
287
288 bool wxSemaphore::IsOk() const
289 {
290 return m_internal != NULL;
291 }
292
293 wxSemaError wxSemaphore::Wait()
294 {
295 wxCHECK_MSG( m_internal, wxSEMA_INVALID,
296 _T("wxSemaphore::Wait(): not initialized") );
297
298 return m_internal->Wait();
299 }
300
301 wxSemaError wxSemaphore::TryWait()
302 {
303 wxCHECK_MSG( m_internal, wxSEMA_INVALID,
304 _T("wxSemaphore::TryWait(): not initialized") );
305
306 return m_internal->TryWait();
307 }
308
309 wxSemaError wxSemaphore::WaitTimeout(unsigned long milliseconds)
310 {
311 wxCHECK_MSG( m_internal, wxSEMA_INVALID,
312 _T("wxSemaphore::WaitTimeout(): not initialized") );
313
314 return m_internal->WaitTimeout(milliseconds);
315 }
316
317 wxSemaError wxSemaphore::Post()
318 {
319 wxCHECK_MSG( m_internal, wxSEMA_INVALID,
320 _T("wxSemaphore::Post(): not initialized") );
321
322 return m_internal->Post();
323 }
324