- if(_surf_parallel_contexts){
-#ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
- tpool = xbt_tpool_new(2, 10);
- (*factory)->runall = smx_ctx_raw_runall_parallel;
- (*factory)->self = smx_ctx_raw_self_parallel;
+ next_context = (smx_context_t)raw_workers_context[worker_id];
+ XBT_DEBUG("Restoring worker stack %lu (working threads = %lu)",
+ worker_id, raw_threads_working);
+ next_stack = ((smx_ctx_raw_t)next_context)->stack_top;
+ }
+
+ SIMIX_context_set_current(next_context);
+ raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, next_stack);
+#endif
+}
+
+/**
+ * \brief Resumes sequentially in the current worker thread the processes ready
+ * to run.
+ * \param first_process the first process to resume
+ */
+static void smx_ctx_raw_resume_parallel(smx_process_t first_process)
+{
+#ifdef CONTEXT_THREADS
+ unsigned long worker_id = __sync_fetch_and_add(&raw_threads_working, 1);
+ xbt_os_thread_set_specific(raw_worker_id_key, (void*) worker_id);
+ smx_ctx_raw_t worker_context = (smx_ctx_raw_t)SIMIX_context_self();
+ raw_workers_context[worker_id] = worker_context;
+ XBT_DEBUG("Saving worker stack %lu", worker_id);
+ raw_stack_t* worker_stack = &(worker_context)->stack_top;
+
+
+ smx_context_t context = first_process->context;
+ SIMIX_context_set_current(context);
+ raw_swapcontext(worker_stack, ((smx_ctx_raw_t) context)->stack_top);
+#endif
+}
+
+/**
+ * \brief Resumes in parallel all processes ready to run.
+ */
+static void smx_ctx_raw_runall_parallel(void)
+{
+#ifdef CONTEXT_THREADS
+ raw_threads_working = 0;
+ xbt_parmap_apply(raw_parmap, (void_f_pvoid_t) smx_ctx_raw_resume_parallel,
+ simix_global->process_to_run);