1 /////////////////////////////////////////////////////////////////////////////
2 // Name: include/wx/thrimpl.cpp
3 // Purpose: common part of wxThread Implementations
4 // Author: Vadim Zeitlin
6 // Created: 04.06.02 (extracted from src/*/thread.cpp files)
8 // Copyright: (c) Vadim Zeitlin (2002)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // this file is supposed to be included only by the various thread.cpp
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 wxMutex::wxMutex(wxMutexType mutexType
)
20 m_internal
= new wxMutexInternal(mutexType
);
22 if ( !m_internal
->IsOk() )
34 bool wxMutex::IsOk() const
36 return m_internal
!= NULL
;
39 wxMutexError
wxMutex::Lock()
41 wxCHECK_MSG( m_internal
, wxMUTEX_INVALID
,
42 _T("wxMutex::Lock(): not initialized") );
44 return m_internal
->Lock();
47 wxMutexError
wxMutex::TryLock()
49 wxCHECK_MSG( m_internal
, wxMUTEX_INVALID
,
50 _T("wxMutex::TryLock(): not initialized") );
52 return m_internal
->TryLock();
55 wxMutexError
wxMutex::Unlock()
57 wxCHECK_MSG( m_internal
, wxMUTEX_INVALID
,
58 _T("wxMutex::Unlock(): not initialized") );
60 return m_internal
->Unlock();
63 // --------------------------------------------------------------------------
64 // wxConditionInternal
65 // --------------------------------------------------------------------------
67 #if defined(__WXMSW__) || defined(__OS2__) || 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
72 #if defined(__OS2__) || defined(__EMX__)
73 void InterlockedIncrement(LONG
*num
)
81 class wxConditionInternal
84 wxConditionInternal(wxMutex
& mutex
);
86 bool IsOk() const { return m_mutex
.IsOk() && m_semaphore
.IsOk(); }
89 wxCondError
WaitTimeout(unsigned long milliseconds
);
92 wxCondError
Broadcast();
95 // the number of threads currently waiting for this condition
98 // the critical section protecting m_numWaiters
99 wxCriticalSection m_csWaiters
;
102 wxSemaphore m_semaphore
;
104 DECLARE_NO_COPY_CLASS(wxConditionInternal
)
107 wxConditionInternal::wxConditionInternal(wxMutex
& mutex
)
110 // another thread can't access it until we return from ctor, so no need to
111 // protect access to m_numWaiters here
115 wxCondError
wxConditionInternal::Wait()
117 // increment the number of waiters
118 ::InterlockedIncrement(&m_numWaiters
);
122 // a potential race condition can occur here
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
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
132 // wait ( if necessary ) and decrement semaphore
133 wxSemaError err
= m_semaphore
.Wait();
136 return err
== wxSEMA_NO_ERROR
? wxCOND_NO_ERROR
: wxCOND_MISC_ERROR
;
139 wxCondError
wxConditionInternal::WaitTimeout(unsigned long milliseconds
)
141 ::InterlockedIncrement(&m_numWaiters
);
145 // a race condition can occur at this point in the code
147 // please see the comments in Wait(), for details
149 wxSemaError err
= m_semaphore
.WaitTimeout(milliseconds
);
151 if ( err
== wxSEMA_BUSY
)
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'.
157 // at this point if another thread calls signal() then the semaphore
158 // will be incremented, but the waiting thread will miss it.
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
165 wxCriticalSectionLocker
lock(m_csWaiters
);
167 err
= m_semaphore
.WaitTimeout(0);
169 if ( err
!= wxSEMA_NO_ERROR
)
177 return err
== wxSEMA_NO_ERROR
? wxCOND_NO_ERROR
: wxCOND_MISC_ERROR
;
180 wxCondError
wxConditionInternal::Signal()
182 wxCriticalSectionLocker
lock(m_csWaiters
);
184 if ( m_numWaiters
> 0 )
186 // increment the semaphore by 1
187 if ( m_semaphore
.Post() != wxSEMA_NO_ERROR
)
188 return wxCOND_MISC_ERROR
;
193 return wxCOND_NO_ERROR
;
196 wxCondError
wxConditionInternal::Broadcast()
198 wxCriticalSectionLocker
lock(m_csWaiters
);
200 while ( m_numWaiters
> 0 )
202 if ( m_semaphore
.Post() != wxSEMA_NO_ERROR
)
203 return wxCOND_MISC_ERROR
;
208 return wxCOND_NO_ERROR
;
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 wxCondition::wxCondition(wxMutex
& mutex
)
218 m_internal
= new wxConditionInternal(mutex
);
220 if ( !m_internal
->IsOk() )
227 wxCondition::~wxCondition()
232 bool wxCondition::IsOk() const
234 return m_internal
!= NULL
;
237 wxCondError
wxCondition::Wait()
239 wxCHECK_MSG( m_internal
, wxCOND_INVALID
,
240 _T("wxCondition::Wait(): not initialized") );
242 return m_internal
->Wait();
245 wxCondError
wxCondition::WaitTimeout(unsigned long milliseconds
)
247 wxCHECK_MSG( m_internal
, wxCOND_INVALID
,
248 _T("wxCondition::Wait(): not initialized") );
250 return m_internal
->WaitTimeout(milliseconds
);
253 wxCondError
wxCondition::Signal()
255 wxCHECK_MSG( m_internal
, wxCOND_INVALID
,
256 _T("wxCondition::Signal(): not initialized") );
258 return m_internal
->Signal();
261 wxCondError
wxCondition::Broadcast()
263 wxCHECK_MSG( m_internal
, wxCOND_INVALID
,
264 _T("wxCondition::Broadcast(): not initialized") );
266 return m_internal
->Broadcast();
269 // --------------------------------------------------------------------------
271 // --------------------------------------------------------------------------
273 wxSemaphore::wxSemaphore(int initialcount
, int maxcount
)
275 m_internal
= new wxSemaphoreInternal( initialcount
, maxcount
);
276 if ( !m_internal
->IsOk() )
283 wxSemaphore::~wxSemaphore()
288 bool wxSemaphore::IsOk() const
290 return m_internal
!= NULL
;
293 wxSemaError
wxSemaphore::Wait()
295 wxCHECK_MSG( m_internal
, wxSEMA_INVALID
,
296 _T("wxSemaphore::Wait(): not initialized") );
298 return m_internal
->Wait();
301 wxSemaError
wxSemaphore::TryWait()
303 wxCHECK_MSG( m_internal
, wxSEMA_INVALID
,
304 _T("wxSemaphore::TryWait(): not initialized") );
306 return m_internal
->TryWait();
309 wxSemaError
wxSemaphore::WaitTimeout(unsigned long milliseconds
)
311 wxCHECK_MSG( m_internal
, wxSEMA_INVALID
,
312 _T("wxSemaphore::WaitTimeout(): not initialized") );
314 return m_internal
->WaitTimeout(milliseconds
);
317 wxSemaError
wxSemaphore::Post()
319 wxCHECK_MSG( m_internal
, wxSEMA_INVALID
,
320 _T("wxSemaphore::Post(): not initialized") );
322 return m_internal
->Post();