1 /* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #ifndef SIMGRID_SIMIX_HPP
8 #define SIMGRID_SIMIX_HPP
17 #include <type_traits>
19 #include <xbt/function_types.h>
20 #include <simgrid/simix.h>
22 XBT_PUBLIC(void) simcall_run_kernel(std::function<void()> const& code);
27 template<class R, class F>
28 void fulfill_promise(std::promise<R>& promise, F&& code)
31 promise.set_value(code());
34 promise.set_exception(std::current_exception());
38 // special version for R=void because the previous code does not compile
41 void fulfill_promise(std::promise<void>& promise, F&& code)
48 promise.set_exception(std::current_exception());
53 typename std::result_of<F()>::type kernel(F&& code)
55 typedef typename std::result_of<F()>::type R;
56 std::promise<R> promise;
57 simcall_run_kernel([&]{
58 xbt_assert(SIMIX_is_maestro(), "Not in maestro");
59 fulfill_promise(promise, code);
61 return promise.get_future().get();
67 class ContextFactory {
72 ContextFactory(std::string name) : name_(std::move(name)) {}
73 virtual ~ContextFactory();
74 virtual Context* create_context(std::function<void()> code,
75 void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
76 virtual void run_all() = 0;
77 virtual Context* self();
78 std::string const& name() const
83 void declare_context(void* T, std::size_t size);
85 template<class T, class... Args>
86 T* new_context(Args&&... args)
88 T* context = new T(std::forward<Args>(args)...);
89 this->declare_context(context, sizeof(T));
96 std::function<void()> code_;
97 void_pfn_smxprocess_t cleanup_func_ = nullptr;
98 smx_process_t process_ = nullptr;
102 Context(std::function<void()> code,
103 void_pfn_smxprocess_t cleanup_func,
104 smx_process_t process);
109 bool has_code() const
113 smx_process_t process()
115 return this->process_;
117 void set_cleanup(void_pfn_smxprocess_t cleanup)
119 cleanup_func_ = cleanup;
125 virtual void suspend() = 0;