workqueue.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. (Failure is not
  321. * currently possible, but callers should check anyway.)
  322. *
  323. * Items are executed in a loose priority order -- each thread will usually
  324. * take from the queued work with the highest prioirity, but will occasionally
  325. * visit lower-priority queues to keep them from starving completely.
  326. *
  327. * Note that because of priorities and thread behavior, work items may not
  328. * be executed strictly in order.
  329. */
  330. workqueue_entry_t *
  331. threadpool_queue_work_priority(threadpool_t *pool,
  332. workqueue_priority_t prio,
  333. workqueue_reply_t (*fn)(void *, void *),
  334. void (*reply_fn)(void *),
  335. void *arg)
  336. {
  337. tor_assert(((int)prio) >= WORKQUEUE_PRIORITY_FIRST &&
  338. ((int)prio) <= WORKQUEUE_PRIORITY_LAST);
  339. workqueue_entry_t *ent = workqueue_entry_new(fn, reply_fn, arg);
  340. ent->on_pool = pool;
  341. ent->pending = 1;
  342. ent->priority = prio;
  343. tor_mutex_acquire(&pool->lock);
  344. TOR_TAILQ_INSERT_TAIL(&pool->work[prio], ent, next_work);
  345. tor_cond_signal_one(&pool->condition);
  346. tor_mutex_release(&pool->lock);
  347. return ent;
  348. }
  349. /** As threadpool_queue_work_priority(), but assumes WQ_PRI_HIGH */
  350. workqueue_entry_t *
  351. threadpool_queue_work(threadpool_t *pool,
  352. workqueue_reply_t (*fn)(void *, void *),
  353. void (*reply_fn)(void *),
  354. void *arg)
  355. {
  356. return threadpool_queue_work_priority(pool, WQ_PRI_HIGH, fn, reply_fn, arg);
  357. }
  358. /**
  359. * Queue a copy of a work item for every thread in a pool. This can be used,
  360. * for example, to tell the threads to update some parameter in their states.
  361. *
  362. * Arguments are as for <b>threadpool_queue_work</b>, except that the
  363. * <b>arg</b> value is passed to <b>dup_fn</b> once per each thread to
  364. * make a copy of it.
  365. *
  366. * UPDATE FUNCTIONS MUST BE IDEMPOTENT. We do not guarantee that every update
  367. * will be run. If a new update is scheduled before the old update finishes
  368. * running, then the new will replace the old in any threads that haven't run
  369. * it yet.
  370. *
  371. * Return 0 on success, -1 on failure.
  372. */
  373. int
  374. threadpool_queue_update(threadpool_t *pool,
  375. void *(*dup_fn)(void *),
  376. workqueue_reply_t (*fn)(void *, void *),
  377. void (*free_fn)(void *),
  378. void *arg)
  379. {
  380. int i, n_threads;
  381. void (*old_args_free_fn)(void *arg);
  382. void **old_args;
  383. void **new_args;
  384. tor_mutex_acquire(&pool->lock);
  385. n_threads = pool->n_threads;
  386. old_args = pool->update_args;
  387. old_args_free_fn = pool->free_update_arg_fn;
  388. new_args = tor_calloc(n_threads, sizeof(void*));
  389. for (i = 0; i < n_threads; ++i) {
  390. if (dup_fn)
  391. new_args[i] = dup_fn(arg);
  392. else
  393. new_args[i] = arg;
  394. }
  395. pool->update_args = new_args;
  396. pool->free_update_arg_fn = free_fn;
  397. pool->update_fn = fn;
  398. ++pool->generation;
  399. tor_cond_signal_all(&pool->condition);
  400. tor_mutex_release(&pool->lock);
  401. if (old_args) {
  402. for (i = 0; i < n_threads; ++i) {
  403. if (old_args[i] && old_args_free_fn)
  404. old_args_free_fn(old_args[i]);
  405. }
  406. tor_free(old_args);
  407. }
  408. return 0;
  409. }
  410. /** Don't have more than this many threads per pool. */
  411. #define MAX_THREADS 1024
  412. /** For half of our threads, choose lower priority queues with probability
  413. * 1/N for each of these values. Both are chosen somewhat arbitrarily. If
  414. * CHANCE_PERMISSIVE is too low, then we have a risk of low-priority tasks
  415. * stalling forever. If it's too high, we have a risk of low-priority tasks
  416. * grabbing half of the threads. */
  417. #define CHANCE_PERMISSIVE 37
  418. #define CHANCE_STRICT INT32_MAX
  419. /** Launch threads until we have <b>n</b>. */
  420. static int
  421. threadpool_start_threads(threadpool_t *pool, int n)
  422. {
  423. if (BUG(n < 0))
  424. return -1; // LCOV_EXCL_LINE
  425. if (n > MAX_THREADS)
  426. n = MAX_THREADS;
  427. tor_mutex_acquire(&pool->lock);
  428. if (pool->n_threads < n)
  429. pool->threads = tor_reallocarray(pool->threads,
  430. sizeof(workerthread_t*), n);
  431. while (pool->n_threads < n) {
  432. /* For half of our threads, we'll choose lower priorities permissively;
  433. * for the other half, we'll stick more strictly to higher priorities.
  434. * This keeps slow low-priority tasks from taking over completely. */
  435. int32_t chance = (pool->n_threads & 1) ? CHANCE_STRICT : CHANCE_PERMISSIVE;
  436. void *state = pool->new_thread_state_fn(pool->new_thread_state_arg);
  437. workerthread_t *thr = workerthread_new(chance,
  438. state, pool, pool->reply_queue);
  439. if (!thr) {
  440. //LCOV_EXCL_START
  441. tor_assert_nonfatal_unreached();
  442. pool->free_thread_state_fn(state);
  443. tor_mutex_release(&pool->lock);
  444. return -1;
  445. //LCOV_EXCL_STOP
  446. }
  447. thr->index = pool->n_threads;
  448. pool->threads[pool->n_threads++] = thr;
  449. }
  450. tor_mutex_release(&pool->lock);
  451. return 0;
  452. }
  453. /**
  454. * Construct a new thread pool with <b>n</b> worker threads, configured to
  455. * send their output to <b>replyqueue</b>. The threads' states will be
  456. * constructed with the <b>new_thread_state_fn</b> call, receiving <b>arg</b>
  457. * as its argument. When the threads close, they will call
  458. * <b>free_thread_state_fn</b> on their states.
  459. */
  460. threadpool_t *
  461. threadpool_new(int n_threads,
  462. replyqueue_t *replyqueue,
  463. void *(*new_thread_state_fn)(void*),
  464. void (*free_thread_state_fn)(void*),
  465. void *arg)
  466. {
  467. threadpool_t *pool;
  468. pool = tor_malloc_zero(sizeof(threadpool_t));
  469. tor_mutex_init_nonrecursive(&pool->lock);
  470. tor_cond_init(&pool->condition);
  471. unsigned i;
  472. for (i = WORKQUEUE_PRIORITY_FIRST; i <= WORKQUEUE_PRIORITY_LAST; ++i) {
  473. TOR_TAILQ_INIT(&pool->work[i]);
  474. }
  475. {
  476. unsigned seed;
  477. crypto_rand((void*)&seed, sizeof(seed));
  478. tor_init_weak_random(&pool->weak_rng, seed);
  479. }
  480. pool->new_thread_state_fn = new_thread_state_fn;
  481. pool->new_thread_state_arg = arg;
  482. pool->free_thread_state_fn = free_thread_state_fn;
  483. pool->reply_queue = replyqueue;
  484. if (threadpool_start_threads(pool, n_threads) < 0) {
  485. //LCOV_EXCL_START
  486. tor_assert_nonfatal_unreached();
  487. tor_cond_uninit(&pool->condition);
  488. tor_mutex_uninit(&pool->lock);
  489. tor_free(pool);
  490. return NULL;
  491. //LCOV_EXCL_STOP
  492. }
  493. return pool;
  494. }
  495. /** Return the reply queue associated with a given thread pool. */
  496. replyqueue_t *
  497. threadpool_get_replyqueue(threadpool_t *tp)
  498. {
  499. return tp->reply_queue;
  500. }
  501. /** Allocate a new reply queue. Reply queues are used to pass results from
  502. * worker threads to the main thread. Since the main thread is running an
  503. * IO-centric event loop, it needs to get woken up with means other than a
  504. * condition variable. */
  505. replyqueue_t *
  506. replyqueue_new(uint32_t alertsocks_flags)
  507. {
  508. replyqueue_t *rq;
  509. rq = tor_malloc_zero(sizeof(replyqueue_t));
  510. if (alert_sockets_create(&rq->alert, alertsocks_flags) < 0) {
  511. //LCOV_EXCL_START
  512. tor_free(rq);
  513. return NULL;
  514. //LCOV_EXCL_STOP
  515. }
  516. tor_mutex_init(&rq->lock);
  517. TOR_TAILQ_INIT(&rq->answers);
  518. return rq;
  519. }
  520. /**
  521. * Return the "read socket" for a given reply queue. The main thread should
  522. * listen for read events on this socket, and call replyqueue_process() every
  523. * time it triggers.
  524. */
  525. tor_socket_t
  526. replyqueue_get_socket(replyqueue_t *rq)
  527. {
  528. return rq->alert.read_fd;
  529. }
  530. /**
  531. * Process all pending replies on a reply queue. The main thread should call
  532. * this function every time the socket returned by replyqueue_get_socket() is
  533. * readable.
  534. */
  535. void
  536. replyqueue_process(replyqueue_t *queue)
  537. {
  538. int r = queue->alert.drain_fn(queue->alert.read_fd);
  539. if (r < 0) {
  540. //LCOV_EXCL_START
  541. static ratelim_t warn_limit = RATELIM_INIT(7200);
  542. log_fn_ratelim(&warn_limit, LOG_WARN, LD_GENERAL,
  543. "Failure from drain_fd: %s",
  544. tor_socket_strerror(-r));
  545. //LCOV_EXCL_STOP
  546. }
  547. tor_mutex_acquire(&queue->lock);
  548. while (!TOR_TAILQ_EMPTY(&queue->answers)) {
  549. /* lock must be held at this point.*/
  550. workqueue_entry_t *work = TOR_TAILQ_FIRST(&queue->answers);
  551. TOR_TAILQ_REMOVE(&queue->answers, work, next_work);
  552. tor_mutex_release(&queue->lock);
  553. work->on_pool = NULL;
  554. work->reply_fn(work->arg);
  555. workqueue_entry_free(work);
  556. tor_mutex_acquire(&queue->lock);
  557. }
  558. tor_mutex_release(&queue->lock);
  559. }