Logo AND Algorithmique Numérique Distribuée

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