Skip to content

Commit

Permalink
Replace GCD semaphores with Mach semaphores on OSX
Browse files Browse the repository at this point in the history
On OSX, GCD (Grand Central Dispatch) semaphores cause issues such as
hangs and crashes:
1. thrstatetest failures: eclipse-openj9/openj9#3822,
eclipse-openj9/openj9#4046
2. vmRuntimeState failures:
eclipse-openj9/openj9#3878

Also, GCD semaphores are not signal-safe.

Mach semaphores resolve the hangs and crashes seen with GCD semaphores. 

Only named POSIX semaphores are available on OSX. Unnamed POSIX
sempahores are not available on OSX. On OSX, named POSIX semaphores are
signal safe, and they are implemented using Mach semaphores. So, it is
safe to assume that Mach semaphores are signal safe. Mach semaphores can
be considered as a substitute for unnamed POSIX semaphores.

Thus, GCD semaphores are replaced with Mach semaphores on OSX.

Fixes eclipse-openj9/openj9#3822
Fixes eclipse-openj9/openj9#3878
Fixes eclipse-openj9/openj9#4046

Signed-off-by: Babneet Singh <sbabneet@ca.ibm.com>
  • Loading branch information
babsingh committed Dec 24, 2018
1 parent 024f264 commit 254d927
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 37 deletions.
38 changes: 11 additions & 27 deletions include_core/unix/thrdsup.h
Expand Up @@ -71,25 +71,7 @@ typedef WRAPPER_TYPE (*WRAPPER_FUNC)(WRAPPER_ARG);
#include <semaphore.h>
typedef sem_t OSSEMAPHORE;
#elif defined(OSX) /* defined(LINUX) || defined(AIXPPC) */
/* Macros __has_extension and __has_feature macros are undefined
* when using GCC 4.2.1 on OSX 10.10 Yosemite. Compiler errors
* occur without these macros. error: missing binary operator
* before token "(". If __has_extension and __has_feature are
* undefined, then the stubs included below will be used. These
* stubs help resolve the compiler errors seen with GCC 4.2.1 on
* OSX 10.10 Yosemite.
*/
#if defined(__GNUC__)
#ifndef __has_extension
#define __has_extension(x) 0
#endif /* __has_extension */

#ifndef __has_feature
#define __has_feature(x) 0
#endif /* __has_feature */
#endif /* defined(__GNUC__) */
#include <dispatch/dispatch.h>
typedef dispatch_semaphore_t OSSEMAPHORE;
typedef semaphore_t OSSEMAPHORE;
#elif defined(J9ZOS390) /* defined(OSX) */
typedef struct zos_sem_t {
int count;
Expand Down Expand Up @@ -353,11 +335,10 @@ extern pthread_condattr_t *defaultCondAttr;

#if defined(LINUX) || defined(AIXPPC)
#define SEM_INIT(sm, pshrd, inval) \
(sem_init((sem_t*)(sm), pshrd, inval))
(sem_init((sem_t *)(sm), pshrd, inval))
#elif defined(OSX)
intptr_t dispatch_semaphore_init(j9sem_t s, int initValue);
#define SEM_INIT(sm, pshrd, inval) \
(dispatch_semaphore_init((sm),(inval)))
(semaphore_create(mach_task_self(), (semaphore_t *)(sm), SYNC_POLICY_FIFO, inval))
#elif defined(J9ZOS390)
#define SEM_INIT(sm,pshrd,inval) (sem_init_zos(sm, pshrd, inval))
#else /* defined(J9ZOS390) */
Expand All @@ -367,7 +348,10 @@ intptr_t dispatch_semaphore_init(j9sem_t s, int initValue);

#if defined(LINUX) || defined(AIXPPC)
#define SEM_DESTROY(sm) \
(sem_destroy((sem_t*)(sm)))
(sem_destroy((sem_t *)(sm)))
#elif defined(OSX)
#define SEM_DESTROY(sm) \
(semaphore_destroy(mach_task_self(), *(semaphore_t *)(sm)))
#elif defined(J9ZOS390)
#define SEM_DESTROY(sm) \
(sem_destroy_zos(sm))
Expand All @@ -385,10 +369,10 @@ intptr_t dispatch_semaphore_init(j9sem_t s, int initValue);

#if defined(LINUX) || defined(AIXPPC)
#define SEM_POST(smP) \
(sem_post((sem_t*)(smP)))
(sem_post((sem_t *)(smP)))
#elif defined(OSX)
#define SEM_POST(smP) \
(dispatch_semaphore_signal((dispatch_semaphore_t)(smP)))
(semaphore_signal(*(semaphore_t *)(smP)))
#elif defined(J9ZOS390)
#define SEM_POST(smP) (sem_post_zos(smP))
#else
Expand All @@ -398,10 +382,10 @@ intptr_t dispatch_semaphore_init(j9sem_t s, int initValue);

#if defined(LINUX) || defined(AIXPPC)
#define SEM_WAIT(smP) \
(sem_wait((sem_t*)(smP)))
(sem_wait((sem_t *)(smP)))
#elif defined(OSX)
#define SEM_WAIT(smP) \
(dispatch_semaphore_wait((dispatch_semaphore_t)(smP), DISPATCH_TIME_FOREVER))
(semaphore_wait(*(semaphore_t *)(smP)))
#elif defined(J9ZOS390)
#define SEM_WAIT(smP) (sem_wait_zos(smP))
#else
Expand Down
10 changes: 0 additions & 10 deletions thread/unix/thrdsup.c
Expand Up @@ -263,16 +263,6 @@ sem_trywait_zos(j9sem_t s)

#endif

#if defined(OSX)
intptr_t
dispatch_semaphore_init(j9sem_t s, int initValue)
{
dispatch_semaphore_t *sem = (dispatch_semaphore_t *)s;
*sem = dispatch_semaphore_create(initValue);
return 0;
}
#endif /* defined(OSX) */

#if J9THREAD_USE_MONOTONIC_COND_CLOCK
/**
* Called by omrthread library init to set the cond clock to CLOCK_MONOTONIC.
Expand Down

0 comments on commit 254d927

Please sign in to comment.