Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Encapsulate main function, argc and argv in a closure
[simgrid.git] / src / simix / ThreadContext.cpp
1 /* Copyright (c) 2009-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/function_types.h"
8 #include "smx_private.h"
9 #include "src/portable.h"           /* loads context system definitions */
10 #include "xbt/swag.h"
11 #include "xbt/xbt_os_thread.h"
12 #include "src/xbt_modinter.h"       /* prototype of os thread module's init/exit in XBT */
13
14 #include "src/simix/ThreadContext.hpp"
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
17
18 static xbt_os_sem_t smx_ctx_thread_sem = nullptr;
19
20 namespace simgrid {
21 namespace simix {
22
23 XBT_PRIVATE ContextFactory* thread_factory()
24 {
25   XBT_VERB("Activating thread context factory");
26   return new ThreadContextFactory();
27 }
28
29 ThreadContextFactory::ThreadContextFactory()
30   : ContextFactory("ThreadContextFactory")
31 {
32   if (SIMIX_context_is_parallel()) {
33     smx_ctx_thread_sem = xbt_os_sem_init(SIMIX_context_get_nthreads());
34   } else {
35     smx_ctx_thread_sem = nullptr;
36   }
37 }
38
39 ThreadContextFactory::~ThreadContextFactory()
40 {
41   if (smx_ctx_thread_sem) {
42     xbt_os_sem_destroy(smx_ctx_thread_sem);
43     smx_ctx_thread_sem = nullptr;
44   }
45 }
46
47 ThreadContext* ThreadContextFactory::create_context(
48     std::function<void()> code,
49     void_pfn_smxprocess_t cleanup, smx_process_t process)
50 {
51   return this->new_context<ThreadContext>(std::move(code), cleanup, process);
52 }
53
54 ThreadContext::ThreadContext(std::function<void()> code,
55     void_pfn_smxprocess_t cleanup, smx_process_t process)
56   : Context(std::move(code), cleanup, process)
57 {
58   /* If the user provided a function for the process then use it
59      otherwise is the context for maestro */
60   if (has_code()) {
61     this->begin_ = xbt_os_sem_init(0);
62     this->end_ = xbt_os_sem_init(0);
63     if (smx_context_stack_size_was_set)
64       xbt_os_thread_setstacksize(smx_context_stack_size);
65     if (smx_context_guard_size_was_set)
66       xbt_os_thread_setguardsize(smx_context_guard_size);
67
68     /* create and start the process */
69     /* NOTE: The first argument to xbt_os_thread_create used to be the process *
70     * name, but now the name is stored at SIMIX level, so we pass a null  */
71     this->thread_ =
72       xbt_os_thread_create(NULL, ThreadContext::wrapper, this, this);
73
74     /* wait the starting of the newly created process */
75     xbt_os_sem_acquire(this->end_);
76
77   } else {
78     xbt_os_thread_set_extra_data(this);
79   }
80 }
81
82 void ThreadContextFactory::run_all()
83 {
84   if (smx_ctx_thread_sem == nullptr) {
85     // Serial execution
86     smx_process_t process;
87     unsigned int cursor;
88     xbt_dynar_foreach(simix_global->process_to_run, cursor, process) {
89       XBT_DEBUG("Handling %p",process);
90       ThreadContext* context = static_cast<ThreadContext*>(process->context);
91       xbt_os_sem_release(context->begin_);
92       xbt_os_sem_acquire(context->end_);
93     }
94   } else {
95     // Parallel execution
96     unsigned int index;
97     smx_process_t process;
98     xbt_dynar_foreach(simix_global->process_to_run, index, process)
99       xbt_os_sem_release(static_cast<ThreadContext*>(process->context)->begin_);
100     xbt_dynar_foreach(simix_global->process_to_run, index, process)
101        xbt_os_sem_acquire(static_cast<ThreadContext*>(process->context)->end_);
102   }
103 }
104
105 ThreadContext* ThreadContextFactory::self()
106 {
107   return static_cast<ThreadContext*>(xbt_os_thread_get_extra_data());
108 }
109
110 ThreadContext::~ThreadContext()
111 {
112   /* check if this is the context of maestro (it doesn't have a real thread) */
113   if (this->thread_) {
114     /* wait about the thread terminason */
115     xbt_os_thread_join(this->thread_, nullptr);
116
117     /* destroy the synchronisation objects */
118     xbt_os_sem_destroy(this->begin_);
119     xbt_os_sem_destroy(this->end_);
120   }
121 }
122
123 void *ThreadContext::wrapper(void *param)
124 {
125   ThreadContext* context = static_cast<ThreadContext*>(param);
126
127 #ifndef WIN32
128   /* Install alternate signal stack, for SIGSEGV handler. */
129   stack_t stack;
130   stack.ss_sp = sigsegv_stack;
131   stack.ss_size = sizeof sigsegv_stack;
132   stack.ss_flags = 0;
133   sigaltstack(&stack, nullptr);
134 #endif
135   /* Tell the maestro we are starting, and wait for its green light */
136   xbt_os_sem_release(context->end_);
137   xbt_os_sem_acquire(context->begin_);
138   if (smx_ctx_thread_sem)       /* parallel run */
139     xbt_os_sem_acquire(smx_ctx_thread_sem);
140
141   (*context)();
142   context->stop();
143   return nullptr;
144 }
145
146 void ThreadContext::stop()
147 {
148   Context::stop();
149   if (smx_ctx_thread_sem)
150     xbt_os_sem_release(smx_ctx_thread_sem);
151
152   // Signal to the maestro that it has finished:
153   xbt_os_sem_release(this->end_);
154
155   xbt_os_thread_exit(NULL);
156 }
157
158 void ThreadContext::suspend()
159 {
160   if (smx_ctx_thread_sem)
161     xbt_os_sem_release(smx_ctx_thread_sem);
162   xbt_os_sem_release(this->end_);
163   xbt_os_sem_acquire(this->begin_);
164   if (smx_ctx_thread_sem)
165     xbt_os_sem_acquire(smx_ctx_thread_sem);
166 }
167
168 }
169 }