Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(476)

Side by Side Diff: util/synchronization/semaphore_posix.cc

Issue 909263002: Add Semaphore::TimedWait() (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/synchronization/semaphore_mac.cc ('k') | util/synchronization/semaphore_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "util/misc/clock.h" 15 #include "util/synchronization/semaphore.h"
Robert Sesek 2015/02/10 21:57:07 The find-copies here sucks.
Mark Mentovai 2015/02/10 22:01:45 Robert Sesek wrote:
16 16
17 #include <time.h> 17 #include <errno.h>
18
19 #include <cmath>
18 20
19 #include "base/logging.h" 21 #include "base/logging.h"
20 #include "base/posix/eintr_wrapper.h" 22 #include "base/posix/eintr_wrapper.h"
21 #include "build/build_config.h"
22
23 namespace {
24
25 const uint64_t kNanosecondsPerSecond = 1E9;
26
27 } // namespace
28 23
29 namespace crashpad { 24 namespace crashpad {
30 25
31 #if !defined(OS_MACOSX) 26 #if !defined(OS_MACOSX)
32 27
33 uint64_t ClockMonotonicNanoseconds() { 28 Semaphore::Semaphore(int value) {
34 timespec now; 29 PCHECK(sem_init(&semaphore_, 0, value) == 0) << "sem_init";
35 int rv = clock_gettime(CLOCK_MONOTONIC, &now); 30 }
36 DPCHECK(rv == 0) << "clock_gettime";
37 31
38 return now.tv_sec * kNanosecondsPerSecond + now.tv_nsec; 32 Semaphore::~Semaphore() {
33 PCHECK(sem_destroy(&semaphore_) == 0) << "sem_destroy";
34 }
35
36 void Semaphore::Wait() {
37 PCHECK(HANDLE_EINTR(sem_wait(&semaphore_)) == 0) << "sem_wait";
38 }
39
40 bool Semaphore::TimedWait(double seconds) {
41 DCHECK_GE(seconds, 0.0);
42 timespec timeout;
43 timeout.tv_sec = seconds;
44 timeout.tv_nsec = (seconds - trunc(seconds)) * 1E9;
45
46 int rv = HANDLE_EINTR(sem_timedwait(&semaphore_, &timeout));
47 PCHECK(rv == 0 || errno == ETIMEDOUT) << "sem_timedwait";
48 return rv == 0;
49 }
50
51 void Semaphore::Signal() {
52 PCHECK(sem_post(&semaphore_) == 0) << "sem_post";
39 } 53 }
40 54
41 #endif 55 #endif
42 56
43 void SleepNanoseconds(uint64_t nanoseconds) {
44 timespec sleep_time;
45 sleep_time.tv_sec = nanoseconds / kNanosecondsPerSecond;
46 sleep_time.tv_nsec = nanoseconds % kNanosecondsPerSecond;
47 int rv = HANDLE_EINTR(nanosleep(&sleep_time, &sleep_time));
48 DPCHECK(rv == 0) << "nanosleep";
49 }
50
51 } // namespace crashpad 57 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/synchronization/semaphore_mac.cc ('k') | util/synchronization/semaphore_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698