tor
master
|
Implementation for the pthreads-based multithreading backend functions. More...
#include "orconfig.h"
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include "compat.h"
#include "torlog.h"
#include "util.h"
Data Structures | |
struct | tor_pthread_data_t |
Macros | |
#define | PTHREAD_CREATE_DETACHED 1 |
Typedefs | |
typedef struct tor_pthread_data_t | tor_pthread_data_t |
Functions | |
int | spawn_func (void(*func)(void *), void *data) |
void | spawn_exit (void) |
void | tor_mutex_init (tor_mutex_t *mutex) |
void | tor_mutex_init_nonrecursive (tor_mutex_t *mutex) |
void | tor_mutex_acquire (tor_mutex_t *m) |
void | tor_mutex_release (tor_mutex_t *m) |
void | tor_mutex_uninit (tor_mutex_t *m) |
unsigned long | tor_get_thread_id (void) |
int | tor_cond_init (tor_cond_t *cond) |
void | tor_cond_uninit (tor_cond_t *cond) |
int | tor_cond_wait (tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv) |
void | tor_cond_signal_one (tor_cond_t *cond) |
void | tor_cond_signal_all (tor_cond_t *cond) |
int | tor_threadlocal_init (tor_threadlocal_t *threadlocal) |
void | tor_threadlocal_destroy (tor_threadlocal_t *threadlocal) |
void * | tor_threadlocal_get (tor_threadlocal_t *threadlocal) |
void | tor_threadlocal_set (tor_threadlocal_t *threadlocal, void *value) |
void | tor_threads_init (void) |
Implementation for the pthreads-based multithreading backend functions.
typedef struct tor_pthread_data_t tor_pthread_data_t |
Wraps a void (*)(void*) function and its argument so we can invoke them in a way pthreads would expect.
void spawn_exit | ( | void | ) |
End the current thread/process.
int spawn_func | ( | void(*)(void *) | func, |
void * | data | ||
) |
Minimalist interface to run a void function in the background. On Unix calls pthread_create, on win32 calls beginthread. Returns -1 on failure. func should not return, but rather should call spawn_exit.
NOTE: if data is used, it should not be allocated on the stack, since in a multithreaded environment, there is no way to be sure that the caller's stack will still be around when the called function is running.
int tor_cond_init | ( | tor_cond_t * | cond | ) |
Initialize an already-allocated condition variable.
void tor_cond_signal_all | ( | tor_cond_t * | cond | ) |
Wake up all of the waiters on cond.
void tor_cond_signal_one | ( | tor_cond_t * | cond | ) |
Wake up one of the waiters on cond.
void tor_cond_uninit | ( | tor_cond_t * | cond | ) |
Release all resources held by cond, but do not free cond itself.
int tor_cond_wait | ( | tor_cond_t * | cond, |
tor_mutex_t * | mutex, | ||
const struct timeval * | tv | ||
) |
Wait until one of the tor_cond_signal functions is called on cond. (If tv is set, and that amount of time passes with no signal to cond, return anyway. All waiters on the condition must wait holding the same mutex. All signallers should hold that mutex. The mutex needs to have been allocated with tor_mutex_init_for_cond().
Returns 0 on success, -1 on failure, 1 on timeout.
unsigned long tor_get_thread_id | ( | void | ) |
Return an integer representing this thread.
void tor_mutex_acquire | ( | tor_mutex_t * | m | ) |
Wait until m is free, then acquire it.
void tor_mutex_init | ( | tor_mutex_t * | mutex | ) |
Initialize mutex so it can be locked. Every mutex must be set up with tor_mutex_init() or tor_mutex_new(); not both.
void tor_mutex_init_nonrecursive | ( | tor_mutex_t * | mutex | ) |
As tor_mutex_init, but initialize a mutex suitable that may be non-recursive, if the OS supports that.
void tor_mutex_release | ( | tor_mutex_t * | m | ) |
Release the lock m so another thread can have it.
void tor_mutex_uninit | ( | tor_mutex_t * | m | ) |
Clean up the mutex m so that it no longer uses any system resources. Does not free m. This function must only be called on mutexes from tor_mutex_init().
void tor_threadlocal_destroy | ( | tor_threadlocal_t * | threadlocal | ) |
Release all resource associated with a thread-local variable.
void* tor_threadlocal_get | ( | tor_threadlocal_t * | threadlocal | ) |
Return the current value of a thread-local variable for this thread.
It's undefined behavior to use this function if the threadlocal hasn't been initialized, or has been destroyed.
int tor_threadlocal_init | ( | tor_threadlocal_t * | threadlocal | ) |
Initialize a thread-local variable.
After you call this function on a tor_threadlocal_t, you can call tor_threadlocal_set to change the current value of this variable for the current thread, and tor_threadlocal_get to retrieve the current value for the current thread. Each thread has its own value.
void tor_threadlocal_set | ( | tor_threadlocal_t * | threadlocal, |
void * | value | ||
) |
Change the current value of a thread-local variable for this thread to value.
It's undefined behavior to use this function if the threadlocal hasn't been initialized, or has been destroyed.
void tor_threads_init | ( | void | ) |
Set up common structures for use by threading.