Fix pthread_cond_timedwait usage (#3925)

This commit is contained in:
Alexander Shabalin
2020-03-03 11:17:00 +03:00
committed by GitHub
parent 38380f0686
commit b339c950b5
4 changed files with 83 additions and 35 deletions
+4 -8
View File
@@ -27,7 +27,7 @@
#if WITH_WORKERS
#include <pthread.h>
#include <sys/time.h>
#include "PthreadUtils.h"
#endif
#if WITH_WORKERS
@@ -179,13 +179,9 @@ class CyclicCollector {
restart:
COLLECTOR_LOG("start cycle GC\n");
if (restartCount > 10 && !terminateCollector_) {
COLLECTOR_LOG("wait for some time to avoid GC trashing\n");
struct timeval tv;
struct timespec ts;
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);
COLLECTOR_LOG("wait for some time to avoid GC thrashing\n");
uint64_t nsDelta = 1000LL * 1000LL * (restartCount - 10);
WaitOnCondVar(&cond_, &lock_, nsDelta);
}
atomicSet(&mutatedAtomics_, 0);
visited.clear();
+42
View File
@@ -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;
}
+20
View File
@@ -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
+17 -27
View File
@@ -24,7 +24,7 @@
#if WITH_WORKERS
#include <pthread.h>
#include <sys/time.h>
#include "PthreadUtils.h"
#endif
#include "Alloc.h"
@@ -409,13 +409,9 @@ class State {
pthread_cond_wait(&cond_, &lock_);
return true;
}
struct timeval tv;
struct timespec ts;
gettimeofday(&tv, nullptr);
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);
uint64_t nsDelta = millis * 1000000LL;
WaitOnCondVar(&cond_, &lock_, nsDelta);
return true;
}
@@ -769,32 +765,26 @@ KLong Worker::checkDelayedLocked() {
bool Worker::waitForQueueLocked(KLong timeoutMicroseconds, KLong* remaining) {
while (queue_.size() == 0) {
KLong closestToRun = checkDelayedLocked();
if (closestToRun == 0) {
KLong closestToRunMicroseconds = checkDelayedLocked();
if (closestToRunMicroseconds == 0) {
continue;
}
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.
} else if (closestToRun > 0) {
struct timeval tv;
struct timespec ts;
gettimeofday(&tv, nullptr);
} else if (closestToRunMicroseconds > 0) {
// Protect from potential overflow, cutting at 10_000_000 seconds, aka 115 days.
if (closestToRun > 10LL * 1000 * 1000 * 1000 * 1000)
closestToRun = 10LL * 1000 * 1000 * 1000 * 1000;
KLong nsDelta = closestToRun * 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);
if (closestToRunMicroseconds > 10LL * 1000 * 1000 * 1000 * 1000)
closestToRunMicroseconds = 10LL * 1000 * 1000 * 1000 * 1000;
uint64_t nsDelta = closestToRunMicroseconds * 1000LL;
uint64_t microsecondsPassed = 0;
WaitOnCondVar(&cond_, &lock_, nsDelta, remaining ? &microsecondsPassed : nullptr);
if (remaining) {
struct timeval tvAfter;
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);
*remaining = timeoutMicroseconds - microsecondsPassed;
}
} else {
pthread_cond_wait(&cond_, &lock_);