Implementing pthread barrier for Mac OS/X I have written this little thingie to fix a problem of missing pthread_barrier_t in Mac OS/X pthreads. Are there any issues with this code?

The header:

#ifndef PTHREAD_BARRIER_H
#define PTHREAD_BARRIER_H

#include &lt;pthread.h&gt;

#ifdef __APPLE__

#ifdef __cplusplus
extern "C" {
#endif

#if !defined(PTHREAD_BARRIER_SERIAL_THREAD)
# define PTHREAD_BARRIER_SERIAL_THREAD  (1)
#endif

#if !defined(PTHREAD_PROCESS_PRIVATE)
# define PTHREAD_PROCESS_PRIVATE    (42)
#endif
#if !defined(PTHREAD_PROCESS_SHARED)
# define PTHREAD_PROCESS_SHARED     (43)
#endif

typedef struct {
} pthread_barrierattr_t;

typedef struct {
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    unsigned int limit;
    unsigned int count;
    unsigned int phase;
} pthread_barrier_t;

int pthread_barrierattr_init(pthread_barrierattr_t *attr);
int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);

int pthread_barrierattr_getpshared(const pthread_barrierattr_t *restrict attr,
                   int *restrict pshared);
int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr,
                   int pshared);

int pthread_barrier_init(pthread_barrier_t *restrict barrier,
             const pthread_barrierattr_t *restrict attr,
             unsigned int count);
int pthread_barrier_destroy(pthread_barrier_t *barrier);

int pthread_barrier_wait(pthread_barrier_t *barrier);

#ifdef  __cplusplus
}
#endif

#endif /* __APPLE__ */

#endif /* PTHREAD_BARRIER_H */


The source file:

#include "pthread_barrier.h"

#include &lt;errno.h&gt;

#ifdef __APPLE__

#define __unused __attribute__((unused))

int
pthread_barrierattr_init(pthread_barrierattr_t *attr __unused)
{
    return 0;
}

int
pthread_barrierattr_destroy(pthread_barrierattr_t *attr __unused)
{
    return 0;
}

int
pthread_barrierattr_getpshared(const pthread_barrierattr_t *restrict attr __unused,
                   int *restrict pshared)
{
    *pshared = PTHREAD_PROCESS_PRIVATE;
    return 0;
}

int
pthread_barrierattr_setpshared(pthread_barrierattr_t *attr __unused,
                   int pshared)
{
    if (pshared != PTHREAD_PROCESS_PRIVATE) {
        errno = EINVAL;
        return -1;
    }
    return 0;
}

int
pthread_barrier_init(pthread_barrier_t *restrict barrier,
             const pthread_barrierattr_t *restrict attr __unused,
             unsigned count)
{
    if (count == 0) {
        errno = EINVAL;
        return -1;
    }

    if (pthread_mutex_init(&amp;barrier-&gt;mutex, 0) &lt; 0) {
        return -1;
    }
    if (pthread_cond_init(&amp;barrier-&gt;cond, 0) &lt; 0) {
        int errno_save = errno;
        pthread_mutex_destroy(&amp;barrier-&gt;mutex);
        errno = errno_save;
        return -1;
    }

    barrier-&gt;limit = count;
    barrier-&gt;count = 0;
    barrier-&gt;phase = 0;

    return 0;
}

int
pthread_barrier_destroy(pthread_barrier_t *barrier)
{
    pthread_mutex_destroy(&amp;barrier-&gt;mutex);
    pthread_cond_destroy(&amp;barrier-&gt;cond);
    return 0;
}

int
pthread_barrier_wait(pthread_barrier_t *barrier)
{
    pthread_mutex_lock(&amp;barrier-&gt;mutex);
    barrier-&gt;count++;
    if (barrier-&gt;count &gt;= barrier-&gt;limit) {
        barrier-&gt;phase++;
        barrier-&gt;count = 0;
        pthread_cond_broadcast(&amp;barrier-&gt;cond);
        pthread_mutex_unlock(&amp;barrier-&gt;mutex);
        return PTHREAD_BARRIER_SERIAL_THREAD;
    } else {
        unsigned phase = barrier-&gt;phase;
        do
            pthread_cond_wait(&amp;barrier-&gt;cond, &amp;barrier-&gt;mutex);
        while (phase == barrier-&gt;phase);
        pthread_mutex_unlock(&amp;barrier-&gt;mutex);
        return 0;
    }
}

#endif /* __APPLE__ */
No problems found
As they say, &quot;no news is good news&quot;.  I read your code all the way through with an eye towards finding any possible errors, but I didn't find anything to criticize.
Instead I'll point out the things I liked about your code.
Use of restrict keyword
I must admit, I don't use restrict but I should learn to, as it can only help the compiler.  I already advocate using const when appropriate, and this is similar.
Proper error checking
You check for errors and set errno appropriately.
Correct barrier operation
I tried to find any concurrency flaw in your barrier wait function but it looked correct.
The one weird thing
The one thing I thought was strange was that you used a do {} while loop without the curly braces:
    do
        pthread_cond_wait(&amp;barrier-&gt;cond, &amp;barrier-&gt;mutex);
    while (phase == barrier-&gt;phase);

I've never seen that before but I guess if you like to do that it's fine.  I think people throw in the curlies because it doesn't waste any lines, whereas with a while loop or an if block it would add an extra line:
    while (condition) {
        foo();
    }                       // &lt;-- Wasted line

    do {
        foo();
    } while (condition);    // &lt;-- No waste