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 // Win32 and OS/2 don't have explicit support for the POSIX condition
68 // variables and their events/event semaphores have quite different semantics,
69 // so we reimplement the conditions from scratch using the mutexes and
71 #if defined(__WXMSW__) || defined(__OS2__) || defined(__EMX__)
73 class wxConditionInternal
76 wxConditionInternal(wxMutex
& mutex
);
78 bool IsOk() const { return m_mutex
.IsOk() && m_semaphore
.IsOk(); }
81 wxCondError
WaitTimeout(unsigned long milliseconds
);
84 wxCondError
Broadcast();
87 // the number of threads currently waiting for this condition
90 // the critical section protecting m_numWaiters
91 wxCriticalSection m_csWaiters
;
94 wxSemaphore m_semaphore
;
96 DECLARE_NO_COPY_CLASS(wxConditionInternal
)
99 wxConditionInternal::wxConditionInternal(wxMutex
& mutex
)
102 // another thread can't access it until we return from ctor, so no need to
103 // protect access to m_numWaiters here
107 wxCondError
wxConditionInternal::Wait()
109 // increment the number of waiters
111 wxCriticalSectionLocker
lock(m_csWaiters
);
117 // a potential race condition can occur here
119 // after a thread increments nwaiters, and unlocks the mutex and before the
120 // semaphore.Wait() is called, if another thread can cause a signal to be
123 // this race condition is handled by using a semaphore and incrementing the
124 // semaphore only if 'nwaiters' is greater that zero since the semaphore,
125 // can 'remember' signals the race condition will not occur
127 // wait ( if necessary ) and decrement semaphore
128 wxSemaError err
= m_semaphore
.Wait();
131 if ( err
== wxSEMA_NO_ERROR
)
132 return wxCOND_NO_ERROR
;
133 else if ( err
== wxSEMA_TIMEOUT
)
134 return wxCOND_TIMEOUT
;
136 return wxCOND_MISC_ERROR
;
139 wxCondError
wxConditionInternal::WaitTimeout(unsigned long milliseconds
)
142 wxCriticalSectionLocker
lock(m_csWaiters
);
148 // a race condition can occur at this point in the code
150 // please see the comments in Wait(), for details
152 wxSemaError err
= m_semaphore
.WaitTimeout(milliseconds
);
154 if ( err
== wxSEMA_TIMEOUT
)
156 // another potential race condition exists here it is caused when a
157 // 'waiting' thread timesout, and returns from WaitForSingleObject, but
158 // has not yet decremented 'nwaiters'.
160 // at this point if another thread calls signal() then the semaphore
161 // will be incremented, but the waiting thread will miss it.
163 // to handle this particular case, the waiting thread calls
164 // WaitForSingleObject again with a timeout of 0, after locking
165 // 'nwaiters_mutex'. this call does not block because of the zero
166 // timeout, but will allow the waiting thread to catch the missed
168 wxCriticalSectionLocker
lock(m_csWaiters
);
170 err
= m_semaphore
.WaitTimeout(0);
172 if ( err
!= wxSEMA_NO_ERROR
)
180 return err
== wxSEMA_NO_ERROR
? wxCOND_NO_ERROR
181 : err
== wxSEMA_TIMEOUT
? wxCOND_TIMEOUT
185 wxCondError
wxConditionInternal::Signal()
187 wxCriticalSectionLocker
lock(m_csWaiters
);
189 if ( m_numWaiters
> 0 )
191 // increment the semaphore by 1
192 if ( m_semaphore
.Post() != wxSEMA_NO_ERROR
)
193 return wxCOND_MISC_ERROR
;
198 return wxCOND_NO_ERROR
;
201 wxCondError
wxConditionInternal::Broadcast()
203 wxCriticalSectionLocker
lock(m_csWaiters
);
205 while ( m_numWaiters
> 0 )
207 if ( m_semaphore
.Post() != wxSEMA_NO_ERROR
)
208 return wxCOND_MISC_ERROR
;
213 return wxCOND_NO_ERROR
;
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 wxCondition::wxCondition(wxMutex
& mutex
)
224 m_internal
= new wxConditionInternal(mutex
);
226 if ( !m_internal
->IsOk() )
233 wxCondition::~wxCondition()
238 bool wxCondition::IsOk() const
240 return m_internal
!= NULL
;
243 wxCondError
wxCondition::Wait()
245 wxCHECK_MSG( m_internal
, wxCOND_INVALID
,
246 _T("wxCondition::Wait(): not initialized") );
248 return m_internal
->Wait();
251 wxCondError
wxCondition::WaitTimeout(unsigned long milliseconds
)
253 wxCHECK_MSG( m_internal
, wxCOND_INVALID
,
254 _T("wxCondition::Wait(): not initialized") );
256 return m_internal
->WaitTimeout(milliseconds
);
259 wxCondError
wxCondition::Signal()
261 wxCHECK_MSG( m_internal
, wxCOND_INVALID
,
262 _T("wxCondition::Signal(): not initialized") );
264 return m_internal
->Signal();
267 wxCondError
wxCondition::Broadcast()
269 wxCHECK_MSG( m_internal
, wxCOND_INVALID
,
270 _T("wxCondition::Broadcast(): not initialized") );
272 return m_internal
->Broadcast();
275 // --------------------------------------------------------------------------
277 // --------------------------------------------------------------------------
279 wxSemaphore::wxSemaphore(int initialcount
, int maxcount
)
281 m_internal
= new wxSemaphoreInternal( initialcount
, maxcount
);
282 if ( !m_internal
->IsOk() )
289 wxSemaphore::~wxSemaphore()
294 bool wxSemaphore::IsOk() const
296 return m_internal
!= NULL
;
299 wxSemaError
wxSemaphore::Wait()
301 wxCHECK_MSG( m_internal
, wxSEMA_INVALID
,
302 _T("wxSemaphore::Wait(): not initialized") );
304 return m_internal
->Wait();
307 wxSemaError
wxSemaphore::TryWait()
309 wxCHECK_MSG( m_internal
, wxSEMA_INVALID
,
310 _T("wxSemaphore::TryWait(): not initialized") );
312 return m_internal
->TryWait();
315 wxSemaError
wxSemaphore::WaitTimeout(unsigned long milliseconds
)
317 wxCHECK_MSG( m_internal
, wxSEMA_INVALID
,
318 _T("wxSemaphore::WaitTimeout(): not initialized") );
320 return m_internal
->WaitTimeout(milliseconds
);
323 wxSemaError
wxSemaphore::Post()
325 wxCHECK_MSG( m_internal
, wxSEMA_INVALID
,
326 _T("wxSemaphore::Post(): not initialized") );
328 return m_internal
->Post();