- 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)
- {
- xbt_os_mutex_acquire(barrier->mutex);
-
- barrier->thread_count++;
- if(barrier->thread_count < barrier->threads_to_wait){
- xbt_os_cond_wait(barrier->cond,barrier->mutex);
- }else{
- barrier->thread_count = 0;
- xbt_os_cond_broadcast(barrier->cond);
- }
- xbt_os_mutex_release(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_start(xbt_parmap_t parmap)
+{
+ int myflag = parmap->done;
+ __sync_fetch_and_add(&parmap->thread_counter, 1);
+ if (parmap->thread_counter < parmap->num_workers) {
+ /* wait for all workers to be ready */
+ futex_wait(&parmap->done, myflag);
+ }
+}
+
+/**
+ * \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_end(xbt_parmap_t parmap)
+{
+ unsigned int mycount;
+
+ mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
+ if (mycount == parmap->num_workers) {
+ /* all workers have finished, wake the controller */
+ parmap->done++;
+ futex_wake(&parmap->done, 1);
+ }
+}
+
+/**
+ * \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_signal(xbt_parmap_t parmap)
+{
+ int myflag = parmap->done;
+ parmap->thread_counter = 0;
+ parmap->work++;
+
+ /* wake all workers */
+ futex_wake(&parmap->work, parmap->num_workers);
+
+ if (parmap->status == XBT_PARMAP_WORK) {
+ /* also work myself */
+ void* work = xbt_parmap_next(parmap);
+ while (work != NULL) {
+ parmap->fun(work);
+ work = xbt_parmap_next(parmap);
+ }
+ }
+
+ unsigned int mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
+ if (mycount < parmap->num_workers) {
+ /* some workers have not finished yet */
+ futex_wait(&parmap->done, myflag);
+ }
+}
+
+/**
+ * \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
+ */
+static void xbt_parmap_futex_wait(xbt_parmap_t parmap)
+{
+ int myflag;
+ unsigned int mycount;
+
+ myflag = parmap->work;
+ mycount = __sync_add_and_fetch(&parmap->thread_counter, 1);
+ if (mycount == parmap->num_workers) {
+ /* all workers have finished, wake the controller */
+ parmap->done++;
+ futex_wake(&parmap->done, 1);
+ }
+
+ /* wait for more work */
+ futex_wait(&parmap->work, myflag);
+}