added wxMutex::LockTimeout() (modified patch 1671637)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44671 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-03-08 00:22:11 +00:00
parent 4738d6746b
commit 696d13ee0e
6 changed files with 97 additions and 7 deletions

View File

@@ -163,11 +163,16 @@ public:
~wxMutexInternal();
wxMutexError Lock();
wxMutexError Lock(unsigned long ms);
wxMutexError TryLock();
wxMutexError Unlock();
bool IsOk() const { return m_isOk; }
private:
// convert the result of pthread_mutex_[timed]lock() call to wx return code
wxMutexError HandleLockResult(int err);
private:
pthread_mutex_t m_mutex;
bool m_isOk;
@@ -245,7 +250,41 @@ wxMutexInternal::~wxMutexInternal()
wxMutexError wxMutexInternal::Lock()
{
int err = pthread_mutex_lock(&m_mutex);
return HandleLockResult(pthread_mutex_lock(&m_mutex));
}
wxMutexError wxMutexInternal::Lock(unsigned long ms)
{
static const long MSEC_IN_SEC = 1000;
static const long NSEC_IN_MSEC = 1000000;
static const long NSEC_IN_SEC = MSEC_IN_SEC * NSEC_IN_MSEC;
time_t seconds = ms/MSEC_IN_SEC;
long nanoseconds = (ms % MSEC_IN_SEC) * NSEC_IN_MSEC;
timespec ts = { 0, 0 };
if ( clock_gettime(CLOCK_REALTIME, &ts) == 0 )
{
ts.tv_sec += seconds;
ts.tv_nsec += nanoseconds;
if ( ts.tv_nsec > NSEC_IN_SEC )
{
ts.tv_sec += 1;
ts.tv_nsec -= NSEC_IN_SEC;
}
}
else // fall back on system timer
{
wxLogDebug(_T("clock_gettime(CLOCK_REALTIME) failed"));
ts.tv_sec = time(NULL) + seconds;
ts.tv_nsec = nanoseconds;
}
return HandleLockResult(pthread_mutex_timedlock(&m_mutex, &ts));
}
wxMutexError wxMutexInternal::HandleLockResult(int err)
{
switch ( err )
{
case EDEADLK:
@@ -255,19 +294,23 @@ wxMutexError wxMutexInternal::Lock()
return wxMUTEX_DEAD_LOCK;
case EINVAL:
wxLogDebug(_T("pthread_mutex_lock(): mutex not initialized."));
wxLogDebug(_T("pthread_mutex_[timed]lock(): mutex not initialized"));
break;
case ETIMEDOUT:
return wxMUTEX_TIMEOUT;
case 0:
return wxMUTEX_NO_ERROR;
default:
wxLogApiError(_T("pthread_mutex_lock()"), err);
wxLogApiError(_T("pthread_mutex_[timed]lock()"), err);
}
return wxMUTEX_MISC_ERROR;
}
wxMutexError wxMutexInternal::TryLock()
{
int err = pthread_mutex_trylock(&m_mutex);