From deb34d3fb4990997101e37f54a334e3f72e77540 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 26 Nov 2018 18:37:23 -0800 Subject: libpulp: add pulp_reset() It's useful to be able to wipe out a pulp instance but reuse its allocations as a fresh pulp instance. This also fixes a context leak in pulp_free() by making it call pulp_reset(). (only relevant to win32 where destroy_context() does something) --- src/pulp.c | 31 +++++++++++++++++++++++++++++++ src/pulp.h | 1 + 2 files changed, 32 insertions(+) (limited to 'src') diff --git a/src/pulp.c b/src/pulp.c index f57f077..7972dac 100644 --- a/src/pulp.c +++ b/src/pulp.c @@ -352,6 +352,35 @@ pulp_t * pulp_new(void) } +/* reset a pulp scheduler instance for reuse */ +void pulp_reset(pulp_t *pulp) +{ + pulp_fiber_t *f; + + assert(pulp); + + pulp->exited = 0; + pulp->now = now(); + + /* XXX: Note the idle queue isn't currently utilized */ + list_for_each_entry(f, &pulp->fibers.idle, fibers) + destroy_context(&f->context); + list_splice_init(&pulp->fibers.idle, &pulp->fibers.free); + + /* The run queue is drained per-tick, but newly created fibers + * with 0 delay go directly on the run queue. So it's possible + * there will be some there outside a tick. + */ + list_for_each_entry(f, &pulp->fibers.run, fibers) + destroy_context(&f->context); + list_splice_init(&pulp->fibers.run, &pulp->fibers.free); + + list_for_each_entry(f, &pulp->fibers.sleep, fibers) + destroy_context(&f->context); + list_splice_init(&pulp->fibers.sleep, &pulp->fibers.free); +} + + /* free a pulp scheduler instance */ void pulp_free(pulp_t *pulp) { @@ -359,6 +388,8 @@ void pulp_free(pulp_t *pulp) assert(pulp); + pulp_reset(pulp); + list_for_each_entry_safe(alloc, _alloc, &pulp->allocs, allocs) free(alloc); diff --git a/src/pulp.h b/src/pulp.h index 9d5f559..cba78e6 100644 --- a/src/pulp.h +++ b/src/pulp.h @@ -32,6 +32,7 @@ typedef struct pulp_mailbox_t { int pulp_init(void); pulp_t * pulp_new(void); +void pulp_reset(pulp_t *pulp); void pulp_free(pulp_t *pulp); int pulp_tick(pulp_t *pulp, unsigned *next_tick_delay_us); void pulp_run(pulp_t *pulp); -- cgit v1.2.3