+sem_t pthread_pause_sem;
+
+void pthread_nanosleep(struct timespec t) {
+ //Sleep calls on Linux get interrupted by signals, causing premature wake
+ //Pthread (un)pause is built using signals
+ //Therefore we need self-restarting sleep implementation
+ //IO timeouts are restarted by SA_RESTART, but sleeps do need explicit restart
+ while(nanosleep(&t, &t)) if(errno!=EINTR) break;
+ return;
+}
+
+void pthread_nsleep(time_t s, long ns) {
+ struct timespec t;
+ t.tv_sec = s;
+ t.tv_nsec = ns;
+ pthread_nanosleep(t);
+}
+
+void pthread_sleep(time_t s) {
+ pthread_nsleep(s, 0);
+}
+
+void pthread_pause_yield() {
+ //Call this to give other threads chance to run
+ sem_wait(&pthread_pause_sem);
+ sem_post(&pthread_pause_sem);
+ //usleep(0);
+ //nanosleep(&((const struct timespec){.tv_sec=0,.tv_nsec=1}), NULL);
+ pthread_nsleep(0,1); //pthread_yield() is not enough, so we use sleep
+ pthread_yield();
+}