Tor  0.4.7.0-alpha-dev
Functions | Variables
compat_threads.c File Reference

Cross-platform threading and inter-thread communication logic. (Platform-specific parts are written in the other compat_*threads modules.) More...

#include "orconfig.h"
#include <stdlib.h>
#include "lib/thread/threads.h"
#include "lib/thread/thread_sys.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/subsys/subsys.h"
#include <string.h>

Go to the source code of this file.

Functions

tor_cond_ttor_cond_new (void)
 
void tor_cond_free_ (tor_cond_t *c)
 
void set_main_thread (void)
 
int in_main_thread (void)
 
void atomic_counter_init (atomic_counter_t *counter)
 
void atomic_counter_destroy (atomic_counter_t *counter)
 
void atomic_counter_add (atomic_counter_t *counter, size_t add)
 
void atomic_counter_sub (atomic_counter_t *counter, size_t sub)
 
size_t atomic_counter_get (atomic_counter_t *counter)
 
size_t atomic_counter_exchange (atomic_counter_t *counter, size_t newval)
 
static int subsys_threads_initialize (void)
 

Variables

static unsigned long main_thread_id = -1
 
const subsys_fns_t sys_threads
 

Detailed Description

Cross-platform threading and inter-thread communication logic. (Platform-specific parts are written in the other compat_*threads modules.)

Definition in file compat_threads.c.

Function Documentation

◆ atomic_counter_add()

void atomic_counter_add ( atomic_counter_t counter,
size_t  add 
)

Add a value to an atomic counter.

Definition at line 87 of file compat_threads.c.

Referenced by atomic_counter_sub().

◆ atomic_counter_destroy()

void atomic_counter_destroy ( atomic_counter_t counter)

Clean up all resources held by an atomic counter.

Destroying a locked mutex is undefined behaviour. Global mutexes may be locked when they are passed to this function, because multiple threads can still access them. So we can either:

  • destroy on shutdown, and re-initialise when tor re-initialises, or
  • skip destroying and re-initialisation, using a sentinel variable. See #31735 for details.

Definition at line 80 of file compat_threads.c.

Referenced by cleanup_protocol_warning_severity_level().

◆ atomic_counter_exchange()

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 112 of file compat_threads.c.

Referenced by set_protocol_warning_severity_level().

◆ atomic_counter_get()

size_t atomic_counter_get ( atomic_counter_t counter)

◆ atomic_counter_init()

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.

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

◆ atomic_counter_sub()

void atomic_counter_sub ( atomic_counter_t counter,
size_t  sub 
)

Subtract a value from an atomic counter.

Definition at line 95 of file compat_threads.c.

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.

Referenced by consensus_diff_queue_diff_work(), consensus_diff_worker_replyfn(), 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.

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

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

Variable Documentation

◆ main_thread_id

unsigned long main_thread_id = -1
static

Identity of the "main" thread

Definition at line 46 of file compat_threads.c.

Referenced by in_main_thread(), and set_main_thread().

◆ sys_threads

const subsys_fns_t sys_threads
Initial value:
= {
.name = "threads",
.supported = true,
.level = -89,
.initialize = subsys_threads_initialize,
}
#define SUBSYS_DECLARE_LOCATION()
Definition: subsys.h:211

Definition at line 130 of file compat_threads.c.