-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathcritical_wait_posix.cpp
57 lines (42 loc) · 1.19 KB
/
critical_wait_posix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "critical_wait.h"
#include <sys/time.h>
// CriticalLock
CriticalLock::CriticalLock() {
pthread_mutex_init(&lock_, nullptr);
}
CriticalLock::~CriticalLock() {
pthread_mutex_destroy(&lock_);
}
void CriticalLock::Lock() {
pthread_mutex_lock(&lock_);
}
void CriticalLock::Unlock() {
pthread_mutex_unlock(&lock_);
}
// CriticalWait
CriticalWait::CriticalWait(CriticalLock* lock) : lock_(lock) {
pthread_cond_init(&cond_, nullptr);
}
CriticalWait::~CriticalWait() {
pthread_cond_destroy(&cond_);
}
void CriticalWait::Wait() {
pthread_cond_wait(&cond_, &lock_->lock_);
}
bool CriticalWait::Wait(unsigned int maxWaitMs) {
int sec = (int)(maxWaitMs / 1000);
int nsec = (maxWaitMs - sec * 1000) * 1000000; // convert to nsec
struct timeval tv;
struct timespec ts;
gettimeofday(&tv, nullptr);
ts.tv_sec = tv.tv_sec + sec;
ts.tv_nsec = nsec;
int res = pthread_cond_timedwait(&cond_, &lock_->lock_, &ts);
return res == 0;
}
void CriticalWait::WakeUp() {
pthread_cond_signal(&cond_);
}