summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Young <dyoung@hdfgroup.org>2020-02-27 17:27:45 (GMT)
committerDavid Young <dyoung@hdfgroup.org>2020-02-27 17:27:45 (GMT)
commit4be589813a8fc5e2c5313464b088d7d3c31a754d (patch)
tree3da06e8bc7b1bc322d90801363ed41cff561099a
parent11d22f3ea50d45796e110fe805a694692de91456 (diff)
downloadhdf5-4be589813a8fc5e2c5313464b088d7d3c31a754d.zip
hdf5-4be589813a8fc5e2c5313464b088d7d3c31a754d.tar.gz
hdf5-4be589813a8fc5e2c5313464b088d7d3c31a754d.tar.bz2
The first implementation seemed to allow for the possibility that a thread
could block at the barrier, wake and exit the barrier, re-acquire the barrier lock and increase `nentered` before the other blocked threads woke and checked `nentered % count == 0`. Then the other blocked threads would check `nentered % count == 0` and, finding it false, go back to sleep in the barrier. This new implementation waits for a looser condition to obtain so that threads don't go back to sleep in the barrier.
-rw-r--r--test/thread_id.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/test/thread_id.c b/test/thread_id.c
index 49e3710..e3cf42e 100644
--- a/test/thread_id.c
+++ b/test/thread_id.c
@@ -143,6 +143,7 @@ int
pthread_barrier_wait(pthread_barrier_t *barrier)
{
int rc;
+ uint64_t threshold;
if (barrier == NULL)
return EINVAL;
@@ -152,8 +153,14 @@ pthread_barrier_wait(pthread_barrier_t *barrier)
rc = EINVAL;
goto out;
}
+ /* Compute the release `threshold`. All threads entering with count = 5
+ * and `nentered` in [0, 4] should be released once `nentered` reaches 5:
+ * call 5 the release `threshold`. All threads entering with count = 5
+ * and `nentered` in [5, 9] should be released once `nentered` reaches 10.
+ */
+ threshold = (barrier->nentered / barrier->count + 1) * barrier->count;
barrier->nentered++;
- while (barrier->nentered % barrier->count != 0) {
+ while (barrier->nentered < threshold) {
if ((rc = pthread_cond_wait(&barrier->cv, &barrier->mtx)) != 0)
goto out;
}