Browse Source

MT#55283 type-safe thread helpers

Change-Id: I90ba4347ed787eb63bad84287bea200fac5dbe82
pull/1993/merge
Richard Fuchs 2 weeks ago
parent
commit
15effb7641
11 changed files with 35 additions and 30 deletions
  1. +1
    -2
      daemon/call.c
  2. +3
    -5
      daemon/helpers.c
  3. +1
    -3
      daemon/main.c
  4. +2
    -4
      daemon/media_player.c
  5. +1
    -2
      daemon/timerthread.c
  6. +1
    -1
      lib/auxlib.c
  7. +7
    -1
      lib/auxlib.h
  8. +7
    -4
      lib/helpers.h
  9. +1
    -2
      lib/poller.c
  10. +1
    -1
      lib/poller.h
  11. +10
    -5
      perf-tester/main.c

+ 1
- 2
daemon/call.c View File

@ -318,8 +318,7 @@ static size_t cb_curl_write(char *ptr, size_t size, size_t nmemb, void *userdata
return size * nmemb;
}
void xmlrpc_kill_calls(void *p) {
struct xmlrpc_helper *xh = p;
void xmlrpc_kill_calls(struct xmlrpc_helper *xh) {
unsigned int tries = 0;
int els_per_ent = 2;


+ 3
- 5
daemon/helpers.c View File

@ -205,8 +205,7 @@ static void thread_detach_cleanup(void *dtp) {
thread_join_me();
}
static void *thread_detach_func(void *d) {
struct detach_thread *dt = d;
static void *thread_detach_func(struct detach_thread *dt) {
pthread_t *t;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
@ -267,7 +266,7 @@ static void *thread_detach_func(void *d) {
return NULL;
}
void thread_create_detach_prio(void (*f)(void *), void *d, const char *scheduler, int priority,
void __thread_create_detach_prio(void (*f)(void *), void *d, const char *scheduler, int priority,
const char *name)
{
struct detach_thread *dt;
@ -282,9 +281,8 @@ void thread_create_detach_prio(void (*f)(void *), void *d, const char *scheduler
abort();
}
static void thread_looper_helper(void *fp) {
static void thread_looper_helper(struct looper_thread *lhp) {
// move object to stack and free it, so we can be cancelled without having a leak
struct looper_thread *lhp = fp;
struct looper_thread lh = *lhp;
g_free(lhp);


+ 1
- 3
daemon/main.c View File

@ -1844,9 +1844,7 @@ static void uring_thread_waker(struct thread_waker *wk) {
struct poller *p = wk->arg;
uring_poller_wake(p);
}
static void uring_poller_loop(void *ptr) {
struct poller *p = ptr;
static void uring_poller_loop(struct poller *p) {
uring_poller_add_waker(p);
struct thread_waker wk = {.func = uring_thread_waker, .arg = p};


+ 2
- 4
daemon/media_player.c View File

@ -798,9 +798,7 @@ static void media_player_cache_packet(struct media_player_cache_entry *entry, ch
entry->coder.handler->handler_func(entry->coder.handler, &packet);
}
static void media_player_cache_entry_decoder_thread(void *p) {
struct media_player_cache_entry *entry = p;
static void media_player_cache_entry_decoder_thread(struct media_player_cache_entry *entry) {
ilog(LOG_DEBUG, "Launching media decoder thread for %s", entry->info_str);
while (true) {
@ -913,7 +911,7 @@ static bool media_player_cache_entry_init(struct media_player *mp, const rtp_pay
entry->coder.handler->packet_encoded = media_player_packet_cache;
// use low priority (10 nice)
thread_create_detach_prio(media_player_cache_entry_decoder_thread, obj_hold(entry), NULL, 10, "mp decoder");
thread_create_detach_prio(media_player_cache_entry_decoder_thread, obj_get(entry), NULL, 10, "mp decoder");
media_player_cached_reader_start(mp, codec_set);


+ 1
- 2
daemon/timerthread.c View File

@ -65,8 +65,7 @@ void timerthread_free(struct timerthread *tt) {
g_free(tt->threads);
}
static void timerthread_run(void *p) {
struct timerthread_thread *tt = p;
static void timerthread_run(struct timerthread_thread *tt) {
struct timerthread *parent = tt->parent;
struct thread_waker waker = { .lock = &tt->lock, .cond = &tt->cond };


+ 1
- 1
lib/auxlib.c View File

@ -73,7 +73,7 @@ void service_notify(const char *message) {
}
int thread_create(void *(*func)(void *), void *arg, bool joinable, pthread_t *handle, const char *name) {
int __thread_create(void *(*func)(void *), void *arg, bool joinable, pthread_t *handle, const char *name) {
pthread_attr_t att;
pthread_t thr;
int ret;


+ 7
- 1
lib/auxlib.h View File

@ -111,7 +111,13 @@ INLINE void config_load(int *argc, char ***argv, GOptionEntry *entries, const ch
}
char *get_thread_buf(void);
int thread_create(void *(*func)(void *), void *arg, bool joinable, pthread_t *handle, const char *name);
int __thread_create(void *(*func)(void *), void *arg, bool joinable, pthread_t *handle, const char *name);
#define thread_create(func, arg, joinable, handle, name) ({ \
void *(*__func)(__typeof(arg)) = (func); \
void *__arg = (arg); \
int __ret = __thread_create((void *(*)(void *)) __func, __arg, (joinable), (handle), (name)); \
__ret; \
})
unsigned int in6_addr_hash(const void *p);
int in6_addr_eq(const void *a, const void *b);


+ 7
- 4
lib/helpers.h View File

@ -214,12 +214,15 @@ void thread_waker_add(struct thread_waker *);
void thread_waker_add_generic(struct thread_waker *);
void thread_waker_del(struct thread_waker *);
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 *);
#define thread_create_detach_prio(func, arg, sched, prio, name) do { \
void (*__func)(__typeof(arg)) = (func); \
void *__arg = (arg); \
__thread_create_detach_prio((void (*)(void *)) __func, __arg, (sched), (prio), (name)); \
} while (0)
void thread_create_looper(enum thread_looper_action (*f)(void), const char *scheduler, int priority,
const char *name, int64_t);
INLINE void thread_create_detach(void (*f)(void *), void *a, const char *name) {
thread_create_detach_prio(f, a, NULL, 0, name);
}
#define thread_create_detach(func, arg, name) thread_create_detach_prio(func, arg, NULL, 0, name)


+ 1
- 2
lib/poller.c View File

@ -327,8 +327,7 @@ out:
return ret;
}
void poller_loop(void *d) {
struct poller *p = d;
void poller_loop(struct poller *p) {
int poller_size = rtpe_common_config_ptr->poller_size;
struct epoll_event *evs;


+ 1
- 1
lib/poller.h View File

@ -45,7 +45,7 @@ void poller_blocked(struct poller *, void *);
bool poller_isblocked(struct poller *, void *);
void poller_error(struct poller *, void *);
void poller_loop(void *);
void poller_loop(struct poller *);
extern bool (*rtpe_poller_add_item)(struct poller *, struct poller_item *);
extern bool (*rtpe_poller_del_item)(struct poller *, int);


+ 10
- 5
perf-tester/main.c View File

@ -230,7 +230,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(DIR, closedir)
static pthread_t thread_new(const char *name, void *(*fn)(void *), void *p) {
static pthread_t __thread_new(const char *name, void *(*fn)(void *), void *p) {
pthread_t ret;
int s = pthread_create(&ret, NULL, fn, p);
if (s != 0)
@ -242,6 +242,13 @@ static pthread_t thread_new(const char *name, void *(*fn)(void *), void *p) {
return ret;
}
#define thread_new(name, func, arg) ({ \
void *(*__func)(__typeof(arg)) = (func); \
void *__arg = (arg); \
pthread_t __thr = __thread_new((name), (void *(*)(void *)) __func, __arg); \
__thr; \
})
static inline long long us_ticks_scale(long long val) {
return val * ticks_per_sec / 1000000;
@ -310,7 +317,7 @@ static int got_frame(decoder_t *decoder, AVFrame *frame, void *p1, void *b) {
}
static void *worker(void *p) {
static void *worker(struct worker *p) {
thread_cancel_disable();
worker_self = p;
worker_self->pid = gettid();
@ -1771,9 +1778,7 @@ static void delay_measure_workers(uint milliseconds, struct stats *totals) {
}
static void *cpu_freq_monitor(void *p) {
struct thread_freq_stats *freq_stats = p;
static void *cpu_freq_monitor(struct thread_freq_stats *freq_stats) {
while (true) {
struct freq_stats iter_stats = {0};


Loading…
Cancel
Save