- void xbt_barrier_wait(xbt_barrier_t barrier)
- {
- int myflag = 0;
- unsigned int mycount = 0;
-
- myflag = barrier->futex;
- mycount = __sync_add_and_fetch(&barrier->thread_count, 1);
- if(mycount < barrier->threads_to_wait){
- futex_wait(&barrier->futex, myflag);
- }else{
- barrier->futex = __sync_add_and_fetch(&barrier->futex, 1);
- barrier->thread_count = 0;
- futex_wake(&barrier->futex, barrier->threads_to_wait);
- }
- }
-#else
- void xbt_barrier_wait(xbt_barrier_t barrier)
- {
- int myflag = 0;
- unsigned int mycount = 0;
-
- xbt_os_mutex_acquire(barrier->mutex);
- //pthread_mutex_lock(&barrier->mutex);
-
- barrier->thread_count++;
- if(barrier->thread_count < barrier->threads_to_wait){
- xbt_os_cond_wait(barrier->cond,barrier->mutex);
- //pthread_cond_wait(&barrier->mutex,&barrier->cond);
- }else{
- barrier->thread_count = 0;
- xbt_os_cond_broadcast(barrier->cond);
- //pthread_cond_broadcast(&barrier->mutex, &barrier->cond);
- }
- xbt_os_mutex_release(barrier->mutex);
- //pthread_mutex_unlock(&barrier->mutex);
- }
+/**
+ * \brief Starts the parmap: waits for all workers to be ready and returns.
+ *
+ * This function is called by the controller thread.
+ *
+ * \param parmap a parmap
+ */
+static void xbt_parmap_futex_master_wait(xbt_parmap_t parmap)
+{
+ unsigned count = parmap->thread_counter;
+ while (count < parmap->num_workers) {
+ /* wait for all workers to be ready */
+ futex_wait(&parmap->thread_counter, count);
+ count = parmap->thread_counter;
+ }
+}
+
+/**
+ * \brief Ends the parmap: wakes the controller thread when all workers terminate.
+ *
+ * This function is called by all worker threads when they end (not including
+ * the controller).
+ *
+ * \param parmap a parmap
+ */
+static void xbt_parmap_futex_worker_signal(xbt_parmap_t parmap)
+{
+ unsigned count = __sync_add_and_fetch(&parmap->thread_counter, 1);
+ if (count == parmap->num_workers) {
+ /* all workers have finished, wake the controller */
+ futex_wake(&parmap->thread_counter, INT_MAX);
+ }
+}
+
+/**
+ * \brief Wakes all workers and waits for them to finish the tasks.
+ *
+ * This function is called by the controller thread.
+ *
+ * \param parmap a parmap
+ */
+static void xbt_parmap_futex_master_signal(xbt_parmap_t parmap)
+{
+ parmap->thread_counter = 1;
+ __sync_add_and_fetch(&parmap->work, 1);
+ /* wake all workers */
+ futex_wake(&parmap->work, INT_MAX);
+}
+
+/**
+ * \brief Waits for some work to process.
+ *
+ * This function is called by each worker thread (not including the controller)
+ * when it has no more work to do.
+ *
+ * \param parmap a parmap
+ * \param round the expected round number
+ */
+static void xbt_parmap_futex_worker_wait(xbt_parmap_t parmap, unsigned round)
+{
+ unsigned work = parmap->work;
+ /* wait for more work */
+ while (work != round) {
+ futex_wait(&parmap->work, work);
+ work = parmap->work;
+ }
+}