diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2014-04-26 15:08:09 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2014-04-26 15:08:09 -0400 |
commit | 12c4a5eb07f346083a59bc7d18567b2df3ed94ee (patch) | |
tree | 638017b057b81d34935048d1b4c4bb7472614d88 /libdimension | |
parent | 0acff566213fdddbc8f4561887aced121f82dc26 (diff) | |
download | dimension-12c4a5eb07f346083a59bc7d18567b2df3ed94ee.tar.xz |
future: Add a dmnsn_future_is_done() function.
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/dimension/future.h | 7 | ||||
-rw-r--r-- | libdimension/future-internal.h | 4 | ||||
-rw-r--r-- | libdimension/future.c | 22 | ||||
-rw-r--r-- | libdimension/tests/render.c | 2 | ||||
-rw-r--r-- | libdimension/threads.c | 4 |
5 files changed, 30 insertions, 9 deletions
diff --git a/libdimension/dimension/future.h b/libdimension/dimension/future.h index 9f1f14a..9ba28b1 100644 --- a/libdimension/dimension/future.h +++ b/libdimension/dimension/future.h @@ -51,6 +51,13 @@ void dmnsn_future_cancel(dmnsn_future *future); double dmnsn_future_progress(const dmnsn_future *future); /** + * Find out if a background task is finished. + * @param[in] future The background task to examine. + * @return true if the task is done, false otherwise. + */ +bool dmnsn_future_is_done(const dmnsn_future *future); + +/** * Wait for a certain amount of progress. Always use this rather than * spinlocking. * @param[in] future The background task to monitor. diff --git a/libdimension/future-internal.h b/libdimension/future-internal.h index 644a486..5fcd553 100644 --- a/libdimension/future-internal.h +++ b/libdimension/future-internal.h @@ -33,12 +33,12 @@ DMNSN_INTERNAL void dmnsn_future_set_total(dmnsn_future *future, size_t total); /** Increment the progress of a background task. */ DMNSN_INTERNAL void dmnsn_future_increment(dmnsn_future *future); /** Instantly complete the background teask. */ -DMNSN_INTERNAL void dmnsn_future_done(dmnsn_future *future); +DMNSN_INTERNAL void dmnsn_future_finish(dmnsn_future *future); /** Set the number of worker threads. */ DMNSN_INTERNAL void dmnsn_future_set_nthreads(dmnsn_future *future, unsigned int nthreads); /** Notify completion of a worker thread. */ -DMNSN_INTERNAL void dmnsn_future_thread_done(dmnsn_future *future); +DMNSN_INTERNAL void dmnsn_future_finish_thread(dmnsn_future *future); struct dmnsn_future { size_t progress; /**< Completed loop iterations. */ diff --git a/libdimension/future.c b/libdimension/future.c index 8d9c0c8..622d40e 100644 --- a/libdimension/future.c +++ b/libdimension/future.c @@ -114,6 +114,20 @@ dmnsn_future_progress(const dmnsn_future *future) return progress; } +/* Find out whether the task is complete. */ +bool +dmnsn_future_is_done(const dmnsn_future *future) +{ + dmnsn_future *mfuture = MUTATE(future); + bool result; + + dmnsn_lock_mutex(&mfuture->mutex); + result = future->progress == future->total; + dmnsn_unlock_mutex(&mfuture->mutex); + + return result; +} + /* Wait until dmnsn_future_progress(future) >= progress */ void dmnsn_future_wait(const dmnsn_future *future, double progress) @@ -217,7 +231,7 @@ dmnsn_future_increment(dmnsn_future *future) /* Immediately set to 100% completion */ void -dmnsn_future_done(dmnsn_future *future) +dmnsn_future_finish(dmnsn_future *future) { dmnsn_lock_mutex(&future->mutex); future->progress = future->total; @@ -241,15 +255,15 @@ dmnsn_future_set_nthreads(dmnsn_future *future, unsigned int nthreads) /* Notify completion of a worker thread */ void -dmnsn_future_thread_done(dmnsn_future *future) +dmnsn_future_finish_thread(dmnsn_future *future) { dmnsn_lock_mutex(&future->mutex); dmnsn_assert(future->nthreads > 0, - "dmnsn_future_thread_done() called with no threads"); + "dmnsn_future_finish_thread() called with no threads"); --future->nthreads; dmnsn_assert(future->nrunning > 0, - "dmnsn_future_thread_done() called with no running threads"); + "dmnsn_future_finish_thread() called with no running threads"); if (--future->nrunning == 0) { dmnsn_cond_broadcast(&future->none_running_cond); } diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c index 1efff28..0192d35 100644 --- a/libdimension/tests/render.c +++ b/libdimension/tests/render.c @@ -366,7 +366,7 @@ main(void) /* Display the scene as it's rendered */ if (display) { - while (dmnsn_future_progress(future) < 1.0) { + while (!dmnsn_future_is_done(future)) { dmnsn_future_pause(future); if (dmnsn_gl_write_canvas(scene->canvas) != 0) { dmnsn_delete_display(display); diff --git a/libdimension/threads.c b/libdimension/threads.c index 76f4796..85832b9 100644 --- a/libdimension/threads.c +++ b/libdimension/threads.c @@ -41,7 +41,7 @@ dmnsn_thread_cleanup(void *arg) dmnsn_future *future = payload->future; dmnsn_free(payload); - dmnsn_future_done(future); + dmnsn_future_finish(future); } /** pthread callback -- call the real thread callback. */ @@ -88,7 +88,7 @@ dmnsn_concurrent_thread(void *ptr) payload->ret = payload->ccthread_fn(payload->arg, payload->thread, payload->nthreads); if (payload->future) { - dmnsn_future_thread_done(payload->future); + dmnsn_future_finish_thread(payload->future); } return NULL; } |