1 /* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_SIMIX_UNIX_CONTEXT_HPP
7 #define SIMGRID_SIMIX_UNIX_CONTEXT_HPP
9 #include <ucontext.h> /* context relative declarations */
16 #include <simgrid/simix.hpp>
17 #include <xbt/parmap.hpp>
18 #include <xbt/xbt_os_thread.h>
20 #include "Context.hpp"
21 #include "src/internal_config.h"
22 #include "src/simix/smx_private.hpp"
28 class UContext : public Context {
30 UContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
33 virtual void resume() = 0;
35 static void swap(UContext* from, UContext* to) { swapcontext(&from->uc_, &to->uc_); }
36 static UContext* getMaestro() { return maestro_context_; }
37 static void setMaestro(UContext* maestro) { maestro_context_ = maestro; }
40 static UContext* maestro_context_;
41 void* stack_ = nullptr; /* the thread stack */
42 ucontext_t uc_; /* the ucontext that executes the code */
44 static void wrapper(int, int);
45 static void make_ctx(ucontext_t* ucp, void (*func)(int, int), UContext* arg);
48 class SerialUContext : public UContext {
50 SerialUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
51 : UContext(std::move(code), cleanup_func, process)
54 void suspend() override;
55 void resume() override;
57 static void run_all();
60 static unsigned long process_index_;
63 #if HAVE_THREAD_CONTEXTS
64 class ParallelUContext : public UContext {
66 ParallelUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
67 : UContext(std::move(code), cleanup_func, process)
70 void suspend() override;
71 void resume() override;
73 static void initialize();
74 static void finalize();
75 static void run_all();
78 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
79 static std::vector<ParallelUContext*> workers_context_;
80 static std::atomic<uintptr_t> threads_working_;
81 static xbt_os_thread_key_t worker_id_key_;
85 class UContextFactory : public ContextFactory {
88 ~UContextFactory() override;
89 Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
90 void run_all() override;