"Fossies" - the Fresh Open Source Software Archive

Member "vfplot-1.0.15/src/libvfplot/pthreadextra.c" (6 Nov 2019, 2289 Bytes) of package /linux/privat/vfplot-1.0.15.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pthreadextra.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2   This is taken from Aleksey Demakov [1] (with just cosmetic
    3   changes) implementing the pthread barrier functionality
    4   missing from OSX.  The licence is BSD 2-Clause "Simplified"
    5 
    6   [1] https://github.com/ademakov/DarwinPthreadBarrier
    7 */
    8 
    9 #include "pthreadextra.h"
   10 
   11 #include <errno.h>
   12 
   13 #ifdef __GNUC__
   14 #define __unused __attribute__((unused))
   15 #else
   16 #define __unused
   17 #endif
   18 
   19 int pthread_barrierattr_init(pthread_barrierattr_t *attr __unused)
   20 {
   21     return 0;
   22 }
   23 
   24 int pthread_barrierattr_destroy(pthread_barrierattr_t *attr __unused)
   25 {
   26     return 0;
   27 }
   28 
   29 int pthread_barrierattr_getpshared(const pthread_barrierattr_t *restrict attr __unused,
   30                                    int *restrict pshared)
   31 {
   32     *pshared = PTHREAD_PROCESS_PRIVATE;
   33     return 0;
   34 }
   35 
   36 int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr __unused, int pshared)
   37 {
   38   if (pshared != PTHREAD_PROCESS_PRIVATE)
   39     {
   40       errno = EINVAL;
   41       return -1;
   42     }
   43   return 0;
   44 }
   45 
   46 int pthread_barrier_init(pthread_barrier_t *restrict barrier,
   47                          const pthread_barrierattr_t *restrict attr __unused,
   48                          unsigned count)
   49 {
   50   if (count == 0)
   51     {
   52       errno = EINVAL;
   53       return -1;
   54     }
   55 
   56   if (pthread_mutex_init(&barrier->mutex, 0) < 0)
   57     {
   58       return -1;
   59     }
   60 
   61   if (pthread_cond_init(&barrier->cond, 0) < 0)
   62     {
   63       int errno_save = errno;
   64       pthread_mutex_destroy(&barrier->mutex);
   65       errno = errno_save;
   66       return -1;
   67     }
   68 
   69   barrier->limit = count;
   70   barrier->count = 0;
   71   barrier->phase = 0;
   72 
   73   return 0;
   74 }
   75 
   76 int pthread_barrier_destroy(pthread_barrier_t *barrier)
   77 {
   78   pthread_mutex_destroy(&barrier->mutex);
   79   pthread_cond_destroy(&barrier->cond);
   80   return 0;
   81 }
   82 
   83 int pthread_barrier_wait(pthread_barrier_t *barrier)
   84 {
   85   pthread_mutex_lock(&barrier->mutex);
   86   barrier->count++;
   87   if (barrier->count >= barrier->limit)
   88     {
   89       barrier->phase++;
   90       barrier->count = 0;
   91       pthread_cond_broadcast(&barrier->cond);
   92       pthread_mutex_unlock(&barrier->mutex);
   93       return PTHREAD_BARRIER_SERIAL_THREAD;
   94     }
   95   else
   96     {
   97       unsigned phase = barrier->phase;
   98       do
   99         pthread_cond_wait(&barrier->cond, &barrier->mutex);
  100       while (phase == barrier->phase);
  101       pthread_mutex_unlock(&barrier->mutex);
  102       return 0;
  103     }
  104 }