+ThreadContext* ThreadContextFactory::attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process)
+{
+ return this->new_context<ThreadContext>(
+ std::function<void()>(), cleanup_func, process, false);
+}
+
+ThreadContext* ThreadContextFactory::create_maestro(std::function<void()> code, smx_process_t process)
+{
+ return this->new_context<ThreadContext>(std::move(code), nullptr, process, true);
+}
+
+ThreadContext::ThreadContext(std::function<void()> code,
+ void_pfn_smxprocess_t cleanup, smx_process_t process, bool maestro)
+ : AttachContext(std::move(code), cleanup, process)
+{
+ // We do not need the semaphores when maestro is in main:
+ // if (!(maestro && !code)) {
+ this->begin_ = xbt_os_sem_init(0);
+ this->end_ = xbt_os_sem_init(0);
+ // }
+
+ /* If the user provided a function for the process then use it */
+ if (has_code()) {
+ if (smx_context_stack_size_was_set)
+ xbt_os_thread_setstacksize(smx_context_stack_size);
+ if (smx_context_guard_size_was_set)
+ xbt_os_thread_setguardsize(smx_context_guard_size);
+
+ /* create and start the process */
+ /* NOTE: The first argument to xbt_os_thread_create used to be the process *
+ * name, but now the name is stored at SIMIX level, so we pass a null */
+ this->thread_ =
+ xbt_os_thread_create(NULL,
+ maestro ? ThreadContext::maestro_wrapper : ThreadContext::wrapper,
+ this, this);
+ /* wait the starting of the newly created process */
+ xbt_os_sem_acquire(this->end_);
+ }
+
+ /* Otherwise, we attach to the current thread */
+ else {
+ xbt_os_thread_set_extra_data(this);
+ }
+}
+