| OLD | NEW |
| (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 <errno.h> |
| 18 |
| 19 #include <cmath> |
| 20 |
| 21 #include "base/logging.h" |
| 22 #include "base/posix/eintr_wrapper.h" |
| 23 |
| 24 namespace crashpad { |
| 25 |
| 26 #if !defined(OS_MACOSX) |
| 27 |
| 28 Semaphore::Semaphore(int value) { |
| 29 PCHECK(sem_init(&semaphore_, 0, value) == 0) << "sem_init"; |
| 30 } |
| 31 |
| 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"; |
| 53 } |
| 54 |
| 55 #endif |
| 56 |
| 57 } // namespace crashpad |
| OLD | NEW |