- unsigned int index = parmap->index++;
- if (index < xbt_dynar_length(parmap->data)) {
- return xbt_dynar_get_as(parmap->data, index, void*);
- }
- return nullptr;
-}
-
-static void xbt_parmap_work(xbt_parmap_t parmap)
-{
- unsigned int index = parmap->index++;
- while (index < xbt_dynar_length(parmap->data)){
- parmap->fun(xbt_dynar_get_as(parmap->data, index, void*));
- index = parmap->index++;
- }
-}
-
-/**
- * \brief Main function of a worker thread.
- * \param arg the parmap
- */
-static void *xbt_parmap_worker_main(void *arg)
-{
- xbt_parmap_thread_data_t data = static_cast<xbt_parmap_thread_data_t>(arg);
- xbt_parmap_t parmap = data->parmap;
- unsigned round = 0;
- smx_context_t context = SIMIX_context_new(std::function<void()>(), nullptr, nullptr);
- SIMIX_context_set_current(context);
-
- XBT_DEBUG("New worker thread created");
-
- /* Worker's main loop */
- while (1) {
- round++;
- parmap->worker_wait_f(parmap, round);
- if (parmap->status == XBT_PARMAP_WORK) {
- XBT_DEBUG("Worker %d got a job", data->worker_id);
-
- xbt_parmap_work(parmap);
- parmap->worker_signal_f(parmap);
-
- XBT_DEBUG("Worker %d has finished", data->worker_id);
- /* We are destroying the parmap */
- } else {
- delete context;
- xbt_free(data);
- return nullptr;
- }
- }
-}
-
-#if HAVE_FUTEX_H
-static void futex_wait(unsigned *uaddr, unsigned val)
-{
- XBT_VERB("Waiting on futex %p", uaddr);
- syscall(SYS_futex, uaddr, FUTEX_WAIT_PRIVATE, val, nullptr, nullptr, 0);
-}
-
-static void futex_wake(unsigned *uaddr, unsigned val)
-{
- XBT_VERB("Waking futex %p", uaddr);
- syscall(SYS_futex, uaddr, FUTEX_WAKE_PRIVATE, val, nullptr, nullptr, 0);
-}
-#endif
-
-/**
- * \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_posix_master_wait(xbt_parmap_t parmap)
-{
- xbt_os_mutex_acquire(parmap->done_mutex);
- if (parmap->thread_counter < parmap->num_workers) {
- /* wait for all workers to be ready */
- xbt_os_cond_wait(parmap->done_cond, parmap->done_mutex);
- }
- xbt_os_mutex_release(parmap->done_mutex);
-}
-
-/**
- * \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_posix_worker_signal(xbt_parmap_t parmap)
-{
- xbt_os_mutex_acquire(parmap->done_mutex);
- parmap->thread_counter++;
- if (parmap->thread_counter == parmap->num_workers) {
- /* all workers have finished, wake the controller */
- xbt_os_cond_signal(parmap->done_cond);
- }
- xbt_os_mutex_release(parmap->done_mutex);
-}
-
-/**
- * \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_posix_master_signal(xbt_parmap_t parmap)
-{
- xbt_os_mutex_acquire(parmap->ready_mutex);
- parmap->thread_counter = 1;
- parmap->work++;
- /* wake all workers */
- xbt_os_cond_broadcast(parmap->ready_cond);
- xbt_os_mutex_release(parmap->ready_mutex);
-}
-
-/**
- * \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_posix_worker_wait(xbt_parmap_t parmap, unsigned round)
-{
- xbt_os_mutex_acquire(parmap->ready_mutex);
- /* wait for more work */
- if (parmap->work != round) {
- xbt_os_cond_wait(parmap->ready_cond, parmap->ready_mutex);
- }
- xbt_os_mutex_release(parmap->ready_mutex);
-}
-
-#if HAVE_FUTEX_H
-/**
- * \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;
- }
-}
-#endif
-
-/**
- * \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_busy_master_wait(xbt_parmap_t parmap)
-{
- while (parmap->thread_counter < parmap->num_workers) {
- xbt_os_thread_yield();
- }
-}
-
-/**
- * \brief Ends the parmap: wakes the controller thread when all workers terminate.
- *
- * This function is called by all worker threads when they end.
- *
- * \param parmap a parmap
- */
-static void xbt_parmap_busy_worker_signal(xbt_parmap_t parmap)
-{
- __sync_add_and_fetch(&parmap->thread_counter, 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_busy_master_signal(xbt_parmap_t parmap)
-{
- parmap->thread_counter = 1;
- __sync_add_and_fetch(&parmap->work, 1);
-}
-
-/**
- * \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_busy_worker_wait(xbt_parmap_t parmap, unsigned round)
-{
- /* wait for more work */
- while (parmap->work != round) {
- xbt_os_thread_yield();