Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Macros | Enumerations | Functions
workqueue.h File Reference

Header for workqueue.c. More...

#include "lib/cc/torint.h"

Go to the source code of this file.

Macros

#define threadpool_free(pool)    FREE_AND_NULL(threadpool_t, threadpool_free_, (pool))
 

Enumerations

enum  workqueue_reply_t { WQ_RPL_REPLY = 0 , WQ_RPL_ERROR = 1 , WQ_RPL_SHUTDOWN = 2 }
 
enum  workqueue_priority_t { WQ_PRI_HIGH = 0 , WQ_PRI_MED = 1 , WQ_PRI_LOW = 2 }
 

Functions

workqueue_entry_tthreadpool_queue_work_priority (threadpool_t *pool, workqueue_priority_t prio, workqueue_reply_t(*fn)(void *, void *), void(*reply_fn)(void *), void *arg)
 
workqueue_entry_tthreadpool_queue_work (threadpool_t *pool, workqueue_reply_t(*fn)(void *, void *), void(*reply_fn)(void *), void *arg)
 
int threadpool_queue_update (threadpool_t *pool, void *(*dup_fn)(void *), workqueue_reply_t(*fn)(void *, void *), void(*free_fn)(void *), void *arg)
 
void * workqueue_entry_cancel (workqueue_entry_t *pending_work)
 
threadpool_tthreadpool_new (int n_threads, replyqueue_t *replyqueue, void *(*new_thread_state_fn)(void *), void(*free_thread_state_fn)(void *), void *arg)
 
void threadpool_free_ (threadpool_t *tp)
 
replyqueue_tthreadpool_get_replyqueue (threadpool_t *tp)
 
replyqueue_treplyqueue_new (uint32_t alertsocks_flags)
 
void replyqueue_process (replyqueue_t *queue)
 
int threadpool_register_reply_event (threadpool_t *tp, void(*cb)(threadpool_t *tp))
 
unsigned int threadpool_get_n_threads (threadpool_t *tp)
 

Detailed Description

Header for workqueue.c.

Definition in file workqueue.h.

Macro Definition Documentation

◆ threadpool_free

#define threadpool_free (   pool)     FREE_AND_NULL(threadpool_t, threadpool_free_, (pool))

Definition at line 62 of file workqueue.h.

Enumeration Type Documentation

◆ workqueue_priority_t

Possible priorities for work. Lower numeric values are more important.

Definition at line 31 of file workqueue.h.

◆ workqueue_reply_t

Possible return value from a work function:

Enumerator
WQ_RPL_ERROR 

indicates success

WQ_RPL_SHUTDOWN 

indicates fatal error

Definition at line 24 of file workqueue.h.

Function Documentation

◆ replyqueue_new()

replyqueue_t * replyqueue_new ( uint32_t  alertsocks_flags)

Allocate a new reply queue. Reply queues are used to pass results from worker threads to the main thread. Since the main thread is running an IO-centric event loop, it needs to get woken up with means other than a condition variable.

Definition at line 771 of file workqueue.c.

◆ replyqueue_process()

void replyqueue_process ( replyqueue_t queue)

Process all pending replies on a reply queue. The main thread should call this function every time the socket returned by replyqueue_get_socket() is readable.

Definition at line 852 of file workqueue.c.

Referenced by reply_event_cb().

◆ threadpool_free_()

void threadpool_free_ ( threadpool_t pool)

Free up the resources allocated by worker threads, worker thread pool, ...

Definition at line 708 of file workqueue.c.

◆ threadpool_get_n_threads()

unsigned int threadpool_get_n_threads ( threadpool_t tp)

Return the number of threads configured for the given pool.

Definition at line 883 of file workqueue.c.

◆ threadpool_get_replyqueue()

replyqueue_t * threadpool_get_replyqueue ( threadpool_t tp)

Return the reply queue associated with a given thread pool.

Definition at line 761 of file workqueue.c.

◆ threadpool_new()

threadpool_t * threadpool_new ( int  n_threads,
replyqueue_t replyqueue,
void *(*)(void *)  new_thread_state_fn,
void(*)(void *)  free_thread_state_fn,
void *  arg 
)

Construct a new thread pool with n worker threads, configured to send their output to replyqueue. The threads' states will be constructed with the new_thread_state_fn call, receiving arg as its argument. When the threads close, they will call free_thread_state_fn on their states.

Definition at line 670 of file workqueue.c.

◆ threadpool_queue_update()

int threadpool_queue_update ( threadpool_t pool,
void *(*)(void *)  dup_fn,
workqueue_reply_t(*)(void *, void *)  fn,
void(*)(void *)  free_fn,
void *  arg 
)

Queue a copy of a work item for every thread in a pool. This can be used, for example, to tell the threads to update some parameter in their states.

Arguments are as for threadpool_queue_work, except that the arg value is passed to dup_fn once per each thread to make a copy of it.

UPDATE FUNCTIONS MUST BE IDEMPOTENT. We do not guarantee that every update will be run. If a new update is scheduled before the old update finishes running, then the new will replace the old in any threads that haven't run it yet.

Return 0 on success, -1 on failure.

Definition at line 497 of file workqueue.c.

◆ threadpool_queue_work()

workqueue_entry_t * threadpool_queue_work ( threadpool_t pool,
workqueue_reply_t(*)(void *, void *)  fn,
void(*)(void *)  reply_fn,
void *  arg 
)

As threadpool_queue_work_priority(), but assumes WQ_PRI_HIGH

Definition at line 473 of file workqueue.c.

◆ threadpool_queue_work_priority()

workqueue_entry_t * threadpool_queue_work_priority ( threadpool_t pool,
workqueue_priority_t  prio,
workqueue_reply_t(*)(void *, void *)  fn,
void(*)(void *)  reply_fn,
void *  arg 
)

Queue an item of work for a thread in a thread pool. The function fn will be run in a worker thread, and will receive as arguments the thread's state object, and the provided object arg. It must return one of WQ_RPL_REPLY, WQ_RPL_ERROR, or WQ_RPL_SHUTDOWN.

Regardless of its return value, the function reply_fn will later be run in the main thread when it invokes replyqueue_process(), and will receive as its argument the same arg object. It's the reply function's responsibility to free the work object.

On success, return a workqueue_entry_t object that can be passed to workqueue_entry_cancel(). On failure, return NULL. (Failure is not currently possible, but callers should check anyway.)

Items are executed in a loose priority order – each thread will usually take from the queued work with the highest prioirity, but will occasionally visit lower-priority queues to keep them from starving completely.

Note that because of priorities and thread behavior, work items may not be executed strictly in order.

Definition at line 446 of file workqueue.c.

Referenced by threadpool_queue_work().

◆ threadpool_register_reply_event()

int threadpool_register_reply_event ( threadpool_t tp,
void(*)(threadpool_t *tp)  cb 
)

Register the threadpool tp's reply queue with Tor's global libevent mainloop. If cb is provided, it is run after each time there is work to process from the reply queue. Return 0 on success, -1 on failure.

Definition at line 828 of file workqueue.c.

◆ workqueue_entry_cancel()

void * workqueue_entry_cancel ( workqueue_entry_t ent)

Cancel a workqueue_entry_t that has been returned from threadpool_queue_work.

You must not call this function on any work whose reply function has been executed in the main thread; that will cause undefined behavior (probably, a crash).

If the work is cancelled, this function return the argument passed to the work function. It is the caller's responsibility to free this storage.

This function will have no effect if the worker thread has already executed or begun to execute the work item. In that case, it will return NULL.

Definition at line 207 of file workqueue.c.

Referenced by cpuworker_cancel_circ_handshake().