workqueue.h (2929B)
1 /* Copyright (c) 2013-2024, The Tor Project, Inc. */ 2 /* See LICENSE for licensing information */ 3 4 /** 5 * \file workqueue.h 6 * \brief Header for workqueue.c 7 **/ 8 9 #ifndef TOR_WORKQUEUE_H 10 #define TOR_WORKQUEUE_H 11 12 #include "lib/cc/torint.h" 13 14 /** A replyqueue is used to tell the main thread about the outcome of 15 * work that we queued for the workers. */ 16 typedef struct replyqueue_t replyqueue_t; 17 /** A thread-pool manages starting threads and passing work to them. */ 18 typedef struct threadpool_t threadpool_t; 19 /** A workqueue entry represents a request that has been passed to a thread 20 * pool. */ 21 typedef struct workqueue_entry_t workqueue_entry_t; 22 23 /** Possible return value from a work function: */ 24 typedef enum workqueue_reply_t { 25 WQ_RPL_REPLY = 0, /** indicates success */ 26 WQ_RPL_ERROR = 1, /** indicates fatal error */ 27 WQ_RPL_SHUTDOWN = 2, /** indicates thread is shutting down */ 28 } workqueue_reply_t; 29 30 /** Possible priorities for work. Lower numeric values are more important. */ 31 typedef enum workqueue_priority_t { 32 WQ_PRI_HIGH = 0, 33 WQ_PRI_MED = 1, 34 WQ_PRI_LOW = 2, 35 } workqueue_priority_t; 36 37 workqueue_entry_t *threadpool_queue_work_priority(threadpool_t *pool, 38 workqueue_priority_t prio, 39 workqueue_reply_t (*fn)(void *, 40 void *), 41 void (*reply_fn)(void *), 42 void *arg); 43 44 workqueue_entry_t *threadpool_queue_work(threadpool_t *pool, 45 workqueue_reply_t (*fn)(void *, 46 void *), 47 void (*reply_fn)(void *), 48 void *arg); 49 50 int threadpool_queue_update(threadpool_t *pool, 51 void *(*dup_fn)(void *), 52 workqueue_reply_t (*fn)(void *, void *), 53 void (*free_fn)(void *), 54 void *arg); 55 void *workqueue_entry_cancel(workqueue_entry_t *pending_work); 56 threadpool_t *threadpool_new(int n_threads, 57 replyqueue_t *replyqueue, 58 void *(*new_thread_state_fn)(void*), 59 void (*free_thread_state_fn)(void*), 60 void *arg); 61 void threadpool_free_(threadpool_t *tp); 62 #define threadpool_free(pool) \ 63 FREE_AND_NULL(threadpool_t, threadpool_free_, (pool)) 64 replyqueue_t *threadpool_get_replyqueue(threadpool_t *tp); 65 66 replyqueue_t *replyqueue_new(uint32_t alertsocks_flags); 67 void replyqueue_process(replyqueue_t *queue); 68 69 int threadpool_register_reply_event(threadpool_t *tp, 70 void (*cb)(threadpool_t *tp)); 71 unsigned int threadpool_get_n_threads(threadpool_t *tp); 72 73 #endif /* !defined(TOR_WORKQUEUE_H) */