mirror of
https://github.com/matrix-construct/construct
synced 2024-11-25 16:22:35 +01:00
ircd::ctx: Add yield() allowing other contexts to run before returning.
This commit is contained in:
parent
b289c63b99
commit
e6c8025ae0
2 changed files with 19 additions and 0 deletions
|
@ -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
|
||||
|
|
18
ircd/ctx.cc
18
ircd/ctx.cc
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue