workqueue.h 3.2 KB

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