> On 12 Apr 2016, at 19:17, Ted Unangst <t...@tedunangst.com> wrote: > > Kári Tristan Helgason wrote: >> I apologise in advance if the diff appears strange, I don't really know how >> to get cvs to do what I want. > > cvs diff -u helps a lot. :)
Thanks, let me try that again: Index: rthread.h =================================================================== RCS file: /cvs/src/lib/librthread/rthread.h,v retrieving revision 1.56 diff -u -p -r1.56 rthread.h --- rthread.h 2 Apr 2016 19:00:51 -0000 1.56 +++ rthread.h 12 Apr 2016 23:04:34 -0000 @@ -141,7 +141,8 @@ struct pthread_barrier { pthread_mutex_t mutex; pthread_cond_t cond; int threshold; - int sofar; + int in; + int out; int generation; }; Index: rthread_barrier.c =================================================================== RCS file: /cvs/src/lib/librthread/rthread_barrier.c,v retrieving revision 1.2 diff -u -p -r1.2 rthread_barrier.c --- rthread_barrier.c 23 Apr 2012 08:30:33 -0000 1.2 +++ rthread_barrier.c 12 Apr 2016 23:04:34 -0000 @@ -69,17 +69,24 @@ err: int pthread_barrier_destroy(pthread_barrier_t *barrier) { + int rc; pthread_barrier_t b; if (barrier == NULL || *barrier == NULL) return (EINVAL); + if (rc = pthread_mutex_lock(&(*barrier)->mutex)) + return (rc); + b = *barrier; - if (b->sofar > 0) + if (b->out > 0 || b->in > 0) { + pthread_mutex_unlock(&b->mutex); return (EBUSY); + } *barrier = NULL; + pthread_mutex_unlock(&b->mutex); pthread_mutex_destroy(&b->mutex); pthread_cond_destroy(&b->cond); free(b); @@ -103,11 +110,12 @@ pthread_barrier_wait(pthread_barrier_t * if ((rc = pthread_mutex_lock(&b->mutex))) goto cancel; - _rthread_debug(6, "sofar: %d, threshold: %d\n", b->sofar, b->threshold); - if (++b->sofar == b->threshold) { - b->sofar = 0; + _rthread_debug(6, "in: %d, threshold: %d\n", b->in, b->threshold); + if (++b->in == b->threshold) { + b->out = b->in - 1; + b->in = 0; b->generation++; - if ((rc = pthread_cond_broadcast(&b->cond))) + if ((rc = pthread_cond_signal(&b->cond))) goto err; done = 1; _rthread_debug(6, "threshold reached\n"); @@ -118,6 +126,9 @@ pthread_barrier_wait(pthread_barrier_t * if ((rc = pthread_cond_wait(&b->cond, &b->mutex))) goto err; } while (gen == b->generation); + b->out--; /* mark thread exit */ + if ((rc = pthread_cond_signal(&b->cond))) + goto err; } err: