diff --git a/emper/Dispatcher.hpp b/emper/Dispatcher.hpp index f40c747de504e88ecd275b176adc1f25d159f05b..077cb5d6531db12cdef800ac41421cd549c5bec7 100644 --- a/emper/Dispatcher.hpp +++ b/emper/Dispatcher.hpp @@ -56,5 +56,10 @@ public: return currentFiber; } + static bool isDispatchedControlFlow() { + const Fiber* fiber = getCurrentFiberPtr(); + return fiber != nullptr; + } + friend ContextManager; }; diff --git a/emper/Runtime.cpp b/emper/Runtime.cpp index 704d438ccfb90fe8c65f6148e63a668a086e75dd..ad848ce597083cd7a2a621636cdb6c4f2f531d3b 100644 --- a/emper/Runtime.cpp +++ b/emper/Runtime.cpp @@ -135,3 +135,25 @@ void Runtime::printLastRuntimeStats() { currentRuntime->printStats(); } +bool Runtime::inRuntime() { + return dispatcher.isDispatchedControlFlow(); +} + +void Runtime::executeAndWait(std::function<void()> f) { + if (inRuntime()) { + ABORT("Ca not use executeAndWait() from within the Runtime"); + } + + std::mutex fiberFinished; + fiberFinished.lock(); + + Fiber* fiber = Fiber::from([&] { + f(); + + fiberFinished.unlock(); + }); + + schedule(*fiber); + + fiberFinished.lock(); +} diff --git a/emper/Runtime.hpp b/emper/Runtime.hpp index 5b443b3ede3c69c59e87dcac59eabf38a2fa3f1d..414befab7e26b9e9dac1314c1ee774416f3eb20b 100644 --- a/emper/Runtime.hpp +++ b/emper/Runtime.hpp @@ -113,6 +113,10 @@ public: void printStats(); + bool inRuntime(); + + void executeAndWait(std::function<void()> f); + friend ContextManager; friend Scheduler; friend Dispatcher;