workqueue.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. To avoid
  100. * contention, each gets its own queue. This breaks the guarantee that that
  101. * queued work will get executed strictly in order. */
  102. typedef struct workerthread_s {
  103. /** Which thread it this? In range 0..in_pool->n_threads-1 */
  104. int index;
  105. /** The pool this thread is a part of. */
  106. struct threadpool_s *in_pool;
  107. /** User-supplied state field that we pass to the worker functions of each
  108. * work item. */
  109. void *state;
  110. /** Reply queue to which we pass our results. */
  111. replyqueue_t *reply_queue;
  112. /** The current update generation of this thread */
  113. unsigned generation;
  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. /** With this probability, we'll consider taking work from a lower-priority
  188. * queue when we already have higher-priority work. This keeps priority from
  189. * becoming completely inverted. */
  190. #define LOWER_PRIORITY_CHANCE 37
  191. /** Extract the next workqueue_entry_t from the the thread's pool, removing
  192. * it from the relevant queues and marking it as non-pending.
  193. *
  194. * The caller must hold the lock. */
  195. static workqueue_entry_t *
  196. worker_thread_extract_next_work(workerthread_t *thread)
  197. {
  198. threadpool_t *pool = thread->in_pool;
  199. work_tailq_t *queue = NULL, *this_queue;
  200. unsigned i;
  201. for (i = WORKQUEUE_PRIORITY_FIRST; i <= WORKQUEUE_PRIORITY_LAST; ++i) {
  202. this_queue = &pool->work[i];
  203. if (!TOR_TAILQ_EMPTY(this_queue)) {
  204. queue = this_queue;
  205. if (! tor_weak_random_one_in_n(&pool->weak_rng, LOWER_PRIORITY_CHANCE)) {
  206. /* Usually we'll just break now, so that we can get out of the loop
  207. * and use the queue where we found work. But with a small
  208. * probability, we'll keep looking for lower priority work, so that
  209. * we don't ignore our low-priority queues entirely. */
  210. break;
  211. }
  212. }
  213. }
  214. if (queue == NULL)
  215. return NULL;
  216. workqueue_entry_t *work = TOR_TAILQ_FIRST(queue);
  217. TOR_TAILQ_REMOVE(queue, work, next_work);
  218. work->pending = 0;
  219. return work;
  220. }
  221. /**
  222. * Main function for the worker thread.
  223. */
  224. static void
  225. worker_thread_main(void *thread_)
  226. {
  227. workerthread_t *thread = thread_;
  228. threadpool_t *pool = thread->in_pool;
  229. workqueue_entry_t *work;
  230. workqueue_reply_t result;
  231. tor_mutex_acquire(&pool->lock);
  232. while (1) {
  233. /* lock must be held at this point. */
  234. while (worker_thread_has_work(thread)) {
  235. /* lock must be held at this point. */
  236. if (thread->in_pool->generation != thread->generation) {
  237. void *arg = thread->in_pool->update_args[thread->index];
  238. thread->in_pool->update_args[thread->index] = NULL;
  239. workqueue_reply_t (*update_fn)(void*,void*) =
  240. thread->in_pool->update_fn;
  241. thread->generation = thread->in_pool->generation;
  242. tor_mutex_release(&pool->lock);
  243. workqueue_reply_t r = update_fn(thread->state, arg);
  244. if (r != WQ_RPL_REPLY) {
  245. return;
  246. }
  247. tor_mutex_acquire(&pool->lock);
  248. continue;
  249. }
  250. work = worker_thread_extract_next_work(thread);
  251. if (BUG(work == NULL))
  252. break;
  253. tor_mutex_release(&pool->lock);
  254. /* We run the work function without holding the thread lock. This
  255. * is the main thread's first opportunity to give us more work. */
  256. result = work->fn(thread->state, work->arg);
  257. /* Queue the reply for the main thread. */
  258. queue_reply(thread->reply_queue, work);
  259. /* We may need to exit the thread. */
  260. if (result != WQ_RPL_REPLY) {
  261. return;
  262. }
  263. tor_mutex_acquire(&pool->lock);
  264. }
  265. /* At this point the lock is held, and there is no work in this thread's
  266. * queue. */
  267. /* TODO: support an idle-function */
  268. /* Okay. Now, wait till somebody has work for us. */
  269. if (tor_cond_wait(&pool->condition, &pool->lock, NULL) < 0) {
  270. log_warn(LD_GENERAL, "Fail tor_cond_wait.");
  271. }
  272. }
  273. }
  274. /** Put a reply on the reply queue. The reply must not currently be on
  275. * any thread's work queue. */
  276. static void
  277. queue_reply(replyqueue_t *queue, workqueue_entry_t *work)
  278. {
  279. int was_empty;
  280. tor_mutex_acquire(&queue->lock);
  281. was_empty = TOR_TAILQ_EMPTY(&queue->answers);
  282. TOR_TAILQ_INSERT_TAIL(&queue->answers, work, next_work);
  283. tor_mutex_release(&queue->lock);
  284. if (was_empty) {
  285. if (queue->alert.alert_fn(queue->alert.write_fd) < 0) {
  286. /* XXXX complain! */
  287. }
  288. }
  289. }
  290. /** Allocate and start a new worker thread to use state object <b>state</b>,
  291. * and send responses to <b>replyqueue</b>. */
  292. static workerthread_t *
  293. workerthread_new(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. if (spawn_func(worker_thread_main, thr) < 0) {
  300. //LCOV_EXCL_START
  301. tor_assert_nonfatal_unreached();
  302. log_err(LD_GENERAL, "Can't launch worker thread.");
  303. tor_free(thr);
  304. return NULL;
  305. //LCOV_EXCL_STOP
  306. }
  307. return thr;
  308. }
  309. /**
  310. * Queue an item of work for a thread in a thread pool. The function
  311. * <b>fn</b> will be run in a worker thread, and will receive as arguments the
  312. * thread's state object, and the provided object <b>arg</b>. It must return
  313. * one of WQ_RPL_REPLY, WQ_RPL_ERROR, or WQ_RPL_SHUTDOWN.
  314. *
  315. * Regardless of its return value, the function <b>reply_fn</b> will later be
  316. * run in the main thread when it invokes replyqueue_process(), and will
  317. * receive as its argument the same <b>arg</b> object. It's the reply
  318. * function's responsibility to free the work object.
  319. *
  320. * On success, return a workqueue_entry_t object that can be passed to
  321. * workqueue_entry_cancel(). On failure, return NULL.
  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 each thread has its own work queue, 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(prio >= WORKQUEUE_PRIORITY_FIRST &&
  338. 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. /** Launch threads until we have <b>n</b>. */
  413. static int
  414. threadpool_start_threads(threadpool_t *pool, int n)
  415. {
  416. if (BUG(n < 0))
  417. return -1; // LCOV_EXCL_LINE
  418. if (n > MAX_THREADS)
  419. n = MAX_THREADS;
  420. tor_mutex_acquire(&pool->lock);
  421. if (pool->n_threads < n)
  422. pool->threads = tor_reallocarray(pool->threads,
  423. sizeof(workerthread_t*), n);
  424. while (pool->n_threads < n) {
  425. void *state = pool->new_thread_state_fn(pool->new_thread_state_arg);
  426. workerthread_t *thr = workerthread_new(state, pool, pool->reply_queue);
  427. if (!thr) {
  428. //LCOV_EXCL_START
  429. tor_assert_nonfatal_unreached();
  430. pool->free_thread_state_fn(state);
  431. tor_mutex_release(&pool->lock);
  432. return -1;
  433. //LCOV_EXCL_STOP
  434. }
  435. thr->index = pool->n_threads;
  436. pool->threads[pool->n_threads++] = thr;
  437. }
  438. tor_mutex_release(&pool->lock);
  439. return 0;
  440. }
  441. /**
  442. * Construct a new thread pool with <b>n</b> worker threads, configured to
  443. * send their output to <b>replyqueue</b>. The threads' states will be
  444. * constructed with the <b>new_thread_state_fn</b> call, receiving <b>arg</b>
  445. * as its argument. When the threads close, they will call
  446. * <b>free_thread_state_fn</b> on their states.
  447. */
  448. threadpool_t *
  449. threadpool_new(int n_threads,
  450. replyqueue_t *replyqueue,
  451. void *(*new_thread_state_fn)(void*),
  452. void (*free_thread_state_fn)(void*),
  453. void *arg)
  454. {
  455. threadpool_t *pool;
  456. pool = tor_malloc_zero(sizeof(threadpool_t));
  457. tor_mutex_init_nonrecursive(&pool->lock);
  458. tor_cond_init(&pool->condition);
  459. unsigned i;
  460. for (i = WORKQUEUE_PRIORITY_FIRST; i <= WORKQUEUE_PRIORITY_LAST; ++i) {
  461. TOR_TAILQ_INIT(&pool->work[i]);
  462. }
  463. {
  464. unsigned seed;
  465. crypto_rand((void*)&seed, sizeof(seed));
  466. tor_init_weak_random(&pool->weak_rng, seed);
  467. }
  468. pool->new_thread_state_fn = new_thread_state_fn;
  469. pool->new_thread_state_arg = arg;
  470. pool->free_thread_state_fn = free_thread_state_fn;
  471. pool->reply_queue = replyqueue;
  472. if (threadpool_start_threads(pool, n_threads) < 0) {
  473. //LCOV_EXCL_START
  474. tor_assert_nonfatal_unreached();
  475. tor_cond_uninit(&pool->condition);
  476. tor_mutex_uninit(&pool->lock);
  477. tor_free(pool);
  478. return NULL;
  479. //LCOV_EXCL_STOP
  480. }
  481. return pool;
  482. }
  483. /** Return the reply queue associated with a given thread pool. */
  484. replyqueue_t *
  485. threadpool_get_replyqueue(threadpool_t *tp)
  486. {
  487. return tp->reply_queue;
  488. }
  489. /** Allocate a new reply queue. Reply queues are used to pass results from
  490. * worker threads to the main thread. Since the main thread is running an
  491. * IO-centric event loop, it needs to get woken up with means other than a
  492. * condition variable. */
  493. replyqueue_t *
  494. replyqueue_new(uint32_t alertsocks_flags)
  495. {
  496. replyqueue_t *rq;
  497. rq = tor_malloc_zero(sizeof(replyqueue_t));
  498. if (alert_sockets_create(&rq->alert, alertsocks_flags) < 0) {
  499. //LCOV_EXCL_START
  500. tor_free(rq);
  501. return NULL;
  502. //LCOV_EXCL_STOP
  503. }
  504. tor_mutex_init(&rq->lock);
  505. TOR_TAILQ_INIT(&rq->answers);
  506. return rq;
  507. }
  508. /**
  509. * Return the "read socket" for a given reply queue. The main thread should
  510. * listen for read events on this socket, and call replyqueue_process() every
  511. * time it triggers.
  512. */
  513. tor_socket_t
  514. replyqueue_get_socket(replyqueue_t *rq)
  515. {
  516. return rq->alert.read_fd;
  517. }
  518. /**
  519. * Process all pending replies on a reply queue. The main thread should call
  520. * this function every time the socket returned by replyqueue_get_socket() is
  521. * readable.
  522. */
  523. void
  524. replyqueue_process(replyqueue_t *queue)
  525. {
  526. int r = queue->alert.drain_fn(queue->alert.read_fd);
  527. if (r < 0) {
  528. //LCOV_EXCL_START
  529. static ratelim_t warn_limit = RATELIM_INIT(7200);
  530. log_fn_ratelim(&warn_limit, LOG_WARN, LD_GENERAL,
  531. "Failure from drain_fd: %s",
  532. tor_socket_strerror(-r));
  533. //LCOV_EXCL_STOP
  534. }
  535. tor_mutex_acquire(&queue->lock);
  536. while (!TOR_TAILQ_EMPTY(&queue->answers)) {
  537. /* lock must be held at this point.*/
  538. workqueue_entry_t *work = TOR_TAILQ_FIRST(&queue->answers);
  539. TOR_TAILQ_REMOVE(&queue->answers, work, next_work);
  540. tor_mutex_release(&queue->lock);
  541. work->on_pool = NULL;
  542. work->reply_fn(work->arg);
  543. workqueue_entry_free(work);
  544. tor_mutex_acquire(&queue->lock);
  545. }
  546. tor_mutex_release(&queue->lock);
  547. }