mutilate/barrier.cc
Jacob Leverich 386320d266 Initial check-in.
This is extracted from another git repo.  This is the first release, and
the prior commit history is not terribly interesting, so I'm not going
to bother using filter-branch to try to cleanly isolate the history for
this tool.

Cheers.
2012-08-23 14:30:32 -07:00

34 lines
728 B
C++

#include <pthread.h>
#include "barrier.h"
int barrier_init(barrier_t *barrier,int needed)
{
barrier->needed = needed;
barrier->called = 0;
pthread_mutex_init(&barrier->mutex,NULL);
pthread_cond_init(&barrier->cond,NULL);
return 0;
}
int barrier_destroy(barrier_t *barrier)
{
pthread_mutex_destroy(&barrier->mutex);
pthread_cond_destroy(&barrier->cond);
return 0;
}
int barrier_wait(barrier_t *barrier)
{
pthread_mutex_lock(&barrier->mutex);
barrier->called++;
if (barrier->called == barrier->needed) {
barrier->called = 0;
pthread_cond_broadcast(&barrier->cond);
} else {
pthread_cond_wait(&barrier->cond,&barrier->mutex);
}
pthread_mutex_unlock(&barrier->mutex);
return 0;
}