Fix pthread_cond_timedwait usage (#3925)
This commit is contained in:
committed by
GitHub
parent
38380f0686
commit
b339c950b5
@@ -27,7 +27,7 @@
|
|||||||
|
|
||||||
#if WITH_WORKERS
|
#if WITH_WORKERS
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <sys/time.h>
|
#include "PthreadUtils.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if WITH_WORKERS
|
#if WITH_WORKERS
|
||||||
@@ -179,13 +179,9 @@ class CyclicCollector {
|
|||||||
restart:
|
restart:
|
||||||
COLLECTOR_LOG("start cycle GC\n");
|
COLLECTOR_LOG("start cycle GC\n");
|
||||||
if (restartCount > 10 && !terminateCollector_) {
|
if (restartCount > 10 && !terminateCollector_) {
|
||||||
COLLECTOR_LOG("wait for some time to avoid GC trashing\n");
|
COLLECTOR_LOG("wait for some time to avoid GC thrashing\n");
|
||||||
struct timeval tv;
|
uint64_t nsDelta = 1000LL * 1000LL * (restartCount - 10);
|
||||||
struct timespec ts;
|
WaitOnCondVar(&cond_, &lock_, nsDelta);
|
||||||
long long nsDelta = 1000LL * 1000LL * (restartCount - 10);
|
|
||||||
ts.tv_nsec = (tv.tv_usec * 1000LL + nsDelta) % 1000000000LL;
|
|
||||||
ts.tv_sec = (tv.tv_sec * 1000000000LL + tv.tv_usec * 1000LL + nsDelta) / 1000000000LL ;
|
|
||||||
pthread_cond_timedwait(&cond_, &lock_, &ts);
|
|
||||||
}
|
}
|
||||||
atomicSet(&mutatedAtomics_, 0);
|
atomicSet(&mutatedAtomics_, 0);
|
||||||
visited.clear();
|
visited.clear();
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "PthreadUtils.h"
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
constexpr int64_t kNanosecondsInASecond = 1000000000LL;
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int WaitOnCondVar(
|
||||||
|
pthread_cond_t* cond,
|
||||||
|
pthread_mutex_t* mutex,
|
||||||
|
uint64_t timeoutNanoseconds,
|
||||||
|
uint64_t* microsecondsPassed) {
|
||||||
|
struct timeval tvBefore;
|
||||||
|
// TODO: Error reporting?
|
||||||
|
gettimeofday(&tvBefore, nullptr);
|
||||||
|
|
||||||
|
struct timespec ts;
|
||||||
|
const uint64_t nanoseconds = tvBefore.tv_usec * 1000LL + timeoutNanoseconds;
|
||||||
|
ts.tv_sec = tvBefore.tv_sec + nanoseconds / kNanosecondsInASecond;
|
||||||
|
ts.tv_nsec = nanoseconds % kNanosecondsInASecond;
|
||||||
|
auto result = pthread_cond_timedwait(cond, mutex, &ts);
|
||||||
|
|
||||||
|
if (microsecondsPassed) {
|
||||||
|
struct timeval tvAfter;
|
||||||
|
// TODO: Error reporting?
|
||||||
|
gettimeofday(&tvAfter, nullptr);
|
||||||
|
|
||||||
|
*microsecondsPassed = (tvAfter.tv_sec - tvBefore.tv_sec) * 1000000LL +
|
||||||
|
tvAfter.tv_usec - tvBefore.tv_usec;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef RUNTIME_PTHREAD_UTILS_H
|
||||||
|
#define RUNTIME_PTHREAD_UTILS_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
// Releases mutex and waits on cond for timeoutNanoseconds.
|
||||||
|
// Returns ETIMEDOUT if timeoutNanoseconds has passed.
|
||||||
|
int WaitOnCondVar(
|
||||||
|
pthread_cond_t* cond,
|
||||||
|
pthread_mutex_t* mutex,
|
||||||
|
uint64_t timeoutNanoseconds,
|
||||||
|
uint64_t* microsecondsPassed = nullptr);
|
||||||
|
|
||||||
|
#endif // RUNTIME_PTHREAD_UTILS_H
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
#if WITH_WORKERS
|
#if WITH_WORKERS
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <sys/time.h>
|
#include "PthreadUtils.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "Alloc.h"
|
#include "Alloc.h"
|
||||||
@@ -409,13 +409,9 @@ class State {
|
|||||||
pthread_cond_wait(&cond_, &lock_);
|
pthread_cond_wait(&cond_, &lock_);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
struct timeval tv;
|
|
||||||
struct timespec ts;
|
uint64_t nsDelta = millis * 1000000LL;
|
||||||
gettimeofday(&tv, nullptr);
|
WaitOnCondVar(&cond_, &lock_, nsDelta);
|
||||||
KLong nsDelta = millis * 1000LL * 1000LL;
|
|
||||||
ts.tv_nsec = (tv.tv_usec * 1000LL + nsDelta) % 1000000000LL;
|
|
||||||
ts.tv_sec = (tv.tv_sec * 1000000000LL + nsDelta) / 1000000000LL;
|
|
||||||
pthread_cond_timedwait(&cond_, &lock_, &ts);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -769,32 +765,26 @@ KLong Worker::checkDelayedLocked() {
|
|||||||
|
|
||||||
bool Worker::waitForQueueLocked(KLong timeoutMicroseconds, KLong* remaining) {
|
bool Worker::waitForQueueLocked(KLong timeoutMicroseconds, KLong* remaining) {
|
||||||
while (queue_.size() == 0) {
|
while (queue_.size() == 0) {
|
||||||
KLong closestToRun = checkDelayedLocked();
|
KLong closestToRunMicroseconds = checkDelayedLocked();
|
||||||
if (closestToRun == 0) {
|
if (closestToRunMicroseconds == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (timeoutMicroseconds >= 0) {
|
if (timeoutMicroseconds >= 0) {
|
||||||
closestToRun = (timeoutMicroseconds < closestToRun || closestToRun < 0) ? timeoutMicroseconds : closestToRun;
|
closestToRunMicroseconds = (timeoutMicroseconds < closestToRunMicroseconds || closestToRunMicroseconds < 0)
|
||||||
|
? timeoutMicroseconds
|
||||||
|
: closestToRunMicroseconds;
|
||||||
}
|
}
|
||||||
if (closestToRun == 0) {
|
if (closestToRunMicroseconds == 0) {
|
||||||
// Just no wait at all here.
|
// Just no wait at all here.
|
||||||
} else if (closestToRun > 0) {
|
} else if (closestToRunMicroseconds > 0) {
|
||||||
struct timeval tv;
|
|
||||||
struct timespec ts;
|
|
||||||
gettimeofday(&tv, nullptr);
|
|
||||||
// Protect from potential overflow, cutting at 10_000_000 seconds, aka 115 days.
|
// Protect from potential overflow, cutting at 10_000_000 seconds, aka 115 days.
|
||||||
if (closestToRun > 10LL * 1000 * 1000 * 1000 * 1000)
|
if (closestToRunMicroseconds > 10LL * 1000 * 1000 * 1000 * 1000)
|
||||||
closestToRun = 10LL * 1000 * 1000 * 1000 * 1000;
|
closestToRunMicroseconds = 10LL * 1000 * 1000 * 1000 * 1000;
|
||||||
KLong nsDelta = closestToRun * 1000LL;
|
uint64_t nsDelta = closestToRunMicroseconds * 1000LL;
|
||||||
ts.tv_nsec = (tv.tv_usec * 1000LL + nsDelta) % 1000000000LL;
|
uint64_t microsecondsPassed = 0;
|
||||||
ts.tv_sec = (tv.tv_sec * 1000000000LL + nsDelta) / 1000000000LL;
|
WaitOnCondVar(&cond_, &lock_, nsDelta, remaining ? µsecondsPassed : nullptr);
|
||||||
pthread_cond_timedwait(&cond_, &lock_, &ts);
|
|
||||||
if (remaining) {
|
if (remaining) {
|
||||||
struct timeval tvAfter;
|
*remaining = timeoutMicroseconds - microsecondsPassed;
|
||||||
gettimeofday(&tvAfter, nullptr);
|
|
||||||
KLong usBefore = tv.tv_sec * 1000000LL + tv.tv_usec;
|
|
||||||
KLong usAfter = tvAfter.tv_sec * 1000000LL + tvAfter.tv_usec;
|
|
||||||
*remaining = timeoutMicroseconds - (usAfter - usBefore);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pthread_cond_wait(&cond_, &lock_);
|
pthread_cond_wait(&cond_, &lock_);
|
||||||
|
|||||||
Reference in New Issue
Block a user