-
-# ifndef LISP_FEATURE_DARWIN
-
-static inline void
-os_sem_init(os_sem_t *sem, unsigned int value)
-{
- if (-1==sem_init(sem, 0, value))
- lose("os_sem_init(%p, %u): %s", sem, value, strerror(errno));
- FSHOW((stderr, "os_sem_init(%p, %u)\n", sem, value));
-}
-
-static inline void
-os_sem_wait(os_sem_t *sem, char *what)
-{
- FSHOW((stderr, "%s: os_sem_wait(%p) ...\n", what, sem));
- while (-1 == sem_wait(sem))
- if (EINTR!=errno)
- lose("%s: os_sem_wait(%p): %s", what, sem, strerror(errno));
- FSHOW((stderr, "%s: os_sem_wait(%p) => ok\n", what, sem));
-}
-
-static inline void
-os_sem_post(sem_t *sem, char *what)
-{
- if (-1 == sem_post(sem))
- lose("%s: os_sem_post(%p): %s", what, sem, strerror(errno));
- FSHOW((stderr, "%s: os_sem_post(%p)\n", what, sem));
-}
-
-static inline void
-os_sem_destroy(os_sem_t *sem)
-{
- if (-1==sem_destroy(sem))
- lose("os_sem_destroy(%p): %s", sem, strerror(errno));
-}
-
-# else
-
-static inline void
-os_sem_init(os_sem_t *sem, unsigned int value)
-{
- if (KERN_SUCCESS!=semaphore_create(current_mach_task, sem, SYNC_POLICY_FIFO, (int)value))
- lose("os_sem_init(%p): %s", sem, strerror(errno));
-}
-
-static inline void
-os_sem_wait(os_sem_t *sem, char *what)
-{
- kern_return_t ret;
- restart:
- FSHOW((stderr, "%s: os_sem_wait(%p)\n", what, sem));
- ret = semaphore_wait(*sem);
- FSHOW((stderr, "%s: os_sem_wait(%p) => %s\n", what, sem,
- KERN_SUCCESS==ret ? "ok" : strerror(errno)));
- switch (ret) {
- case KERN_SUCCESS:
- return;
- case KERN_OPERATION_TIMED_OUT:
- fprintf(stderr, "%s: os_sem_wait(%p): %s", what, sem, strerror(errno));
- goto restart;
- default:
- lose("%s: os_sem_wait(%p): %s", what, sem, strerror(errno));
- }
-}
-
-static inline void
-os_sem_post(os_sem_t *sem, char *what)
-{
- if (KERN_SUCCESS!=semaphore_signal(*sem))
- lose("%s: os_sem_post(%p): %s", what, sem, strerror(errno));
- FSHOW((stderr, "%s: os_sem_post(%p) ok\n", what, sem));
-}
-
-static inline void
-os_sem_destroy(os_sem_t *sem)
-{
- if (-1==semaphore_destroy(current_mach_task, *sem))
- lose("os_sem_destroy(%p): %s", sem, strerror(errno));
-}
-
-# endif
-
-/* Only access thread state with blockables blocked. */
-static inline lispobj
-thread_state(struct thread *thread)
-{
- lispobj state;
- sigset_t old;
- block_blockable_signals(NULL, &old);
- os_sem_wait(thread->state_sem, "thread_state");
- state = thread->state;
- os_sem_post(thread->state_sem, "thread_state");
- thread_sigmask(SIG_SETMASK, &old, NULL);
- return state;
-}
-
-static inline void
-set_thread_state(struct thread *thread, lispobj state)
-{
- int i, waitcount = 0;
- sigset_t old;
- block_blockable_signals(NULL, &old);
- os_sem_wait(thread->state_sem, "set_thread_state");
- if (thread->state != state) {
- if ((STATE_STOPPED==state) ||
- (STATE_DEAD==state)) {
- waitcount = thread->state_not_running_waitcount;
- thread->state_not_running_waitcount = 0;
- for (i=0; i<waitcount; i++)
- os_sem_post(thread->state_not_running_sem, "set_thread_state (not running)");
- }
- if ((STATE_RUNNING==state) ||
- (STATE_DEAD==state)) {
- waitcount = thread->state_not_stopped_waitcount;
- thread->state_not_stopped_waitcount = 0;
- for (i=0; i<waitcount; i++)
- os_sem_post(thread->state_not_stopped_sem, "set_thread_state (not stopped)");
- }
- thread->state = state;
- }
- os_sem_post(thread->state_sem, "set_thread_state");
- thread_sigmask(SIG_SETMASK, &old, NULL);
-}
-
-static inline void
-wait_for_thread_state_change(struct thread *thread, lispobj state)
-{
- sigset_t old;
- os_sem_t *wait_sem;
- block_blockable_signals(NULL, &old);
- start:
- os_sem_wait(thread->state_sem, "wait_for_thread_state_change");
- if (thread->state == state) {
- switch (state) {
- case STATE_RUNNING:
- wait_sem = thread->state_not_running_sem;
- thread->state_not_running_waitcount++;
- break;
- case STATE_STOPPED:
- wait_sem = thread->state_not_stopped_sem;
- thread->state_not_stopped_waitcount++;
- break;
- default:
- lose("Invalid state in wait_for_thread_state_change: "OBJ_FMTX"\n", state);
- }
- } else {
- wait_sem = NULL;
- }
- os_sem_post(thread->state_sem, "wait_for_thread_state_change");
- if (wait_sem) {
- os_sem_wait(wait_sem, "wait_for_thread_state_change");
- goto start;
- }
- thread_sigmask(SIG_SETMASK, &old, NULL);
-}
-