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