OLD | NEW |
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> | 17 #include <limits> |
18 | 18 |
19 #include "base/logging.h" | 19 #include "base/logging.h" |
20 #include "base/posix/eintr_wrapper.h" | |
21 | 20 |
22 namespace crashpad { | 21 namespace crashpad { |
23 | 22 |
24 #if defined(OS_MACOSX) | |
25 | |
26 Semaphore::Semaphore(int value) | |
27 : semaphore_(dispatch_semaphore_create(value)) { | |
28 CHECK(semaphore_) << "dispatch_semaphore_create"; | |
29 } | |
30 | |
31 Semaphore::~Semaphore() { | |
32 dispatch_release(semaphore_); | |
33 } | |
34 | |
35 void Semaphore::Wait() { | |
36 CHECK_EQ(dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER), 0); | |
37 } | |
38 | |
39 void Semaphore::Signal() { | |
40 dispatch_semaphore_signal(semaphore_); | |
41 } | |
42 | |
43 #elif defined(OS_WIN) | |
44 | |
45 Semaphore::Semaphore(int value) | 23 Semaphore::Semaphore(int value) |
46 : semaphore_(CreateSemaphore(nullptr, | 24 : semaphore_(CreateSemaphore(nullptr, |
47 value, | 25 value, |
48 std::numeric_limits<LONG>::max(), | 26 std::numeric_limits<LONG>::max(), |
49 nullptr)) { | 27 nullptr)) { |
50 PCHECK(semaphore_) << "CreateSemaphore"; | 28 PCHECK(semaphore_) << "CreateSemaphore"; |
51 } | 29 } |
52 | 30 |
53 Semaphore::~Semaphore() { | 31 Semaphore::~Semaphore() { |
54 PCHECK(CloseHandle(semaphore_)); | 32 PCHECK(CloseHandle(semaphore_)); |
55 } | 33 } |
56 | 34 |
57 void Semaphore::Wait() { | 35 void Semaphore::Wait() { |
58 PCHECK(WaitForSingleObject(semaphore_, INFINITE) == WAIT_OBJECT_0); | 36 PCHECK(WaitForSingleObject(semaphore_, INFINITE) == WAIT_OBJECT_0); |
59 } | 37 } |
60 | 38 |
| 39 bool Semaphore::TimedWait(double seconds) { |
| 40 DCHECK_GE(seconds, 0.0); |
| 41 DWORD rv = WaitForSingleObject(semaphore_, static_cast<DWORD>(seconds * 1E3)); |
| 42 PCHECK(rv == WAIT_OBJECT_0 || rv == WAIT_TIMEOUT) << "WaitForSingleObject"; |
| 43 return rv == WAIT_OBJECT_0; |
| 44 } |
| 45 |
61 void Semaphore::Signal() { | 46 void Semaphore::Signal() { |
62 PCHECK(ReleaseSemaphore(semaphore_, 1, nullptr)); | 47 PCHECK(ReleaseSemaphore(semaphore_, 1, nullptr)); |
63 } | 48 } |
64 | 49 |
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 | 50 } // namespace crashpad |
OLD | NEW |