alsem.h 969 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #ifndef COMMON_ALSEM_H
  2. #define COMMON_ALSEM_H
  3. #if defined(__APPLE__)
  4. #include <AvailabilityMacros.h>
  5. #include <TargetConditionals.h>
  6. #if (((MAC_OS_X_VERSION_MIN_REQUIRED > 1050) && !defined(__ppc__)) || TARGET_OS_IOS || TARGET_OS_TV)
  7. #include <dispatch/dispatch.h>
  8. #define AL_APPLE_HAVE_DISPATCH 1
  9. #else
  10. #include <semaphore.h> /* Fallback option for Apple without a working libdispatch */
  11. #endif
  12. #elif !defined(_WIN32)
  13. #include <semaphore.h>
  14. #endif
  15. namespace al {
  16. class semaphore {
  17. #ifdef _WIN32
  18. using native_type = void*;
  19. #elif defined(AL_APPLE_HAVE_DISPATCH)
  20. using native_type = dispatch_semaphore_t;
  21. #else
  22. using native_type = sem_t;
  23. #endif
  24. native_type mSem{};
  25. public:
  26. semaphore(unsigned int initial=0);
  27. semaphore(const semaphore&) = delete;
  28. ~semaphore();
  29. semaphore& operator=(const semaphore&) = delete;
  30. void post();
  31. void wait() noexcept;
  32. bool try_wait() noexcept;
  33. };
  34. } // namespace al
  35. #endif /* COMMON_ALSEM_H */