tor  0.4.2.1-alpha-dev
Data Structures | Macros | Typedefs | Functions
threads.h File Reference
#include "orconfig.h"
#include "lib/cc/torint.h"
#include "lib/testsupport/testsupport.h"
#include "lib/lock/compat_mutex.h"

Go to the source code of this file.

Data Structures

struct  tor_cond_t
 
struct  tor_threadlocal_s
 
struct  atomic_counter_t
 

Macros

#define tor_mutex_init_for_cond(m)   tor_mutex_init_nonrecursive(m)
 
#define tor_cond_free(c)   FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c))
 
#define ATOMIC_LINKAGE
 

Typedefs

typedef struct tor_cond_t tor_cond_t
 
typedef struct tor_threadlocal_s tor_threadlocal_t
 
typedef struct atomic_counter_t atomic_counter_t
 

Functions

int spawn_func (void(*func)(void *), void *data)
 
void spawn_exit (void) ATTR_NORETURN
 
unsigned long tor_get_thread_id (void)
 
void tor_threads_init (void)
 
void set_main_thread (void)
 
int in_main_thread (void)
 
tor_cond_ttor_cond_new (void)
 
void tor_cond_free_ (tor_cond_t *cond)
 
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)
 
ATOMIC_LINKAGE void atomic_counter_init (atomic_counter_t *counter)
 
ATOMIC_LINKAGE void atomic_counter_destroy (atomic_counter_t *counter)
 
ATOMIC_LINKAGE void atomic_counter_add (atomic_counter_t *counter, size_t add)
 
ATOMIC_LINKAGE void atomic_counter_sub (atomic_counter_t *counter, size_t sub)
 
ATOMIC_LINKAGE size_t atomic_counter_get (atomic_counter_t *counter)
 
ATOMIC_LINKAGE size_t atomic_counter_exchange (atomic_counter_t *counter, size_t newval)
 

Detailed Description

Header for threads.c.

Definition in file threads.h.

Macro Definition Documentation

◆ tor_mutex_init_for_cond

#define tor_mutex_init_for_cond (   m)    tor_mutex_init_nonrecursive(m)

Conditions need nonrecursive mutexes with pthreads.

Definition at line 36 of file threads.h.

Typedef Documentation

◆ atomic_counter_t

Atomic counter type; holds a size_t value.

Function Documentation

◆ atomic_counter_add()

ATOMIC_LINKAGE void atomic_counter_add ( atomic_counter_t counter,
size_t  add 
)

Add a value to an atomic counter.

Definition at line 79 of file compat_threads.c.

References tor_mutex_acquire(), and tor_mutex_release().

Referenced by atomic_counter_sub().

◆ atomic_counter_destroy()

ATOMIC_LINKAGE void atomic_counter_destroy ( atomic_counter_t counter)

Clean up all resources held by an atomic counter.

Definition at line 72 of file compat_threads.c.

References tor_mutex_uninit().

Referenced by cleanup_protocol_warning_severity_level().

◆ atomic_counter_exchange()

ATOMIC_LINKAGE size_t atomic_counter_exchange ( atomic_counter_t counter,
size_t  newval 
)

Replace the value of an atomic counter; return the old one.

Definition at line 104 of file compat_threads.c.

References tor_mutex_acquire(), and tor_mutex_release().

Referenced by set_protocol_warning_severity_level().

◆ atomic_counter_get()

ATOMIC_LINKAGE size_t atomic_counter_get ( atomic_counter_t counter)

◆ atomic_counter_init()

ATOMIC_LINKAGE void atomic_counter_init ( atomic_counter_t counter)

Initialize a new atomic counter with the value 0

Definition at line 65 of file compat_threads.c.

References tor_mutex_init_nonrecursive().

Referenced by init_protocol_warning_severity_level(), tor_compress_init(), tor_lzma_init(), tor_zlib_init(), and tor_zstd_init().

◆ atomic_counter_sub()

ATOMIC_LINKAGE void atomic_counter_sub ( atomic_counter_t counter,
size_t  sub 
)

Subtract a value from an atomic counter.

Definition at line 87 of file compat_threads.c.

References atomic_counter_add().

Referenced by tor_lzma_compress_free_(), tor_zlib_compress_free_(), and tor_zstd_compress_free_().

◆ in_main_thread()

int in_main_thread ( void  )

Return true iff called from the main thread.

Definition at line 57 of file compat_threads.c.

References main_thread_id, and tor_get_thread_id().

Referenced by control_event_logmsg(), and control_event_logmsg_pending().

◆ set_main_thread()

void set_main_thread ( void  )

Start considering the current thread to be the 'main thread'. This has no effect on anything besides in_main_thread().

Definition at line 51 of file compat_threads.c.

References main_thread_id, and tor_get_thread_id().

◆ spawn_exit()

void spawn_exit ( void  )

End the current thread/process.

Definition at line 93 of file compat_pthreads.c.

◆ spawn_func()

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.

Definition at line 72 of file compat_pthreads.c.

◆ tor_cond_free_()

void tor_cond_free_ ( tor_cond_t c)

Free all storage held in c.

Definition at line 37 of file compat_threads.c.

References tor_cond_uninit(), and tor_free.

◆ tor_cond_init()

int tor_cond_init ( tor_cond_t cond)

Initialize an already-allocated condition variable.

Definition at line 114 of file compat_pthreads.c.

◆ tor_cond_new()

tor_cond_t* tor_cond_new ( void  )

Allocate and return a new condition variable.

Definition at line 27 of file compat_threads.c.

◆ tor_cond_signal_all()

void tor_cond_signal_all ( tor_cond_t cond)

Wake up all of the waiters on cond.

Definition at line 221 of file compat_pthreads.c.

◆ tor_cond_signal_one()

void tor_cond_signal_one ( tor_cond_t cond)

Wake up one of the waiters on cond.

Definition at line 215 of file compat_pthreads.c.

◆ tor_cond_uninit()

void tor_cond_uninit ( tor_cond_t cond)

Release all resources held by cond, but do not free cond itself.

Definition at line 150 of file compat_pthreads.c.

Referenced by tor_cond_free_().

◆ tor_cond_wait()

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.

Definition at line 167 of file compat_pthreads.c.

References timeradd.

◆ tor_get_thread_id()

unsigned long tor_get_thread_id ( void  )

Return an integer representing this thread.

Definition at line 100 of file compat_pthreads.c.

Referenced by in_main_thread(), and set_main_thread().

◆ tor_threadlocal_destroy()

void tor_threadlocal_destroy ( tor_threadlocal_t threadlocal)

Release all resource associated with a thread-local variable.

Definition at line 234 of file compat_pthreads.c.

Referenced by crypto_rand_fast_shutdown().

◆ tor_threadlocal_get()

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.

Definition at line 241 of file compat_pthreads.c.

Referenced by destroy_thread_fast_rng(), and get_thread_fast_rng().

◆ tor_threadlocal_init()

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.

Definition at line 227 of file compat_pthreads.c.

Referenced by crypto_rand_fast_init().

◆ tor_threadlocal_set()

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.

Definition at line 247 of file compat_pthreads.c.

References tor_assert().

◆ tor_threads_init()

void tor_threads_init ( void  )

Set up common structures for use by threading.

Definition at line 255 of file compat_pthreads.c.

References threads_initialized.