void JavaContext::start_hook()
{
- SIMIX_context_set_current(this); // We need to attach it also for maestro, in contrary to our ancestor
+ Context::set_current(this); // We need to attach it also for maestro, in contrary to our ancestor
//Attach the thread to the JVM
JNIEnv *env;
Parmap<T>& parmap = data->parmap;
unsigned round = 0;
smx_context_t context = SIMIX_context_new(std::function<void()>(), nullptr, nullptr);
- SIMIX_context_set_current(context);
+ kernel::context::Context::set_current(context);
XBT_CDEBUG(xbt_parmap, "New worker thread created");
namespace simgrid {
namespace kernel {
namespace context {
-static thread_local smx_context_t smx_current_context;
ContextFactoryInitializer factory_initializer = nullptr;
ContextFactory::~ContextFactory() = default;
-Context* ContextFactory::self()
+static thread_local smx_context_t smx_current_context;
+Context* Context::self()
{
return smx_current_context;
}
+void Context::set_current(Context* self)
+{
+ smx_current_context = self;
+}
void Context::declare_context(std::size_t size)
{
if (has_code())
this->cleanup_func_ = cleanup_func;
else
- SIMIX_context_set_current(this);
+ set_current(this);
}
Context::~Context() = default;
smx_context_t SIMIX_context_self()
{
if (simix_global && simix_global->context_factory)
- return simix_global->context_factory->self();
+ return simgrid::kernel::context::Context::self();
else
return nullptr;
}
-
-/**
- * @brief Sets the current context of this thread.
- * @param context the context to set
- */
-void SIMIX_context_set_current(smx_context_t context)
-{
- simgrid::kernel::context::smx_current_context = context;
-}
virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
virtual void run_all() = 0;
- /** @brief Returns the current context of this thread. */
- Context* self();
std::string const& name() const { return name_; }
protected:
void declare_context(std::size_t size);
public:
- class StopRequest {
- /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
- *
- * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
- * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
- */
- public:
- StopRequest() = default;
- explicit StopRequest(std::string msg) : msg_(msg) {}
-
- private:
- std::string msg_;
- };
bool iwannadie = false;
Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
Context(const Context&) = delete;
Context& operator=(const Context&) = delete;
+ virtual ~Context();
void operator()() { code_(); }
bool has_code() const { return static_cast<bool>(code_); }
smx_actor_t process() { return this->actor_; }
void set_cleanup(void_pfn_smxprocess_t cleanup) { cleanup_func_ = cleanup; }
- // Virtual methods
- virtual ~Context();
+ // Scheduling methods
virtual void stop();
virtual void suspend() = 0;
+
+ // Retrieving the self() context
+ /** @brief Retrives the current context of this thread */
+ static Context* self();
+ /** @brief Sets the current context of this thread */
+ static void set_current(Context* self);
+
+ class StopRequest {
+ /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
+ *
+ * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
+ * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
+ */
+ public:
+ StopRequest() = default;
+ explicit StopRequest(std::string msg) : msg_(msg) {}
+
+ private:
+ std::string msg_;
+ };
};
class XBT_PUBLIC AttachContext : public Context {
XBT_PRIVATE void *SIMIX_context_stack_new();
XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
-XBT_PUBLIC void SIMIX_context_set_current(smx_context_t context);
-
XBT_PUBLIC int SIMIX_process_get_maxpid();
XBT_PRIVATE void SIMIX_post_create_environment();
XBT_DEBUG("No more process to run");
next_context = static_cast<SerialBoostContext*>(BoostContext::get_maestro());
}
- SIMIX_context_set_current(next_context);
+ Context::set_current(next_context);
BoostContext::swap(this, next_context);
}
void SerialBoostContext::resume()
{
- SIMIX_context_set_current(this);
+ Context::set_current(this);
BoostContext::swap(BoostContext::get_maestro(), this);
}
next_context = workers_context_[worker_id_];
}
- SIMIX_context_set_current(next_context);
+ Context::set_current(next_context);
BoostContext::swap(this, next_context);
}
ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(SIMIX_context_self());
workers_context_[worker_id_] = worker_context;
- SIMIX_context_set_current(this);
+ Context::set_current(this);
BoostContext::swap(worker_context, this);
}
XBT_DEBUG("No more process to run");
next_context = static_cast<SerialRawContext*>(RawContext::get_maestro());
}
- SIMIX_context_set_current(next_context);
+ Context::set_current(next_context);
RawContext::swap(this, next_context);
}
void SerialRawContext::resume()
{
- SIMIX_context_set_current(this);
+ Context::set_current(this);
RawContext::swap(RawContext::get_maestro(), this);
}
XBT_DEBUG("Restoring worker stack %zu (working threads = %zu)", worker_id_, threads_working_.load());
}
- SIMIX_context_set_current(next_context);
+ Context::set_current(next_context);
RawContext::swap(this, next_context);
}
ParallelRawContext* worker_context = static_cast<ParallelRawContext*>(SIMIX_context_self());
workers_context_[worker_id_] = worker_context;
XBT_DEBUG("Saving worker stack %zu", worker_id_);
- SIMIX_context_set_current(this);
+ Context::set_current(this);
RawContext::swap(worker_context, this);
}
/* Otherwise, we attach to the current thread */
else {
- SIMIX_context_set_current(this);
+ Context::set_current(this);
}
}
void *ThreadContext::wrapper(void *param)
{
ThreadContext* context = static_cast<ThreadContext*>(param);
- SIMIX_context_set_current(context);
+ Context::set_current(context);
#ifndef WIN32
/* Install alternate signal stack, for SIGSEGV handler. */
ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
maestro->end_.acquire();
- SIMIX_context_set_current(nullptr);
+ Context::set_current(nullptr);
}
// SerialThreadContext
XBT_DEBUG("No more process to run");
next_context = static_cast<SerialUContext*>(UContext::get_maestro());
}
- SIMIX_context_set_current(next_context);
+ Context::set_current(next_context);
UContext::swap(this, next_context);
}
void SerialUContext::resume()
{
- SIMIX_context_set_current(this);
+ Context::set_current(this);
UContext::swap(UContext::get_maestro(), this);
}
// When given that soul, the body will wait for the next scheduling round
}
- SIMIX_context_set_current(next_context);
+ Context::set_current(next_context);
// Get the next soul to run, either simulated or initial minion's one:
UContext::swap(this, next_context);
}
// Write down that this soul is hosted in that body (for now)
workers_context_[worker_id_] = worker_context;
// Write in simix that I switched my soul
- SIMIX_context_set_current(this);
+ Context::set_current(this);
// Actually do that using the relevant library call:
UContext::swap(worker_context, this);
// No body runs that soul anymore at this point. Instead the current body took the soul of simulated process The