[m-rev.] for review: Handle sem_wait, sem_timedwait being interrupted by signal handlers.

Peter Wang novalazy at gmail.com
Wed Jul 5 17:28:32 AEST 2017


> diff --git a/runtime/mercury_thread.h b/runtime/mercury_thread.h
> index 32de40d..288abe7 100644
> --- a/runtime/mercury_thread.h
> +++ b/runtime/mercury_thread.h
> @@ -74,13 +74,18 @@
>      // generated code is considered stable, since the alternative versions do
>      // the same thing, but with debugging support enabled.
>  
> +    // MR_SEM_IS_EINTR is used to test if MR_SEM_WAIT or MR_SEM_TIMED_WAIT was
> +    // interrupted by a signal. We do not test errno when using libdispatch as
> +    // the manual page for dispatch_semaphore_wait does not mention errno nor
> +    // EINTR.
> +
>      #define MR_LOCK(lck, from)      pthread_mutex_lock((lck))
>      #define MR_UNLOCK(lck, from)    pthread_mutex_unlock((lck))
>  
>      #define MR_COND_SIGNAL(cnd, from)    pthread_cond_signal((cnd))
>      #define MR_COND_BROADCAST(cnd, from) pthread_cond_broadcast((cnd))
>      #define MR_COND_WAIT(cnd, mtx, from) pthread_cond_wait((cnd), (mtx))
> -    #define MR_COND_TIMED_WAIT(cond, mtx, abstime, from)                     \
> +    #define MR_COND_TIMED_WAIT(cond, mtx, abstime, from)                \
>          pthread_cond_timedwait((cond), (mtx), (abstime))
>  
>      #if defined(MR_USE_LIBDISPATCH)
> @@ -89,11 +94,13 @@
>        #define MR_SEM_POST(sem, from)  dispatch_semaphore_signal(*(sem))
>        #define MR_SEM_TIMED_WAIT(sem, abstime, from)                     \
>          dispatch_semaphore_wait(*(sem), dispatch_walltime((abstime), 0))
> +      #define MR_SEM_IS_EINTR(errno)    MR_FALSE
>      #else
>        #define MR_SEM_WAIT(sem, from)  sem_wait((sem))
>        #define MR_SEM_POST(sem, from)  sem_post((sem))
>        #define MR_SEM_TIMED_WAIT(sem, abstime, from)                     \
>          sem_timedwait((sem), (abstime))
> +      #define MR_SEM_IS_EINTR(errno)  (errno == EINTR)
>      #endif // !MR_USE_LIBDISPATCH
>  
>    #else // MR_DEBUG_THREADS

Oops, MR_SEM_IS_EINTR needs to be defined even when MR_DEBUG_THREADS is
enabled.

Peter


More information about the reviews mailing list