Tor  0.4.7.0-alpha-dev
Data Structures | Macros | Functions | Variables
compat_pthreads.c File Reference

Implementation for the pthreads-based multithreading backend functions. More...

#include "orconfig.h"
#include "lib/thread/threads.h"
#include "lib/wallclock/timeval.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include <sys/time.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  tor_pthread_data_t
 

Macros

#define PTHREAD_CREATE_DETACHED   1
 

Functions

static void * tor_pthread_helper_fn (void *_data)
 
int spawn_func (void(*func)(void *), void *data)
 
void spawn_exit (void)
 
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)
 

Variables

static pthread_attr_t attr_detached
 
static int threads_initialized = 0
 

Detailed Description

Implementation for the pthreads-based multithreading backend functions.

Definition in file compat_pthreads.c.

Function Documentation

◆ 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_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_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.

◆ 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_pthread_helper_fn()

static void* tor_pthread_helper_fn ( void *  _data)
static

Given a tor_pthread_data_t _data, call _data->func(d->data) and free _data. Used to make sure we can call functions the way pthread expects.

Definition at line 37 of file compat_pthreads.c.

◆ 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.

◆ 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.

Variable Documentation

◆ attr_detached

pthread_attr_t attr_detached
static

A pthread attribute to make threads start detached.

Definition at line 57 of file compat_pthreads.c.

Referenced by tor_threads_init().

◆ threads_initialized

int threads_initialized = 0
static

True iff we've called tor_threads_init()

Definition at line 59 of file compat_pthreads.c.

Referenced by tor_threads_init().