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

Side by Side Diff: util/synchronization/semaphore.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
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/synchronization/semaphore.h"
16
17 #include <limits>
18
19 #include "base/logging.h"
20 #include "base/posix/eintr_wrapper.h"
21
22 namespace crashpad {
23
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)
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698