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 <cmath> 18 19 #include "base/logging.h" 20 21 namespace crashpad { 22 Semaphore(int value)23Semaphore::Semaphore(int value) 24 : semaphore_(dispatch_semaphore_create(value)) { 25 CHECK(semaphore_) << "dispatch_semaphore_create"; 26 } 27 ~Semaphore()28Semaphore::~Semaphore() { 29 dispatch_release(semaphore_); 30 } 31 Wait()32void Semaphore::Wait() { 33 CHECK_EQ(dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER), 0); 34 } 35 TimedWait(double seconds)36bool Semaphore::TimedWait(double seconds) { 37 DCHECK_GE(seconds, 0.0); 38 39 if (std::isinf(seconds)) { 40 Wait(); 41 return true; 42 } 43 44 const dispatch_time_t timeout = 45 dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC); 46 return dispatch_semaphore_wait(semaphore_, timeout) == 0; 47 } 48 Signal()49void Semaphore::Signal() { 50 dispatch_semaphore_signal(semaphore_); 51 } 52 53 } // namespace crashpad 54