Browse Source

MT#55283 generalise thread_waker

Allow other mechanisms for waking up a thread.

Change-Id: I29c8bc90b1b494e9299432af48f36155d256c0aa
pull/1826/head
Richard Fuchs 2 years ago
parent
commit
6aca3e88ad
2 changed files with 13 additions and 3 deletions
  1. +10
    -3
      daemon/helpers.c
  2. +3
    -0
      include/helpers.h

+ 10
- 3
daemon/helpers.c View File

@ -134,9 +134,7 @@ void threads_join_all(bool cancel) {
mutex_lock(&thread_wakers_lock); mutex_lock(&thread_wakers_lock);
for (l = thread_wakers; l; l = l->next) { for (l = thread_wakers; l; l = l->next) {
struct thread_waker *wk = l->data; struct thread_waker *wk = l->data;
mutex_lock(wk->lock);
cond_broadcast(wk->cond);
mutex_unlock(wk->lock);
wk->func(wk);
} }
mutex_unlock(&thread_wakers_lock); mutex_unlock(&thread_wakers_lock);
@ -172,7 +170,16 @@ void threads_join_all(bool cancel) {
} }
} }
static void thread_waker_wake_cond(struct thread_waker *wk) {
mutex_lock(wk->lock);
cond_broadcast(wk->cond);
mutex_unlock(wk->lock);
}
void thread_waker_add(struct thread_waker *wk) { void thread_waker_add(struct thread_waker *wk) {
wk->func = thread_waker_wake_cond;
thread_waker_add_generic(wk);
}
void thread_waker_add_generic(struct thread_waker *wk) {
mutex_lock(&thread_wakers_lock); mutex_lock(&thread_wakers_lock);
thread_wakers = g_list_prepend(thread_wakers, wk); thread_wakers = g_list_prepend(thread_wakers, wk);
mutex_unlock(&thread_wakers_lock); mutex_unlock(&thread_wakers_lock);


+ 3
- 0
include/helpers.h View File

@ -212,8 +212,10 @@ INLINE void swap_ptrs(void *a, void *b) {
/*** THREAD HELPERS ***/ /*** THREAD HELPERS ***/
struct thread_waker { struct thread_waker {
void (*func)(struct thread_waker *);
mutex_t *lock; mutex_t *lock;
cond_t *cond; cond_t *cond;
void *arg;
}; };
enum thread_looper_action { enum thread_looper_action {
TLA_CONTINUE, TLA_CONTINUE,
@ -221,6 +223,7 @@ enum thread_looper_action {
}; };
void thread_waker_add(struct thread_waker *); void thread_waker_add(struct thread_waker *);
void thread_waker_add_generic(struct thread_waker *);
void thread_waker_del(struct thread_waker *); void thread_waker_del(struct thread_waker *);
void threads_join_all(bool cancel); void threads_join_all(bool cancel);
void thread_create_detach_prio(void (*)(void *), void *, const char *, int, const char *); void thread_create_detach_prio(void (*)(void *), void *, const char *, int, const char *);


Loading…
Cancel
Save