Navigation


Changeset 878:901526bc034c in freeDiameter for tests/testfifo.c


Ignore:
Timestamp:
Nov 4, 2012, 1:18:48 AM (11 years ago)
Author:
Sebastien Decugis <sdecugis@freediameter.net>
Branch:
default
Phase:
public
Message:

Added simple implementation of barriers for Mac OS X

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/testfifo.c

    r768 r878  
    3838#include <limits.h>
    3939
     40/* Wrapper for pthread_barrier stuff on Mac OS X */
     41#ifndef HAVE_PTHREAD_BAR
     42
     43#define PTHREAD_BARRIER_SERIAL_THREAD 1
     44typedef struct {
     45        int count;
     46        int entered;
     47        int serial;
     48        pthread_mutex_t mutex;
     49        pthread_cond_t cond;
     50} pthread_barrier_t;
     51
     52int pthread_barrier_init(pthread_barrier_t * barrier, int * barrier_attr, int count)
     53{
     54        memset(barrier, 0, sizeof(pthread_barrier_t));
     55        barrier->count = count;
     56        pthread_mutex_init(&barrier->mutex, NULL);
     57        pthread_cond_init(&barrier->cond, NULL);
     58        return 0;
     59}
     60
     61int pthread_barrier_destroy(pthread_barrier_t * barrier)
     62{
     63        pthread_mutex_destroy(&barrier->mutex);
     64        pthread_cond_destroy(&barrier->cond);
     65        return 0;
     66}
     67
     68int pthread_barrier_wait(pthread_barrier_t * barrier)
     69{
     70        int ret = 0;
     71        int serial;
     72        pthread_mutex_lock(&barrier->mutex);
     73        serial = barrier->serial;
     74       
     75        /* first thread gets the special value */
     76        if (barrier->entered++ == 0)
     77                ret = PTHREAD_BARRIER_SERIAL_THREAD;
     78       
     79        /* Count was achieved? */
     80        if (barrier->entered == barrier->count) {
     81                /* Ok, increase serial, reset number of threads, and signal everyone */
     82                barrier->entered = 0;
     83                barrier->serial++;
     84                pthread_cond_broadcast(&barrier->cond);
     85        } else {
     86                do {
     87                        pthread_cond_wait(&barrier->cond, &barrier->mutex);
     88                } while (barrier->serial == serial);
     89                /* this protects against spurious wakes */
     90        }
     91        pthread_mutex_unlock(&barrier->mutex);
     92        return 0;
     93}
     94
     95#endif /* HAVE_PTHREAD_BAR */
     96
    4097/* Structure for testing threshold function */
    4198static struct thrh_test {
Note: See TracChangeset for help on using the changeset viewer.