Logo AND Algorithmique Numérique Distribuée

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