thread/Cond: add method timed_wait()
This commit is contained in:
parent
0954f580fa
commit
c5c43c4541
|
@ -32,6 +32,8 @@
|
||||||
|
|
||||||
#include "PosixMutex.hxx"
|
#include "PosixMutex.hxx"
|
||||||
|
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Low-level wrapper for a pthread_cond_t.
|
* Low-level wrapper for a pthread_cond_t.
|
||||||
*/
|
*/
|
||||||
|
@ -55,6 +57,17 @@ public:
|
||||||
void wait(PosixMutex &mutex) {
|
void wait(PosixMutex &mutex) {
|
||||||
pthread_cond_wait(&cond, &mutex.mutex);
|
pthread_cond_wait(&cond, &mutex.mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool timed_wait(PosixMutex &mutex, unsigned timeout_ms) {
|
||||||
|
struct timeval now;
|
||||||
|
gettimeofday(&now, nullptr);
|
||||||
|
|
||||||
|
struct timespec ts;
|
||||||
|
ts.tv_sec = now.tv_sec + timeout_ms / 1000;
|
||||||
|
ts.tv_nsec = (now.tv_usec + (timeout_ms % 1000) * 1000) * 1000;
|
||||||
|
|
||||||
|
return pthread_cond_timedwait(&cond, &mutex.mutex, &ts) == 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -54,9 +54,13 @@ public:
|
||||||
WakeAllConditionVariable(&cond);
|
WakeAllConditionVariable(&cond);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool timed_wait(CriticalSection &mutex, DWORD timeout_ms) {
|
||||||
|
return SleepConditionVariableCS(&cond, &mutex.critical_section,
|
||||||
|
timeout_ms);
|
||||||
|
}
|
||||||
|
|
||||||
void wait(CriticalSection &mutex) {
|
void wait(CriticalSection &mutex) {
|
||||||
SleepConditionVariableCS(&cond, &mutex.critical_section,
|
timed_wait(mutex, INFINITE);
|
||||||
INFINITE);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue