threads.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef AL_THREADS_H
  2. #define AL_THREADS_H
  3. #if defined(__GNUC__) && defined(__i386__)
  4. /* force_align_arg_pointer is required for proper function arguments aligning
  5. * when SSE code is used. Some systems (Windows, QNX) do not guarantee our
  6. * thread functions will be properly aligned on the stack, even though GCC may
  7. * generate code with the assumption that it is. */
  8. #define FORCE_ALIGN __attribute__((force_align_arg_pointer))
  9. #else
  10. #define FORCE_ALIGN
  11. #endif
  12. #ifdef _WIN32
  13. #define WIN32_LEAN_AND_MEAN
  14. #include <windows.h>
  15. #elif defined(__APPLE__)
  16. #include <dispatch/dispatch.h>
  17. #else
  18. #include <semaphore.h>
  19. #endif
  20. void althrd_setname(const char *name);
  21. namespace al {
  22. class semaphore {
  23. #ifdef _WIN32
  24. using native_type = HANDLE;
  25. #elif defined(__APPLE__)
  26. using native_type = dispatch_semaphore_t;
  27. #else
  28. using native_type = sem_t;
  29. #endif
  30. native_type mSem;
  31. public:
  32. semaphore(unsigned int initial=0);
  33. semaphore(const semaphore&) = delete;
  34. ~semaphore();
  35. semaphore& operator=(const semaphore&) = delete;
  36. void post();
  37. void wait() noexcept;
  38. bool try_wait() noexcept;
  39. };
  40. } // namespace al
  41. #endif /* AL_THREADS_H */