int sem_wait(sem_t * sem) { if (!sem || *sem == -1) { errno = EINVAL; return -1; } if (waitone(*sem, INFINITE) < 0) return -1; return 0; }
void enter(critsect_t cs) { tid_t tid = threadid(); if (cs->owner == tid) { cs->recursion++; } else { if (atomic_add(&cs->count, 1) > 0) while (waitone(cs->event, INFINITE) != 0); cs->owner = tid; } }
int sem_trywait(sem_t *sem) { if (!sem || *sem == -1) { errno = EINVAL; return -1; } if (waitone(*sem, 0) < 0) { errno = EAGAIN; return -1; } return 0; }
int sem_timedwait(sem_t *sem, const struct timespec *abstime) { struct timeval curtime; long timeout; if (!sem || *sem == -1) { errno = EINVAL; return -1; } if (gettimeofday(&curtime, NULL) < 0) return -1; timeout = ((long) (abstime->tv_sec - curtime.tv_sec) * 1000L + (long)((abstime->tv_nsec / 1000) - curtime.tv_usec) / 1000L); if (timeout < 0) timeout = 0L; if (waitone(*sem, timeout) < 0) return -1; return 0; }