crl_winapi_semaphore.cpp 842 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include <crl/winapi/crl_winapi_semaphore.h>
  8. #ifdef CRL_USE_WINAPI
  9. #include <crl/winapi/crl_winapi_windows_h.h>
  10. namespace crl {
  11. auto semaphore::implementation::create() -> pointer {
  12. auto result = CreateSemaphore(nullptr, 0, 1, nullptr);
  13. if (!result) {
  14. std::terminate();
  15. }
  16. return result;
  17. }
  18. void semaphore::implementation::operator()(pointer value) {
  19. if (value) {
  20. CloseHandle(value);
  21. }
  22. };
  23. void semaphore::acquire() {
  24. WaitForSingleObject(_handle.get(), INFINITE);
  25. }
  26. void semaphore::release() {
  27. ReleaseSemaphore(_handle.get(), 1, nullptr);
  28. }
  29. } // namespace crl
  30. #endif // CRL_USE_WINAPI