0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-27 07:54:05 +01:00

ircd::ctx: Add yield() allowing other contexts to run before returning.

This commit is contained in:
Jason Volk 2016-10-20 00:54:42 -07:00
parent b289c63b99
commit e6c8025ae0
2 changed files with 19 additions and 0 deletions

View file

@ -62,6 +62,7 @@ bool notify(ctx &); // Increment the semaphore (onl
extern __thread struct ctx *current; // Always set to the currently running context or null
ctx &cur(); // Convenience for *current (try to use this instead)
void yield(); // Allow other contexts to run before returning.
void wait(); // Returns when context notified.
// Return remaining time if notified; or <= 0 if not, and timeout thrown on throw overloads

View file

@ -69,6 +69,24 @@ ctx::wait()
c.wait(); // now you're yielding with portals
}
void
ctx::yield()
{
bool done(false);
const auto restore([&done, &me(cur())]
{
done = true;
notify(me);
});
// All spurious notifications are ignored until `done`
ios->post(restore); do
{
wait();
}
while(!done);
}
ircd::ctx::context::context(const size_t &stack_sz,
std::function<void ()> func,
const enum flags &flags)