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);
28 typename std::result_of<F()>::type kernel(F&& code)
30 typedef typename std::result_of<F()>::type R;
31 std::promise<R> promise;
32 simcall_run_kernel([&]{
34 promise.set_value(code());
37 promise.set_exception(std::current_exception());
40 return promise.get_future().get();
46 class ContextFactory {
51 ContextFactory(std::string name) : name_(std::move(name)) {}
52 virtual ~ContextFactory();
53 virtual Context* create_context(std::function<void()> code,
54 void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
55 virtual void run_all() = 0;
56 virtual Context* self();
57 std::string const& name() const
62 void declare_context(void* T, std::size_t size);
64 template<class T, class... Args>
65 T* new_context(Args&&... args)
67 T* context = new T(std::forward<Args>(args)...);
68 this->declare_context(context, sizeof(T));
75 std::function<void()> code_;
76 void_pfn_smxprocess_t cleanup_func_ = nullptr;
77 smx_process_t process_ = nullptr;
81 Context(std::function<void()> code,
82 void_pfn_smxprocess_t cleanup_func,
83 smx_process_t process);
92 smx_process_t process()
94 return this->process_;
96 void set_cleanup(void_pfn_smxprocess_t cleanup)
98 cleanup_func_ = cleanup;
104 virtual void suspend() = 0;