X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f6bcfd974ef26faf6f91a62cac09827e09463fd1..211cc8dc907ee50ca6d383b8df16bba9a4d3ce2d:/src/msw/thread.cpp diff --git a/src/msw/thread.cpp b/src/msw/thread.cpp index e701abb39b..f76f9a4c7b 100644 --- a/src/msw/thread.cpp +++ b/src/msw/thread.cpp @@ -211,44 +211,83 @@ class wxConditionInternal public: wxConditionInternal() { - event = ::CreateEvent( - NULL, // default secutiry - FALSE, // not manual reset - FALSE, // nonsignaled initially - NULL // nameless event - ); - if ( !event ) + m_hEvent = ::CreateEvent( + NULL, // default secutiry + FALSE, // not manual reset + FALSE, // nonsignaled initially + NULL // nameless event + ); + if ( !m_hEvent ) { wxLogSysError(_("Can not create event object.")); } - waiters = 0; + + // nobody waits for us yet + m_nWaiters = 0; } bool Wait(DWORD timeout) { - waiters++; + // as m_nWaiters variable is accessed from multiple waiting threads + // (and possibly from the broadcasting thread), we need to change its + // value atomically + ::InterlockedIncrement(&m_nWaiters); - // FIXME this should be MsgWaitForMultipleObjects() as well probably - DWORD rc = ::WaitForSingleObject(event, timeout); + // FIXME this should be MsgWaitForMultipleObjects() as we want to keep + // processing Windows messages while waiting (or don't we?) + DWORD rc = ::WaitForSingleObject(m_hEvent, timeout); - waiters--; + ::InterlockedDecrement(&m_nWaiters); return rc != WAIT_TIMEOUT; } + void Signal() + { + // set the event to signaled: if a thread is already waiting on it, it + // will be woken up, otherwise the event will remain in the signaled + // state until someone waits on it. In any case, the system will return + // it to a non signalled state afterwards. If multiple threads are + // waiting, only one will be woken up. + if ( !::SetEvent(m_hEvent) ) + { + wxLogLastError(wxT("SetEvent")); + } + } + + void Broadcast() + { + // we need to save the original value as m_nWaiters is goign to be + // decreased by the signalled thread resulting in the loop being + // executed less times than needed + LONG nWaiters = m_nWaiters; + + // this works because all these threads are already waiting and so each + // SetEvent() inside Signal() is really a PulseEvent() because the + // event state is immediately returned to non-signaled + for ( LONG n = 0; n < nWaiters; n++ ) + { + Signal(); + } + } + ~wxConditionInternal() { - if ( event ) + if ( m_hEvent ) { - if ( !::CloseHandle(event) ) + if ( !::CloseHandle(m_hEvent) ) { wxLogLastError(wxT("CloseHandle(event)")); } } } - HANDLE event; - int waiters; +private: + // the Win32 synchronization object corresponding to this event + HANDLE m_hEvent; + + // number of threads waiting for this condition + LONG m_nWaiters; }; wxCondition::wxCondition() @@ -274,26 +313,12 @@ bool wxCondition::Wait(unsigned long sec, void wxCondition::Signal() { - // set the event to signaled: if a thread is already waiting on it, it will - // be woken up, otherwise the event will remain in the signaled state until - // someone waits on it. In any case, the system will return it to a non - // signalled state afterwards. If multiple threads are waiting, only one - // will be woken up. - if ( !::SetEvent(m_internal->event) ) - { - wxLogLastError(wxT("SetEvent")); - } + m_internal->Signal(); } void wxCondition::Broadcast() { - // this works because all these threads are already waiting and so each - // SetEvent() inside Signal() is really a PulseEvent() because the event - // state is immediately returned to non-signaled - for ( int i = 0; i < m_internal->waiters; i++ ) - { - Signal(); - } + m_internal->Broadcast(); } // ---------------------------------------------------------------------------- @@ -302,8 +327,14 @@ void wxCondition::Broadcast() wxCriticalSection::wxCriticalSection() { - wxASSERT_MSG( sizeof(CRITICAL_SECTION) <= sizeof(m_buffer), +#ifdef __WXDEBUG__ + // Done this way to stop warnings during compilation about statement + // always being false + int csSize = sizeof(CRITICAL_SECTION); + int bSize = sizeof(m_buffer); + wxASSERT_MSG( csSize <= bSize, _T("must increase buffer size in wx/thread.h") ); +#endif ::InitializeCriticalSection((CRITICAL_SECTION *)m_buffer); } @@ -1126,7 +1157,7 @@ void WXDLLEXPORT wxMutexGuiLeave() } else { - // decrement the number of waiters now + // decrement the number of threads waiting for GUI access now wxASSERT_MSG( gs_nWaitingForGui > 0, wxT("calling wxMutexGuiLeave() without entering it first?") );