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 fulfill_promise(promise, code);
60 return promise.get_future().get();
66 class ContextFactory {
71 ContextFactory(std::string name) : name_(std::move(name)) {}
72 virtual ~ContextFactory();
73 virtual Context* create_context(std::function<void()> code,
74 void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
75 virtual void run_all() = 0;
76 virtual Context* self();
77 std::string const& name() const
82 void declare_context(void* T, std::size_t size);
84 template<class T, class... Args>
85 T* new_context(Args&&... args)
87 T* context = new T(std::forward<Args>(args)...);
88 this->declare_context(context, sizeof(T));
95 std::function<void()> code_;
96 void_pfn_smxprocess_t cleanup_func_ = nullptr;
97 smx_process_t process_ = nullptr;
101 Context(std::function<void()> code,
102 void_pfn_smxprocess_t cleanup_func,
103 smx_process_t process);
108 bool has_code() const
112 smx_process_t process()
114 return this->process_;
116 void set_cleanup(void_pfn_smxprocess_t cleanup)
118 cleanup_func_ = cleanup;
124 virtual void suspend() = 0;