workqueue.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /* copyright (c) 2013-2015, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file workqueue.c
  5. *
  6. * \brief Implements worker threads, queues of work for them, and mechanisms
  7. * for them to send answers back to the main thread.
  8. *
  9. * The main structure here is a threadpool_t : it manages a set of worker
  10. * threads, a queue of pending work, and a reply queue. Every piece of work
  11. * is a workqueue_entry_t, containing data to process and a function to
  12. * process it with.
  13. *
  14. * The main thread informs the worker threads of pending work by using a
  15. * condition variable. The workers inform the main process of completed work
  16. * by using an alert_sockets_t object, as implemented in compat_threads.c.
  17. *
  18. * The main thread can also queue an "update" that will be handled by all the
  19. * workers. This is useful for updating state that all the workers share.
  20. *
  21. * In Tor today, there is currently only one thread pool, used in cpuworker.c.
  22. */
  23. #include "orconfig.h"
  24. #include "compat.h"
  25. #include "compat_threads.h"
  26. #include "crypto.h"
  27. #include "util.h"
  28. #include "workqueue.h"
  29. #include "tor_queue.h"
  30. #include "torlog.h"
  31. #define WORKQUEUE_PRIORITY_FIRST WQ_PRI_HIGH
  32. #define WORKQUEUE_PRIORITY_LAST WQ_PRI_LOW
  33. #define WORKQUEUE_N_PRIORITIES (((int) WORKQUEUE_PRIORITY_LAST)+1)
  34. TOR_TAILQ_HEAD(work_tailq_t, workqueue_entry_s);
  35. typedef struct work_tailq_t work_tailq_t;
  36. struct threadpool_s {
  37. /** An array of pointers to workerthread_t: one for each running worker
  38. * thread. */
  39. struct workerthread_s **threads;
  40. /** Condition variable that we wait on when we have no work, and which
  41. * gets signaled when our queue becomes nonempty. */
  42. tor_cond_t condition;
  43. /** Queues of pending work that we have to do. The queue with priority
  44. * <b>p</b> is work[p]. */
  45. work_tailq_t work[WORKQUEUE_N_PRIORITIES];
  46. /** Weak RNG, used to decide when to ignore priority. */
  47. tor_weak_rng_t weak_rng;
  48. /** The current 'update generation' of the threadpool. Any thread that is
  49. * at an earlier generation needs to run the update function. */
  50. unsigned generation;
  51. /** Function that should be run for updates on each thread. */
  52. workqueue_reply_t (*update_fn)(void *, void *);
  53. /** Function to free update arguments if they can't be run. */
  54. void (*free_update_arg_fn)(void *);
  55. /** Array of n_threads update arguments. */
  56. void **update_args;
  57. /** Number of elements in threads. */
  58. int n_threads;
  59. /** Mutex to protect all the above fields. */
  60. tor_mutex_t lock;
  61. /** A reply queue to use when constructing new threads. */
  62. replyqueue_t *reply_queue;
  63. /** Functions used to allocate and free thread state. */
  64. void *(*new_thread_state_fn)(void*);
  65. void (*free_thread_state_fn)(void*);
  66. void *new_thread_state_arg;
  67. };
  68. /** Used to put a workqueue_priority_t value into a bitfield. */
  69. #define workqueue_priority_bitfield_t ENUM_BF(workqueue_priority_t)
  70. /** Number of bits needed to hold all legal values of workqueue_priority_t */
  71. #define WORKQUEUE_PRIORITY_BITS 2
  72. struct workqueue_entry_s {
  73. /** The next workqueue_entry_t that's pending on the same thread or
  74. * reply queue. */
  75. TOR_TAILQ_ENTRY(workqueue_entry_s) next_work;
  76. /** The threadpool to which this workqueue_entry_t was assigned. This field
  77. * is set when the workqueue_entry_t is created, and won't be cleared until
  78. * after it's handled in the main thread. */
  79. struct threadpool_s *on_pool;
  80. /** True iff this entry is waiting for a worker to start processing it. */
  81. uint8_t pending;
  82. /** Priority of this entry. */
  83. workqueue_priority_bitfield_t priority : WORKQUEUE_PRIORITY_BITS;
  84. /** Function to run in the worker thread. */
  85. workqueue_reply_t (*fn)(void *state, void *arg);
  86. /** Function to run while processing the reply queue. */
  87. void (*reply_fn)(void *arg);
  88. /** Argument for the above functions. */
  89. void *arg;
  90. };
  91. struct replyqueue_s {
  92. /** Mutex to protect the answers field */
  93. tor_mutex_t lock;
  94. /** Doubly-linked list of answers that the reply queue needs to handle. */
  95. TOR_TAILQ_HEAD(, workqueue_entry_s) answers;
  96. /** Mechanism to wake up the main thread when it is receiving answers. */
  97. alert_sockets_t alert;
  98. };
  99. /** A worker thread represents a single thread in a thread pool. */
  100. typedef struct workerthread_s {
  101. /** Which thread it this? In range 0..in_pool->n_threads-1 */
  102. int index;
  103. /** The pool this thread is a part of. */
  104. struct threadpool_s *in_pool;
  105. /** User-supplied state field that we pass to the worker functions of each
  106. * work item. */
  107. void *state;
  108. /** Reply queue to which we pass our results. */
  109. replyqueue_t *reply_queue;
  110. /** The current update generation of this thread */
  111. unsigned generation;
  112. /** One over the probability of taking work from a lower-priority queue. */
  113. int32_t lower_priority_chance;
  114. } workerthread_t;
  115. static void queue_reply(replyqueue_t *queue, workqueue_entry_t *work);
  116. /** Allocate and return a new workqueue_entry_t, set up to run the function
  117. * <b>fn</b> in the worker thread, and <b>reply_fn</b> in the main
  118. * thread. See threadpool_queue_work() for full documentation. */
  119. static workqueue_entry_t *
  120. workqueue_entry_new(workqueue_reply_t (*fn)(void*, void*),
  121. void (*reply_fn)(void*),
  122. void *arg)
  123. {
  124. workqueue_entry_t *ent = tor_malloc_zero(sizeof(workqueue_entry_t));
  125. ent->fn = fn;
  126. ent->reply_fn = reply_fn;
  127. ent->arg = arg;
  128. ent->priority = WQ_PRI_HIGH;
  129. return ent;
  130. }
  131. #define workqueue_entry_free(ent) \
  132. FREE_AND_NULL(workqueue_entry_t, workqueue_entry_free_, (ent))
  133. /**
  134. * Release all storage held in <b>ent</b>. Call only when <b>ent</b> is not on
  135. * any queue.
  136. */
  137. static void
  138. workqueue_entry_free_(workqueue_entry_t *ent)
  139. {
  140. if (!ent)
  141. return;
  142. memset(ent, 0xf0, sizeof(*ent));
  143. tor_free(ent);
  144. }
  145. /**
  146. * Cancel a workqueue_entry_t that has been returned from
  147. * threadpool_queue_work.
  148. *
  149. * You must not call this function on any work whose reply function has been
  150. * executed in the main thread; that will cause undefined behavior (probably,
  151. * a crash).
  152. *
  153. * If the work is cancelled, this function return the argument passed to the
  154. * work function. It is the caller's responsibility to free this storage.
  155. *
  156. * This function will have no effect if the worker thread has already executed
  157. * or begun to execute the work item. In that case, it will return NULL.
  158. */
  159. void *
  160. workqueue_entry_cancel(workqueue_entry_t *ent)
  161. {
  162. int cancelled = 0;
  163. void *result = NULL;
  164. tor_mutex_acquire(&ent->on_pool->lock);
  165. workqueue_priority_t prio = ent->priority;
  166. if (ent->pending) {
  167. TOR_TAILQ_REMOVE(&ent->on_pool->work[prio], ent, next_work);
  168. cancelled = 1;
  169. result = ent->arg;
  170. }
  171. tor_mutex_release(&ent->on_pool->lock);
  172. if (cancelled) {
  173. workqueue_entry_free(ent);
  174. }
  175. return result;
  176. }
  177. /**DOCDOC
  178. must hold lock */
  179. static int
  180. worker_thread_has_work(workerthread_t *thread)
  181. {
  182. unsigned i;
  183. for (i = WORKQUEUE_PRIORITY_FIRST; i <= WORKQUEUE_PRIORITY_LAST; ++i) {
  184. if (!TOR_TAILQ_EMPTY(&thread->in_pool->work[i]))
  185. return 1;
  186. }
  187. return thread->generation != thread->in_pool->generation;
  188. }
  189. /** Extract the next workqueue_entry_t from the the thread's pool, removing
  190. * it from the relevant queues and marking it as non-pending.
  191. *
  192. * The caller must hold the lock. */
  193. static workqueue_entry_t *
  194. worker_thread_extract_next_work(workerthread_t *thread)
  195. {
  196. threadpool_t *pool = thread->in_pool;
  197. work_tailq_t *queue = NULL, *this_queue;
  198. unsigned i;
  199. for (i = WORKQUEUE_PRIORITY_FIRST; i <= WORKQUEUE_PRIORITY_LAST; ++i) {
  200. this_queue = &pool->work[i];
  201. if (!TOR_TAILQ_EMPTY(this_queue)) {
  202. queue = this_queue;
  203. if (! tor_weak_random_one_in_n(&pool->weak_rng,
  204. thread->lower_priority_chance)) {
  205. /* Usually we'll just break now, so that we can get out of the loop
  206. * and use the queue where we found work. But with a small
  207. * probability, we'll keep looking for lower priority work, so that
  208. * we don't ignore our low-priority queues entirely. */
  209. break;
  210. }
  211. }
  212. }
  213. if (queue == NULL)
  214. return NULL;
  215. workqueue_entry_t *work = TOR_TAILQ_FIRST(queue);
  216. TOR_TAILQ_REMOVE(queue, work, next_work);
  217. work->pending = 0;
  218. return work;
  219. }
  220. /**
  221. * Main function for the worker thread.
  222. */
  223. static void
  224. worker_thread_main(void *thread_)
  225. {
  226. workerthread_t *thread = thread_;
  227. threadpool_t *pool = thread->in_pool;
  228. workqueue_entry_t *work;
  229. workqueue_reply_t result;
  230. tor_mutex_acquire(&pool->lock);
  231. while (1) {
  232. /* lock must be held at this point. */
  233. while (worker_thread_has_work(thread)) {
  234. /* lock must be held at this point. */
  235. if (thread->in_pool->generation != thread->generation) {
  236. void *arg = thread->in_pool->update_args[thread->index];
  237. thread->in_pool->update_args[thread->index] = NULL;
  238. workqueue_reply_t (*update_fn)(void*,void*) =
  239. thread->in_pool->update_fn;
  240. thread->generation = thread->in_pool->generation;
  241. tor_mutex_release(&pool->lock);
  242. workqueue_reply_t r = update_fn(thread->state, arg);
  243. if (r != WQ_RPL_REPLY) {
  244. return;
  245. }
  246. tor_mutex_acquire(&pool->lock);
  247. continue;
  248. }
  249. work = worker_thread_extract_next_work(thread);
  250. if (BUG(work == NULL))
  251. break;
  252. tor_mutex_release(&pool->lock);
  253. /* We run the work function without holding the thread lock. This
  254. * is the main thread's first opportunity to give us more work. */
  255. result = work->fn(thread->state, work->arg);
  256. /* Queue the reply for the main thread. */
  257. queue_reply(thread->reply_queue, work);
  258. /* We may need to exit the thread. */
  259. if (result != WQ_RPL_REPLY) {
  260. return;
  261. }
  262. tor_mutex_acquire(&pool->lock);
  263. }
  264. /* At this point the lock is held, and there is no work in this thread's
  265. * queue. */
  266. /* TODO: support an idle-function */
  267. /* Okay. Now, wait till somebody has work for us. */
  268. if (tor_cond_wait(&pool->condition, &pool->lock, NULL) < 0) {
  269. log_warn(LD_GENERAL, "Fail tor_cond_wait.");
  270. }
  271. }
  272. }
  273. /** Put a reply on the reply queue. The reply must not currently be on
  274. * any thread's work queue. */
  275. static void
  276. queue_reply(replyqueue_t *queue, workqueue_entry_t *work)
  277. {
  278. int was_empty;
  279. tor_mutex_acquire(&queue->lock);
  280. was_empty = TOR_TAILQ_EMPTY(&queue->answers);
  281. TOR_TAILQ_INSERT_TAIL(&queue->answers, work, next_work);
  282. tor_mutex_release(&queue->lock);
  283. if (was_empty) {
  284. if (queue->alert.alert_fn(queue->alert.write_fd) < 0) {
  285. /* XXXX complain! */
  286. }
  287. }
  288. }
  289. /** Allocate and start a new worker thread to use state object <b>state</b>,
  290. * and send responses to <b>replyqueue</b>. */
  291. static workerthread_t *
  292. workerthread_new(int32_t lower_priority_chance,
  293. void *state, threadpool_t *pool, replyqueue_t *replyqueue)
  294. {
  295. workerthread_t *thr = tor_malloc_zero(sizeof(workerthread_t));
  296. thr->state = state;
  297. thr->reply_queue = replyqueue;
  298. thr->in_pool = pool;
  299. thr->lower_priority_chance = lower_priority_chance;
  300. if (spawn_func(worker_thread_main, thr) < 0) {
  301. //LCOV_EXCL_START
  302. tor_assert_nonfatal_unreached();
  303. log_err(LD_GENERAL, "Can't launch worker thread.");
  304. tor_free(thr);
  305. return NULL;
  306. //LCOV_EXCL_STOP
  307. }
  308. return thr;
  309. }
  310. /**
  311. * Queue an item of work for a thread in a thread pool. The function
  312. * <b>fn</b> will be run in a worker thread, and will receive as arguments the
  313. * thread's state object, and the provided object <b>arg</b>. It must return
  314. * one of WQ_RPL_REPLY, WQ_RPL_ERROR, or WQ_RPL_SHUTDOWN.
  315. *
  316. * Regardless of its return value, the function <b>reply_fn</b> will later be
  317. * run in the main thread when it invokes replyqueue_process(), and will
  318. * receive as its argument the same <b>arg</b> object. It's the reply
  319. * function's responsibility to free the work object.
  320. *
  321. * On success, return a workqueue_entry_t object that can be passed to
  322. * workqueue_entry_cancel(). On failure, return NULL. (Failure is not
  323. * currently possible, but callers should check anyway.)
  324. *
  325. * Items are executed in a loose priority order -- each thread will usually
  326. * take from the queued work with the highest prioirity, but will occasionally
  327. * visit lower-priority queues to keep them from starving completely.
  328. *
  329. * Note that because of priorities and thread behavior, work items may not
  330. * be executed strictly in order.
  331. */
  332. workqueue_entry_t *
  333. threadpool_queue_work_priority(threadpool_t *pool,
  334. workqueue_priority_t prio,
  335. workqueue_reply_t (*fn)(void *, void *),
  336. void (*reply_fn)(void *),
  337. void *arg)
  338. {
  339. tor_assert(((int)prio) >= WORKQUEUE_PRIORITY_FIRST &&
  340. ((int)prio) <= WORKQUEUE_PRIORITY_LAST);
  341. workqueue_entry_t *ent = workqueue_entry_new(fn, reply_fn, arg);
  342. ent->on_pool = pool;
  343. ent->pending = 1;
  344. ent->priority = prio;
  345. tor_mutex_acquire(&pool->lock);
  346. TOR_TAILQ_INSERT_TAIL(&pool->work[prio], ent, next_work);
  347. tor_cond_signal_one(&pool->condition);
  348. tor_mutex_release(&pool->lock);
  349. return ent;
  350. }
  351. /** As threadpool_queue_work_priority(), but assumes WQ_PRI_HIGH */
  352. workqueue_entry_t *
  353. threadpool_queue_work(threadpool_t *pool,
  354. workqueue_reply_t (*fn)(void *, void *),
  355. void (*reply_fn)(void *),
  356. void *arg)
  357. {
  358. return threadpool_queue_work_priority(pool, WQ_PRI_HIGH, fn, reply_fn, arg);
  359. }
  360. /**
  361. * Queue a copy of a work item for every thread in a pool. This can be used,
  362. * for example, to tell the threads to update some parameter in their states.
  363. *
  364. * Arguments are as for <b>threadpool_queue_work</b>, except that the
  365. * <b>arg</b> value is passed to <b>dup_fn</b> once per each thread to
  366. * make a copy of it.
  367. *
  368. * UPDATE FUNCTIONS MUST BE IDEMPOTENT. We do not guarantee that every update
  369. * will be run. If a new update is scheduled before the old update finishes
  370. * running, then the new will replace the old in any threads that haven't run
  371. * it yet.
  372. *
  373. * Return 0 on success, -1 on failure.
  374. */
  375. int
  376. threadpool_queue_update(threadpool_t *pool,
  377. void *(*dup_fn)(void *),
  378. workqueue_reply_t (*fn)(void *, void *),
  379. void (*free_fn)(void *),
  380. void *arg)
  381. {
  382. int i, n_threads;
  383. void (*old_args_free_fn)(void *arg);
  384. void **old_args;
  385. void **new_args;
  386. tor_mutex_acquire(&pool->lock);
  387. n_threads = pool->n_threads;
  388. old_args = pool->update_args;
  389. old_args_free_fn = pool->free_update_arg_fn;
  390. new_args = tor_calloc(n_threads, sizeof(void*));
  391. for (i = 0; i < n_threads; ++i) {
  392. if (dup_fn)
  393. new_args[i] = dup_fn(arg);
  394. else
  395. new_args[i] = arg;
  396. }
  397. pool->update_args = new_args;
  398. pool->free_update_arg_fn = free_fn;
  399. pool->update_fn = fn;
  400. ++pool->generation;
  401. tor_cond_signal_all(&pool->condition);
  402. tor_mutex_release(&pool->lock);
  403. if (old_args) {
  404. for (i = 0; i < n_threads; ++i) {
  405. if (old_args[i] && old_args_free_fn)
  406. old_args_free_fn(old_args[i]);
  407. }
  408. tor_free(old_args);
  409. }
  410. return 0;
  411. }
  412. /** Don't have more than this many threads per pool. */
  413. #define MAX_THREADS 1024
  414. /** For half of our threads, choose lower priority queues with probability
  415. * 1/N for each of these values. Both are chosen somewhat arbitrarily. If
  416. * CHANCE_PERMISSIVE is too low, then we have a risk of low-priority tasks
  417. * stalling forever. If it's too high, we have a risk of low-priority tasks
  418. * grabbing half of the threads. */
  419. #define CHANCE_PERMISSIVE 37
  420. #define CHANCE_STRICT INT32_MAX
  421. /** Launch threads until we have <b>n</b>. */
  422. static int
  423. threadpool_start_threads(threadpool_t *pool, int n)
  424. {
  425. if (BUG(n < 0))
  426. return -1; // LCOV_EXCL_LINE
  427. if (n > MAX_THREADS)
  428. n = MAX_THREADS;
  429. tor_mutex_acquire(&pool->lock);
  430. if (pool->n_threads < n)
  431. pool->threads = tor_reallocarray(pool->threads,
  432. sizeof(workerthread_t*), n);
  433. while (pool->n_threads < n) {
  434. /* For half of our threads, we'll choose lower priorities permissively;
  435. * for the other half, we'll stick more strictly to higher priorities.
  436. * This keeps slow low-priority tasks from taking over completely. */
  437. int32_t chance = (pool->n_threads & 1) ? CHANCE_STRICT : CHANCE_PERMISSIVE;
  438. void *state = pool->new_thread_state_fn(pool->new_thread_state_arg);
  439. workerthread_t *thr = workerthread_new(chance,
  440. state, pool, pool->reply_queue);
  441. if (!thr) {
  442. //LCOV_EXCL_START
  443. tor_assert_nonfatal_unreached();
  444. pool->free_thread_state_fn(state);
  445. tor_mutex_release(&pool->lock);
  446. return -1;
  447. //LCOV_EXCL_STOP
  448. }
  449. thr->index = pool->n_threads;
  450. pool->threads[pool->n_threads++] = thr;
  451. }
  452. tor_mutex_release(&pool->lock);
  453. return 0;
  454. }
  455. /**
  456. * Construct a new thread pool with <b>n</b> worker threads, configured to
  457. * send their output to <b>replyqueue</b>. The threads' states will be
  458. * constructed with the <b>new_thread_state_fn</b> call, receiving <b>arg</b>
  459. * as its argument. When the threads close, they will call
  460. * <b>free_thread_state_fn</b> on their states.
  461. */
  462. threadpool_t *
  463. threadpool_new(int n_threads,
  464. replyqueue_t *replyqueue,
  465. void *(*new_thread_state_fn)(void*),
  466. void (*free_thread_state_fn)(void*),
  467. void *arg)
  468. {
  469. threadpool_t *pool;
  470. pool = tor_malloc_zero(sizeof(threadpool_t));
  471. tor_mutex_init_nonrecursive(&pool->lock);
  472. tor_cond_init(&pool->condition);
  473. unsigned i;
  474. for (i = WORKQUEUE_PRIORITY_FIRST; i <= WORKQUEUE_PRIORITY_LAST; ++i) {
  475. TOR_TAILQ_INIT(&pool->work[i]);
  476. }
  477. {
  478. unsigned seed;
  479. crypto_rand((void*)&seed, sizeof(seed));
  480. tor_init_weak_random(&pool->weak_rng, seed);
  481. }
  482. pool->new_thread_state_fn = new_thread_state_fn;
  483. pool->new_thread_state_arg = arg;
  484. pool->free_thread_state_fn = free_thread_state_fn;
  485. pool->reply_queue = replyqueue;
  486. if (threadpool_start_threads(pool, n_threads) < 0) {
  487. //LCOV_EXCL_START
  488. tor_assert_nonfatal_unreached();
  489. tor_cond_uninit(&pool->condition);
  490. tor_mutex_uninit(&pool->lock);
  491. tor_free(pool);
  492. return NULL;
  493. //LCOV_EXCL_STOP
  494. }
  495. return pool;
  496. }
  497. /** Return the reply queue associated with a given thread pool. */
  498. replyqueue_t *
  499. threadpool_get_replyqueue(threadpool_t *tp)
  500. {
  501. return tp->reply_queue;
  502. }
  503. /** Allocate a new reply queue. Reply queues are used to pass results from
  504. * worker threads to the main thread. Since the main thread is running an
  505. * IO-centric event loop, it needs to get woken up with means other than a
  506. * condition variable. */
  507. replyqueue_t *
  508. replyqueue_new(uint32_t alertsocks_flags)
  509. {
  510. replyqueue_t *rq;
  511. rq = tor_malloc_zero(sizeof(replyqueue_t));
  512. if (alert_sockets_create(&rq->alert, alertsocks_flags) < 0) {
  513. //LCOV_EXCL_START
  514. tor_free(rq);
  515. return NULL;
  516. //LCOV_EXCL_STOP
  517. }
  518. tor_mutex_init(&rq->lock);
  519. TOR_TAILQ_INIT(&rq->answers);
  520. return rq;
  521. }
  522. /**
  523. * Return the "read socket" for a given reply queue. The main thread should
  524. * listen for read events on this socket, and call replyqueue_process() every
  525. * time it triggers.
  526. */
  527. tor_socket_t
  528. replyqueue_get_socket(replyqueue_t *rq)
  529. {
  530. return rq->alert.read_fd;
  531. }
  532. /**
  533. * Process all pending replies on a reply queue. The main thread should call
  534. * this function every time the socket returned by replyqueue_get_socket() is
  535. * readable.
  536. */
  537. void
  538. replyqueue_process(replyqueue_t *queue)
  539. {
  540. int r = queue->alert.drain_fn(queue->alert.read_fd);
  541. if (r < 0) {
  542. //LCOV_EXCL_START
  543. static ratelim_t warn_limit = RATELIM_INIT(7200);
  544. log_fn_ratelim(&warn_limit, LOG_WARN, LD_GENERAL,
  545. "Failure from drain_fd: %s",
  546. tor_socket_strerror(-r));
  547. //LCOV_EXCL_STOP
  548. }
  549. tor_mutex_acquire(&queue->lock);
  550. while (!TOR_TAILQ_EMPTY(&queue->answers)) {
  551. /* lock must be held at this point.*/
  552. workqueue_entry_t *work = TOR_TAILQ_FIRST(&queue->answers);
  553. TOR_TAILQ_REMOVE(&queue->answers, work, next_work);
  554. tor_mutex_release(&queue->lock);
  555. work->on_pool = NULL;
  556. work->reply_fn(work->arg);
  557. workqueue_entry_free(work);
  558. tor_mutex_acquire(&queue->lock);
  559. }
  560. tor_mutex_release(&queue->lock);
  561. }