2424
2525#include " os/os_specific.h"
2626
27+ #include " common/common.h"
28+
29+ #include < errno.h>
30+ #include < semaphore.h>
2731#include < time.h>
2832#include < unistd.h>
33+ #include < pthread.h>
2934
3035double Timing::GetTickFrequency ()
3136{
@@ -41,4 +46,78 @@ uint64_t Timing::GetTick()
4146
4247void Threading::SetCurrentThreadName (const rdcstr &name)
4348{
49+ // only substantial difference from linux_threading.cpp
50+ pthread_setname_np (pthread_self (), name.c_str ());
51+ }
52+
53+ uint32_t Threading::NumberOfCores ()
54+ {
55+ long ret = sysconf (_SC_NPROCESSORS_ONLN);
56+ if (ret <= 0 )
57+ return 1 ;
58+ return uint32_t (ret);
59+ }
60+
61+ namespace Threading
62+ {
63+
64+ // works for all posix except apple, hence being here
65+ struct PosixSemaphore : public Semaphore
66+ {
67+ ~PosixSemaphore () {}
68+
69+ sem_t h;
70+ };
71+
72+ Semaphore *Semaphore::Create ()
73+ {
74+ PosixSemaphore *sem = new PosixSemaphore ();
75+ int err = sem_init (&sem->h , 0 , 0 );
76+ // only documented errors are too large initial value (impossible for 0) or for shared semaphores
77+ // going wrong (we're not shared)
78+ RDCASSERT (err == 0 , (int )errno);
79+ return sem;
4480}
81+
82+ void Semaphore::Destroy ()
83+ {
84+ PosixSemaphore *sem = (PosixSemaphore *)this ;
85+ sem_destroy (&sem->h );
86+ delete sem;
87+ }
88+
89+ void Semaphore::Wake (uint32_t numToWake)
90+ {
91+ PosixSemaphore *sem = (PosixSemaphore *)this ;
92+ for (uint32_t i = 0 ; i < numToWake; i++)
93+ sem_post (&sem->h );
94+ }
95+
96+ void Semaphore::WaitForWake ()
97+ {
98+ PosixSemaphore *sem = (PosixSemaphore *)this ;
99+
100+ // handle extremely moronic stupid signal interruptions
101+ do
102+ {
103+ int ret = sem_wait (&sem->h );
104+
105+ if (ret == -1 )
106+ {
107+ if (errno == EINTR)
108+ continue ;
109+
110+ RDCWARN (" Semaphore wait failed: %d" , errno);
111+ }
112+ } while (false );
113+ }
114+
115+ Semaphore::Semaphore ()
116+ {
117+ }
118+
119+ Semaphore::~Semaphore ()
120+ {
121+ }
122+
123+ };
0 commit comments