add optional millis to Wait(), fix waitNotified() time calculation

This commit is contained in:
John
2022-09-10 14:21:24 +02:00
parent 2b07439794
commit 327704ff3b
2 changed files with 15 additions and 7 deletions
+13 -6
View File
@@ -93,11 +93,18 @@ bool WaitThread::join() {
return Thread::join();
}
bool WaitThread::Wait(int seconds) {
bool WaitThread::Wait(int seconds, int millis) {
pthread_mutex_lock(&m_mutex);
struct timespec t;
clockGettime(&t);
t.tv_sec += seconds;
long newMillis = t.tv_nsec/1000000 + millis;
if (newMillis >= 1000) {
t.tv_sec += newMillis / 1000;
t.tv_nsec = (newMillis%1000) * 1000000; // rounds down to whole millis
} else {
t.tv_nsec += millis * 1000000;
}
pthread_cond_timedwait(&m_cond, &m_mutex, &t);
pthread_mutex_unlock(&m_mutex);
return isRunning();
@@ -120,11 +127,11 @@ bool NotifiableThread::waitNotified(int millis) {
if (!m_notified) {
struct timespec t;
clockGettime(&t);
t.tv_sec += millis / 1000000000;
t.tv_nsec += (millis % 1000000000) * 1000000;
if (t.tv_nsec > 1000000000) {
t.tv_sec++;
t.tv_nsec -= 1000000000;
t.tv_sec += millis / 1000;
t.tv_nsec += (millis % 1000) * 1000000;
if (t.tv_nsec >= 1000000000) {
t.tv_sec += t.tv_nsec / 1000000000;
t.tv_nsec %= 1000000000;
}
pthread_cond_timedwait(&m_cond, &m_mutex, &t);
}
+2 -1
View File
@@ -129,9 +129,10 @@ class WaitThread : public Thread {
/**
* Wait for the specified amount of time.
* @param seconds the number of seconds to wait.
* @param millis the optional number of milliseconds to wait.
* @return true if this @a WaitThread is still running and not yet stopped.
*/
bool Wait(int seconds);
bool Wait(int seconds, int millis = 0);
protected: