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 */
15 #include <simgrid/simix.hpp>
16 #include <xbt/parmap.hpp>
17 #include <xbt/xbt_os_thread.h>
19 #include "Context.hpp"
20 #include "src/internal_config.h"
21 #include "src/simix/smx_private.hpp"
29 class ParallelUContext;
30 class UContextFactory;
32 class UContext : public Context {
34 ucontext_t uc_; /* the ucontext that executes the code */
35 char* stack_ = nullptr; /* the thread stack */
37 friend UContextFactory;
38 UContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
41 static void swap(UContext* from, UContext* to) { swapcontext(&from->uc_, &to->uc_); }
44 class SerialUContext : public UContext {
46 SerialUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
47 : UContext(std::move(code), cleanup_func, process)
50 void suspend() override;
54 class ParallelUContext : public UContext {
56 ParallelUContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
57 : UContext(std::move(code), cleanup_func, process)
60 void suspend() override;
64 class UContextFactory : public ContextFactory {
67 friend SerialUContext;
68 friend ParallelUContext;
71 ~UContextFactory() override;
72 Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
73 void run_all() override;