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

Side by Side Diff: util/synchronization/semaphore_mac.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
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/synchronization/semaphore.h" 15 #include "util/synchronization/semaphore.h"
16 16
17 #include <limits>
18
19 #include "base/logging.h" 17 #include "base/logging.h"
20 #include "base/posix/eintr_wrapper.h"
21 18
22 namespace crashpad { 19 namespace crashpad {
23 20
24 #if defined(OS_MACOSX)
25
26 Semaphore::Semaphore(int value) 21 Semaphore::Semaphore(int value)
27 : semaphore_(dispatch_semaphore_create(value)) { 22 : semaphore_(dispatch_semaphore_create(value)) {
28 CHECK(semaphore_) << "dispatch_semaphore_create"; 23 CHECK(semaphore_) << "dispatch_semaphore_create";
29 } 24 }
30 25
31 Semaphore::~Semaphore() { 26 Semaphore::~Semaphore() {
32 dispatch_release(semaphore_); 27 dispatch_release(semaphore_);
33 } 28 }
34 29
35 void Semaphore::Wait() { 30 void Semaphore::Wait() {
36 CHECK_EQ(dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER), 0); 31 CHECK_EQ(dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER), 0);
37 } 32 }
38 33
34 bool Semaphore::TimedWait(double seconds) {
35 DCHECK_GE(seconds, 0.0);
36 const dispatch_time_t timeout =
37 dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC);
38 return dispatch_semaphore_wait(semaphore_, timeout) == 0;
39 }
40
39 void Semaphore::Signal() { 41 void Semaphore::Signal() {
40 dispatch_semaphore_signal(semaphore_); 42 dispatch_semaphore_signal(semaphore_);
41 } 43 }
42 44
43 #elif defined(OS_WIN)
44
45 Semaphore::Semaphore(int value)
46 : semaphore_(CreateSemaphore(nullptr,
47 value,
48 std::numeric_limits<LONG>::max(),
49 nullptr)) {
50 PCHECK(semaphore_) << "CreateSemaphore";
51 }
52
53 Semaphore::~Semaphore() {
54 PCHECK(CloseHandle(semaphore_));
55 }
56
57 void Semaphore::Wait() {
58 PCHECK(WaitForSingleObject(semaphore_, INFINITE) == WAIT_OBJECT_0);
59 }
60
61 void Semaphore::Signal() {
62 PCHECK(ReleaseSemaphore(semaphore_, 1, nullptr));
63 }
64
65 #else
66
67 Semaphore::Semaphore(int value) {
68 PCHECK(sem_init(&semaphore_, 0, value) == 0) << "sem_init";
69 }
70
71 Semaphore::~Semaphore() {
72 PCHECK(sem_destroy(&semaphore_)) << "sem_destroy";
73 }
74
75 void Semaphore::Wait() {
76 PCHECK(HANDLE_EINTR(sem_wait(&semaphore_))) << "sem_wait";
77 }
78
79 void Semaphore::Signal() {
80 PCHECK(sem_post(&semaphore_)) << "sem_post";
81 }
82
83 #endif
84
85 } // namespace crashpad 45 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698