summaryrefslogtreecommitdiffstats
path: root/misc
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2018-05-18 23:24:17 +0200
committerwm4 <wm4@nowhere>2018-05-24 19:56:35 +0200
commit1ad027e8fd6df7fd3a96589c30b510568f264627 (patch)
tree4a263592b0a2aa188ae496a6ca62ac9292e57006 /misc
parentf0cc6ba18df12f582c9777ab44fc98ebe9cb755e (diff)
downloadmpv-1ad027e8fd6df7fd3a96589c30b510568f264627.tar.bz2
mpv-1ad027e8fd6df7fd3a96589c30b510568f264627.tar.xz
thread_pool: add a helper function
The behavior of mp_thread_pool_queue() doesn't or shouldn't change, but the new helper function requires touching its logic.
Diffstat (limited to 'misc')
-rw-r--r--misc/thread_pool.c35
-rw-r--r--misc/thread_pool.h5
2 files changed, 30 insertions, 10 deletions
diff --git a/misc/thread_pool.c b/misc/thread_pool.c
index 26c3861f1b..217c990c19 100644
--- a/misc/thread_pool.c
+++ b/misc/thread_pool.c
@@ -139,12 +139,15 @@ static void thread_pool_dtor(void *ctx)
pthread_mutex_destroy(&pool->lock);
}
-static void add_thread(struct mp_thread_pool *pool)
+static bool add_thread(struct mp_thread_pool *pool)
{
pthread_t thread;
- if (pthread_create(&thread, NULL, worker_thread, pool) == 0)
- MP_TARRAY_APPEND(pool, pool->threads, pool->num_threads, thread);
+ if (pthread_create(&thread, NULL, worker_thread, pool) != 0)
+ return false;
+
+ MP_TARRAY_APPEND(pool, pool->threads, pool->num_threads, thread);
+ return true;
}
struct mp_thread_pool *mp_thread_pool_create(void *ta_parent, int init_threads,
@@ -175,8 +178,8 @@ struct mp_thread_pool *mp_thread_pool_create(void *ta_parent, int init_threads,
return pool;
}
-bool mp_thread_pool_queue(struct mp_thread_pool *pool, void (*fn)(void *ctx),
- void *fn_ctx)
+static bool thread_pool_add(struct mp_thread_pool *pool, void (*fn)(void *ctx),
+ void *fn_ctx, bool allow_queue)
{
bool ok = true;
@@ -192,17 +195,29 @@ bool mp_thread_pool_queue(struct mp_thread_pool *pool, void (*fn)(void *ctx),
if (pool->busy_threads + pool->num_work + 1 > pool->num_threads &&
pool->num_threads < pool->max_threads)
{
- // We ignore failures, unless there are no threads available (below).
- add_thread(pool);
+ if (!add_thread(pool)) {
+ // If we can queue it, it'll get done as long as there is 1 thread.
+ ok = allow_queue && pool->num_threads > 0;
+ }
}
- if (pool->num_threads) {
+ if (ok) {
MP_TARRAY_INSERT_AT(pool, pool->work, pool->num_work, 0, work);
pthread_cond_signal(&pool->wakeup);
- } else {
- ok = false;
}
pthread_mutex_unlock(&pool->lock);
return ok;
}
+
+bool mp_thread_pool_queue(struct mp_thread_pool *pool, void (*fn)(void *ctx),
+ void *fn_ctx)
+{
+ return thread_pool_add(pool, fn, fn_ctx, true);
+}
+
+bool mp_thread_pool_run(struct mp_thread_pool *pool, void (*fn)(void *ctx),
+ void *fn_ctx)
+{
+ return thread_pool_add(pool, fn, fn_ctx, false);
+}
diff --git a/misc/thread_pool.h b/misc/thread_pool.h
index 4c15c0b5c5..14954da58f 100644
--- a/misc/thread_pool.h
+++ b/misc/thread_pool.h
@@ -26,4 +26,9 @@ struct mp_thread_pool *mp_thread_pool_create(void *ta_parent, int init_threads,
bool mp_thread_pool_queue(struct mp_thread_pool *pool, void (*fn)(void *ctx),
void *fn_ctx);
+// Like mp_thread_pool_queue(), but only queue the item and succeed if a thread
+// can be reserved for the item (i.e. minimal wait time instead of unbounded).
+bool mp_thread_pool_run(struct mp_thread_pool *pool, void (*fn)(void *ctx),
+ void *fn_ctx);
+
#endif