Ticket #69517: npth-patch-for-tiger-leopard.diff

File npth-patch-for-tiger-leopard.diff, 1.9 KB (added by ballapete (Peter "Pete" Dyballa), 8 months ago)

New patch for unnamed semaphores on Tiger and Leopard

  • configure.ac

    diff --git a/configure.ac b/configure.ac
    index 576a26e..0b789ee 100644
    a b case "${host}" in 
    175175    *-apple-darwin*)
    176176        AC_SEARCH_LIBS([dispatch_semaphore_create],[dispatch],
    177177        [AC_DEFINE([HAVE_LIB_DISPATCH],1,[Defined if we have libdispatch])])
     178        if test "$ac_cv_search_dispatch_semaphore_create" = no; then
     179           AC_DEFINE(HAVE_NO_POSIX_SEMAPHORE,1,[Define if no good semaphore.])
     180        fi
    178181        ;;
    179182    *-*-aix*)
    180183        have_fork_unsafe_semaphore=yes
  • src/npth.c

    diff --git a/src/npth.c b/src/npth.c
    index 044a291..c7b42df 100644
    a b sem_wait (sem_t *sem) 
    6262  dispatch_semaphore_wait (*sem, DISPATCH_TIME_FOREVER);
    6363  return 0;
    6464}
     65#elif HAVE_NO_POSIX_SEMAPHORE
     66/* Fallback implementation without POSIX semaphore,
     67   for a system like MacOS Tiger */
     68typedef struct {
     69  pthread_mutex_t mutex;
     70  pthread_cond_t cond;
     71  unsigned int value;
     72} sem_t;
     73
     74static int
     75sem_init (sem_t *sem, int is_shared, unsigned int value)
     76{
     77  int r;
     78
     79  (void)is_shared;              /* Not supported.  */
     80  r = pthread_mutex_init (&sem->mutex, NULL);
     81  if (r)
     82    return r;
     83  r = pthread_cond_init (&sem->cond, NULL);
     84  if (r)
     85    return r;
     86  sem->value = value;
     87  return 0;
     88}
     89
     90static int
     91sem_post (sem_t *sem)
     92{
     93  int r;
     94
     95  r = pthread_mutex_lock (&sem->mutex);
     96  if (r)
     97    return r;
     98
     99  sem->value++;
     100  pthread_cond_signal (&sem->cond);
     101
     102  r = pthread_mutex_unlock (&sem->mutex);
     103  if (r)
     104    return r;
     105  return 0;
     106}
     107
     108static int
     109sem_wait (sem_t *sem)
     110{
     111  int r;
     112
     113  r = pthread_mutex_lock (&sem->mutex);
     114  if (r)
     115    return r;
     116
     117  while (sem->value == 0)
     118    pthread_cond_wait (&sem->cond, &sem->mutex);
     119  sem->value--;
     120
     121  r = pthread_mutex_unlock (&sem->mutex);
     122  if (r)
     123    return r;
     124  return 0;
     125}
    65126#else
    66127# include <semaphore.h>
    67128#endif