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

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

Issue 797173003: win: implement Semaphore (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: pcheck Created 6 years 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.h ('k') | no next file » | 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/synchronization/semaphore.h" 15 #include "util/synchronization/semaphore.h"
16 16
17 #include <limits>
18
17 #include "base/logging.h" 19 #include "base/logging.h"
18 #include "base/posix/eintr_wrapper.h" 20 #include "base/posix/eintr_wrapper.h"
19 21
20 namespace crashpad { 22 namespace crashpad {
21 23
22 #if defined(OS_MACOSX) 24 #if defined(OS_MACOSX)
23 25
24 Semaphore::Semaphore(int value) 26 Semaphore::Semaphore(int value)
25 : semaphore_(dispatch_semaphore_create(value)) { 27 : semaphore_(dispatch_semaphore_create(value)) {
26 CHECK(semaphore_) << "dispatch_semaphore_create"; 28 CHECK(semaphore_) << "dispatch_semaphore_create";
27 } 29 }
28 30
29 Semaphore::~Semaphore() { 31 Semaphore::~Semaphore() {
30 dispatch_release(semaphore_); 32 dispatch_release(semaphore_);
31 } 33 }
32 34
33 void Semaphore::Wait() { 35 void Semaphore::Wait() {
34 CHECK_EQ(dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER), 0); 36 CHECK_EQ(dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER), 0);
35 } 37 }
36 38
37 void Semaphore::Signal() { 39 void Semaphore::Signal() {
38 dispatch_semaphore_signal(semaphore_); 40 dispatch_semaphore_signal(semaphore_);
39 } 41 }
40 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
41 #else 65 #else
42 66
43 Semaphore::Semaphore(int value) { 67 Semaphore::Semaphore(int value) {
44 PCHECK(sem_init(&semaphore_, 0, value) == 0) << "sem_init"; 68 PCHECK(sem_init(&semaphore_, 0, value) == 0) << "sem_init";
45 } 69 }
46 70
47 Semaphore::~Semaphore() { 71 Semaphore::~Semaphore() {
48 PCHECK(sem_destroy(&semaphore_)) << "sem_destroy"; 72 PCHECK(sem_destroy(&semaphore_)) << "sem_destroy";
49 } 73 }
50 74
51 void Semaphore::Wait() { 75 void Semaphore::Wait() {
52 PCHECK(HANDLE_EINTR(sem_wait(&semaphore_))) << "sem_wait"; 76 PCHECK(HANDLE_EINTR(sem_wait(&semaphore_))) << "sem_wait";
53 } 77 }
54 78
55 void Semaphore::Signal() { 79 void Semaphore::Signal() {
56 PCHECK(sem_post(&semaphore_)) << "sem_post"; 80 PCHECK(sem_post(&semaphore_)) << "sem_post";
57 } 81 }
58 82
59 #endif 83 #endif
60 84
61 } // namespace crashpad 85 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/synchronization/semaphore.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698