workqueue.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. /**
  132. * Release all storage held in <b>ent</b>. Call only when <b>ent</b> is not on
  133. * any queue.
  134. */
  135. static void
  136. workqueue_entry_free(workqueue_entry_t *ent)
  137. {
  138. if (!ent)
  139. return;
  140. memset(ent, 0xf0, sizeof(*ent));
  141. tor_free(ent);
  142. }
  143. /**
  144. * Cancel a workqueue_entry_t that has been returned from
  145. * threadpool_queue_work.
  146. *
  147. * You must not call this function on any work whose reply function has been
  148. * executed in the main thread; that will cause undefined behavior (probably,
  149. * a crash).
  150. *
  151. * If the work is cancelled, this function return the argument passed to the
  152. * work function. It is the caller's responsibility to free this storage.
  153. *
  154. * This function will have no effect if the worker thread has already executed
  155. * or begun to execute the work item. In that case, it will return NULL.
  156. */
  157. void *
  158. workqueue_entry_cancel(workqueue_entry_t *ent)
  159. {
  160. int cancelled = 0;
  161. void *result = NULL;
  162. tor_mutex_acquire(&ent->on_pool->lock);
  163. workqueue_priority_t prio = ent->priority;
  164. if (ent->pending) {
  165. TOR_TAILQ_REMOVE(&ent->on_pool->work[prio], ent, next_work);
  166. cancelled = 1;
  167. result = ent->arg;
  168. }
  169. tor_mutex_release(&ent->on_pool->lock);
  170. if (cancelled) {
  171. workqueue_entry_free(ent);
  172. }
  173. return result;
  174. }
  175. /**DOCDOC
  176. must hold lock */
  177. static int
  178. worker_thread_has_work(workerthread_t *thread)
  179. {
  180. unsigned i;
  181. for (i = WORKQUEUE_PRIORITY_FIRST; i <= WORKQUEUE_PRIORITY_LAST; ++i) {
  182. if (!TOR_TAILQ_EMPTY(&thread->in_pool->work[i]))
  183. return 1;
  184. }
  185. return thread->generation != thread->in_pool->generation;
  186. }
  187. /** Extract the next workqueue_entry_t from the the thread's pool, removing
  188. * it from the relevant queues and marking it as non-pending.
  189. *
  190. * The caller must hold the lock. */
  191. static workqueue_entry_t *
  192. worker_thread_extract_next_work(workerthread_t *thread)
  193. {
  194. threadpool_t *pool = thread->in_pool;
  195. work_tailq_t *queue = NULL, *this_queue;
  196. unsigned i;
  197. for (i = WORKQUEUE_PRIORITY_FIRST; i <= WORKQUEUE_PRIORITY_LAST; ++i) {
  198. this_queue = &pool->work[i];
  199. if (!TOR_TAILQ_EMPTY(this_queue)) {
  200. queue = this_queue;
  201. if (! tor_weak_random_one_in_n(&pool->weak_rng,
  202. thread->lower_priority_chance)) {
  203. /* Usually we'll just break now, so that we can get out of the loop
  204. * and use the queue where we found work. But with a small
  205. * probability, we'll keep looking for lower priority work, so that
  206. * we don't ignore our low-priority queues entirely. */
  207. break;
  208. }
  209. }
  210. }
  211. if (queue == NULL)
  212. return NULL;
  213. workqueue_entry_t *work = TOR_TAILQ_FIRST(queue);
  214. TOR_TAILQ_REMOVE(queue, work, next_work);
  215. work->pending = 0;
  216. return work;
  217. }
  218. /**
  219. * Main function for the worker thread.
  220. */
  221. static void
  222. worker_thread_main(void *thread_)
  223. {
  224. workerthread_t *thread = thread_;
  225. threadpool_t *pool = thread->in_pool;
  226. workqueue_entry_t *work;
  227. workqueue_reply_t result;
  228. tor_mutex_acquire(&pool->lock);
  229. while (1) {
  230. /* lock must be held at this point. */
  231. while (worker_thread_has_work(thread)) {
  232. /* lock must be held at this point. */
  233. if (thread->in_pool->generation != thread->generation) {
  234. void *arg = thread->in_pool->update_args[thread->index];
  235. thread->in_pool->update_args[thread->index] = NULL;
  236. workqueue_reply_t (*update_fn)(void*,void*) =
  237. thread->in_pool->update_fn;
  238. thread->generation = thread->in_pool->generation;
  239. tor_mutex_release(&pool->lock);
  240. workqueue_reply_t r = update_fn(thread->state, arg);
  241. if (r != WQ_RPL_REPLY) {
  242. return;
  243. }
  244. tor_mutex_acquire(&pool->lock);
  245. continue;
  246. }
  247. work = worker_thread_extract_next_work(thread);
  248. if (BUG(work == NULL))
  249. break;
  250. tor_mutex_release(&pool->lock);
  251. /* We run the work function without holding the thread lock. This
  252. * is the main thread's first opportunity to give us more work. */
  253. result = work->fn(thread->state, work->arg);
  254. /* Queue the reply for the main thread. */
  255. queue_reply(thread->reply_queue, work);
  256. /* We may need to exit the thread. */
  257. if (result != WQ_RPL_REPLY) {
  258. return;
  259. }
  260. tor_mutex_acquire(&pool->lock);
  261. }
  262. /* At this point the lock is held, and there is no work in this thread's
  263. * queue. */
  264. /* TODO: support an idle-function */
  265. /* Okay. Now, wait till somebody has work for us. */
  266. if (tor_cond_wait(&pool->condition, &pool->lock, NULL) < 0) {
  267. log_warn(LD_GENERAL, "Fail tor_cond_wait.");
  268. }
  269. }
  270. }
  271. /** Put a reply on the reply queue. The reply must not currently be on
  272. * any thread's work queue. */
  273. static void
  274. queue_reply(replyqueue_t *queue, workqueue_entry_t *work)
  275. {
  276. int was_empty;
  277. tor_mutex_acquire(&queue->lock);
  278. was_empty = TOR_TAILQ_EMPTY(&queue->answers);
  279. TOR_TAILQ_INSERT_TAIL(&queue->answers, work, next_work);
  280. tor_mutex_release(&queue->lock);
  281. if (was_empty) {
  282. if (queue->alert.alert_fn(queue->alert.write_fd) < 0) {
  283. /* XXXX complain! */
  284. }
  285. }
  286. }
  287. /** Allocate and start a new worker thread to use state object <b>state</b>,
  288. * and send responses to <b>replyqueue</b>. */
  289. static workerthread_t *
  290. workerthread_new(int32_t lower_priority_chance,
  291. void *state, threadpool_t *pool, replyqueue_t *replyqueue)
  292. {
  293. workerthread_t *thr = tor_malloc_zero(sizeof(workerthread_t));
  294. thr->state = state;
  295. thr->reply_queue = replyqueue;
  296. thr->in_pool = pool;
  297. thr->lower_priority_chance = lower_priority_chance;
  298. if (spawn_func(worker_thread_main, thr) < 0) {
  299. //LCOV_EXCL_START
  300. tor_assert_nonfatal_unreached();
  301. log_err(LD_GENERAL, "Can't launch worker thread.");
  302. tor_free(thr);
  303. return NULL;
  304. //LCOV_EXCL_STOP
  305. }
  306. return thr;
  307. }
  308. /**
  309. * Queue an item of work for a thread in a thread pool. The function
  310. * <b>fn</b> will be run in a worker thread, and will receive as arguments the
  311. * thread's state object, and the provided object <b>arg</b>. It must return
  312. * one of WQ_RPL_REPLY, WQ_RPL_ERROR, or WQ_RPL_SHUTDOWN.
  313. *
  314. * Regardless of its return value, the function <b>reply_fn</b> will later be
  315. * run in the main thread when it invokes replyqueue_process(), and will
  316. * receive as its argument the same <b>arg</b> object. It's the reply
  317. * function's responsibility to free the work object.
  318. *
  319. * On success, return a workqueue_entry_t object that can be passed to
  320. * workqueue_entry_cancel(). On failure, return NULL.
  321. *
  322. * Items are executed in a loose priority order -- each thread will usually
  323. * take from the queued work with the highest prioirity, but will occasionally
  324. * visit lower-priority queues to keep them from starving completely.
  325. *
  326. * Note that because of priorities and thread behavior, work items may not
  327. * be executed strictly in order.
  328. */
  329. workqueue_entry_t *
  330. threadpool_queue_work_priority(threadpool_t *pool,
  331. workqueue_priority_t prio,
  332. workqueue_reply_t (*fn)(void *, void *),
  333. void (*reply_fn)(void *),
  334. void *arg)
  335. {
  336. tor_assert(prio >= WORKQUEUE_PRIORITY_FIRST &&
  337. prio <= WORKQUEUE_PRIORITY_LAST);
  338. workqueue_entry_t *ent = workqueue_entry_new(fn, reply_fn, arg);
  339. ent->on_pool = pool;
  340. ent->pending = 1;
  341. ent->priority = prio;
  342. tor_mutex_acquire(&pool->lock);
  343. TOR_TAILQ_INSERT_TAIL(&pool->work[prio], ent, next_work);
  344. tor_cond_signal_one(&pool->condition);
  345. tor_mutex_release(&pool->lock);
  346. return ent;
  347. }
  348. /** As threadpool_queue_work_priority(), but assumes WQ_PRI_HIGH */
  349. workqueue_entry_t *
  350. threadpool_queue_work(threadpool_t *pool,
  351. workqueue_reply_t (*fn)(void *, void *),
  352. void (*reply_fn)(void *),
  353. void *arg)
  354. {
  355. return threadpool_queue_work_priority(pool, WQ_PRI_HIGH, fn, reply_fn, arg);
  356. }
  357. /**
  358. * Queue a copy of a work item for every thread in a pool. This can be used,
  359. * for example, to tell the threads to update some parameter in their states.
  360. *
  361. * Arguments are as for <b>threadpool_queue_work</b>, except that the
  362. * <b>arg</b> value is passed to <b>dup_fn</b> once per each thread to
  363. * make a copy of it.
  364. *
  365. * UPDATE FUNCTIONS MUST BE IDEMPOTENT. We do not guarantee that every update
  366. * will be run. If a new update is scheduled before the old update finishes
  367. * running, then the new will replace the old in any threads that haven't run
  368. * it yet.
  369. *
  370. * Return 0 on success, -1 on failure.
  371. */
  372. int
  373. threadpool_queue_update(threadpool_t *pool,
  374. void *(*dup_fn)(void *),
  375. workqueue_reply_t (*fn)(void *, void *),
  376. void (*free_fn)(void *),
  377. void *arg)
  378. {
  379. int i, n_threads;
  380. void (*old_args_free_fn)(void *arg);
  381. void **old_args;
  382. void **new_args;
  383. tor_mutex_acquire(&pool->lock);
  384. n_threads = pool->n_threads;
  385. old_args = pool->update_args;
  386. old_args_free_fn = pool->free_update_arg_fn;
  387. new_args = tor_calloc(n_threads, sizeof(void*));
  388. for (i = 0; i < n_threads; ++i) {
  389. if (dup_fn)
  390. new_args[i] = dup_fn(arg);
  391. else
  392. new_args[i] = arg;
  393. }
  394. pool->update_args = new_args;
  395. pool->free_update_arg_fn = free_fn;
  396. pool->update_fn = fn;
  397. ++pool->generation;
  398. tor_cond_signal_all(&pool->condition);
  399. tor_mutex_release(&pool->lock);
  400. if (old_args) {
  401. for (i = 0; i < n_threads; ++i) {
  402. if (old_args[i] && old_args_free_fn)
  403. old_args_free_fn(old_args[i]);
  404. }
  405. tor_free(old_args);
  406. }
  407. return 0;
  408. }
  409. /** Don't have more than this many threads per pool. */
  410. #define MAX_THREADS 1024
  411. /** For half of our threads, choose lower priority queues with probability
  412. * 1/N for each of these values. Both are chosen somewhat arbitrarily. If
  413. * CHANCE_PERMISSIVE is too low, then we have a risk of low-priority tasks
  414. * stalling forever. If it's too high, we have a risk of low-priority tasks
  415. * grabbing half of the threads. */
  416. #define CHANCE_PERMISSIVE 37
  417. #define CHANCE_STRICT INT32_MAX
  418. /** Launch threads until we have <b>n</b>. */
  419. static int
  420. threadpool_start_threads(threadpool_t *pool, int n)
  421. {
  422. if (BUG(n < 0))
  423. return -1; // LCOV_EXCL_LINE
  424. if (n > MAX_THREADS)
  425. n = MAX_THREADS;
  426. tor_mutex_acquire(&pool->lock);
  427. if (pool->n_threads < n)
  428. pool->threads = tor_reallocarray(pool->threads,
  429. sizeof(workerthread_t*), n);
  430. while (pool->n_threads < n) {
  431. /* For half of our threads, we'll choose lower priorities permissively;
  432. * for the other half, we'll stick more strictly to higher priorities.
  433. * This keeps slow low-priority tasks from taking over completely. */
  434. int32_t chance = (pool->n_threads & 1) ? CHANCE_STRICT : CHANCE_PERMISSIVE;
  435. void *state = pool->new_thread_state_fn(pool->new_thread_state_arg);
  436. workerthread_t *thr = workerthread_new(chance,
  437. state, pool, pool->reply_queue);
  438. if (!thr) {
  439. //LCOV_EXCL_START
  440. tor_assert_nonfatal_unreached();
  441. pool->free_thread_state_fn(state);
  442. tor_mutex_release(&pool->lock);
  443. return -1;
  444. //LCOV_EXCL_STOP
  445. }
  446. thr->index = pool->n_threads;
  447. pool->threads[pool->n_threads++] = thr;
  448. }
  449. tor_mutex_release(&pool->lock);
  450. return 0;
  451. }
  452. /**
  453. * Construct a new thread pool with <b>n</b> worker threads, configured to
  454. * send their output to <b>replyqueue</b>. The threads' states will be
  455. * constructed with the <b>new_thread_state_fn</b> call, receiving <b>arg</b>
  456. * as its argument. When the threads close, they will call
  457. * <b>free_thread_state_fn</b> on their states.
  458. */
  459. threadpool_t *
  460. threadpool_new(int n_threads,
  461. replyqueue_t *replyqueue,
  462. void *(*new_thread_state_fn)(void*),
  463. void (*free_thread_state_fn)(void*),
  464. void *arg)
  465. {
  466. threadpool_t *pool;
  467. pool = tor_malloc_zero(sizeof(threadpool_t));
  468. tor_mutex_init_nonrecursive(&pool->lock);
  469. tor_cond_init(&pool->condition);
  470. unsigned i;
  471. for (i = WORKQUEUE_PRIORITY_FIRST; i <= WORKQUEUE_PRIORITY_LAST; ++i) {
  472. TOR_TAILQ_INIT(&pool->work[i]);
  473. }
  474. {
  475. unsigned seed;
  476. crypto_rand((void*)&seed, sizeof(seed));
  477. tor_init_weak_random(&pool->weak_rng, seed);
  478. }
  479. pool->new_thread_state_fn = new_thread_state_fn;
  480. pool->new_thread_state_arg = arg;
  481. pool->free_thread_state_fn = free_thread_state_fn;
  482. pool->reply_queue = replyqueue;
  483. if (threadpool_start_threads(pool, n_threads) < 0) {
  484. //LCOV_EXCL_START
  485. tor_assert_nonfatal_unreached();
  486. tor_cond_uninit(&pool->condition);
  487. tor_mutex_uninit(&pool->lock);
  488. tor_free(pool);
  489. return NULL;
  490. //LCOV_EXCL_STOP
  491. }
  492. return pool;
  493. }
  494. /** Return the reply queue associated with a given thread pool. */
  495. replyqueue_t *
  496. threadpool_get_replyqueue(threadpool_t *tp)
  497. {
  498. return tp->reply_queue;
  499. }
  500. /** Allocate a new reply queue. Reply queues are used to pass results from
  501. * worker threads to the main thread. Since the main thread is running an
  502. * IO-centric event loop, it needs to get woken up with means other than a
  503. * condition variable. */
  504. replyqueue_t *
  505. replyqueue_new(uint32_t alertsocks_flags)
  506. {
  507. replyqueue_t *rq;
  508. rq = tor_malloc_zero(sizeof(replyqueue_t));
  509. if (alert_sockets_create(&rq->alert, alertsocks_flags) < 0) {
  510. //LCOV_EXCL_START
  511. tor_free(rq);
  512. return NULL;
  513. //LCOV_EXCL_STOP
  514. }
  515. tor_mutex_init(&rq->lock);
  516. TOR_TAILQ_INIT(&rq->answers);
  517. return rq;
  518. }
  519. /**
  520. * Return the "read socket" for a given reply queue. The main thread should
  521. * listen for read events on this socket, and call replyqueue_process() every
  522. * time it triggers.
  523. */
  524. tor_socket_t
  525. replyqueue_get_socket(replyqueue_t *rq)
  526. {
  527. return rq->alert.read_fd;
  528. }
  529. /**
  530. * Process all pending replies on a reply queue. The main thread should call
  531. * this function every time the socket returned by replyqueue_get_socket() is
  532. * readable.
  533. */
  534. void
  535. replyqueue_process(replyqueue_t *queue)
  536. {
  537. int r = queue->alert.drain_fn(queue->alert.read_fd);
  538. if (r < 0) {
  539. //LCOV_EXCL_START
  540. static ratelim_t warn_limit = RATELIM_INIT(7200);
  541. log_fn_ratelim(&warn_limit, LOG_WARN, LD_GENERAL,
  542. "Failure from drain_fd: %s",
  543. tor_socket_strerror(-r));
  544. //LCOV_EXCL_STOP
  545. }
  546. tor_mutex_acquire(&queue->lock);
  547. while (!TOR_TAILQ_EMPTY(&queue->answers)) {
  548. /* lock must be held at this point.*/
  549. workqueue_entry_t *work = TOR_TAILQ_FIRST(&queue->answers);
  550. TOR_TAILQ_REMOVE(&queue->answers, work, next_work);
  551. tor_mutex_release(&queue->lock);
  552. work->on_pool = NULL;
  553. work->reply_fn(work->arg);
  554. workqueue_entry_free(work);
  555. tor_mutex_acquire(&queue->lock);
  556. }
  557. tor_mutex_release(&queue->lock);
  558. }