🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB

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