sem_timedwait.c 634 B

12345678910111213141516171819202122232425262728293031
  1. #include <semaphore.h>
  2. #include "pthread_impl.h"
  3. static void cleanup(void *p)
  4. {
  5. a_dec(p);
  6. }
  7. int sem_timedwait(sem_t *restrict sem, const struct timespec *restrict at)
  8. {
  9. pthread_testcancel();
  10. if (!sem_trywait(sem)) return 0;
  11. int spins = 100;
  12. while (spins-- && sem->__val[0] <= 0 && !sem->__val[1]) a_spin();
  13. while (sem_trywait(sem)) {
  14. int r;
  15. a_inc(sem->__val+1);
  16. a_cas(sem->__val, 0, -1);
  17. pthread_cleanup_push(cleanup, (void *)(sem->__val+1));
  18. r = __timedwait_cp(sem->__val, -1, CLOCK_REALTIME, at, sem->__val[2]);
  19. pthread_cleanup_pop(1);
  20. if (r && r != EINTR) {
  21. errno = r;
  22. return -1;
  23. }
  24. }
  25. return 0;
  26. }