semaphore_win.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include "util/synchronization/semaphore.h"
  15. #include <cmath>
  16. #include <limits>
  17. #include "base/logging.h"
  18. namespace crashpad {
  19. Semaphore::Semaphore(int value)
  20. : semaphore_(CreateSemaphore(nullptr,
  21. value,
  22. std::numeric_limits<LONG>::max(),
  23. nullptr)) {
  24. PCHECK(semaphore_) << "CreateSemaphore";
  25. }
  26. Semaphore::~Semaphore() {
  27. PCHECK(CloseHandle(semaphore_));
  28. }
  29. void Semaphore::Wait() {
  30. PCHECK(WaitForSingleObject(semaphore_, INFINITE) == WAIT_OBJECT_0);
  31. }
  32. bool Semaphore::TimedWait(double seconds) {
  33. DCHECK_GE(seconds, 0.0);
  34. if (std::isinf(seconds)) {
  35. Wait();
  36. return true;
  37. }
  38. DWORD rv = WaitForSingleObject(semaphore_, static_cast<DWORD>(seconds * 1E3));
  39. PCHECK(rv == WAIT_OBJECT_0 || rv == WAIT_TIMEOUT) << "WaitForSingleObject";
  40. return rv == WAIT_OBJECT_0;
  41. }
  42. void Semaphore::Signal() {
  43. PCHECK(ReleaseSemaphore(semaphore_, 1, nullptr));
  44. }
  45. } // namespace crashpad