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

Implement the tor_mutex API using pthread_mutex_t. More...

#include "lib/lock/compat_mutex.h"
#include "lib/cc/compat_compiler.h"
#include "lib/err/torerr.h"

Go to the source code of this file.

Functions

void tor_locking_init (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)
 

Variables

static pthread_mutexattr_t attr_recursive
 
static int attr_initialized = 0
 

Detailed Description

Implement the tor_mutex API using pthread_mutex_t.

Definition in file compat_mutex_pthreads.c.

Function Documentation

◆ tor_locking_init()

void tor_locking_init ( void  )

Initialize the locking module, if it is not already initialized.

Definition at line 29 of file compat_mutex_pthreads.c.

Referenced by tor_mutex_init_nonrecursive().

◆ tor_mutex_acquire()

void tor_mutex_acquire ( tor_mutex_t m)

Wait until m is free, then acquire it.

Definition at line 71 of file compat_mutex_pthreads.c.

◆ tor_mutex_init()

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.

Definition at line 41 of file compat_mutex_pthreads.c.

◆ tor_mutex_init_nonrecursive()

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.

Definition at line 56 of file compat_mutex_pthreads.c.

◆ tor_mutex_release()

void tor_mutex_release ( tor_mutex_t m)

Release the lock m so another thread can have it.

Definition at line 84 of file compat_mutex_pthreads.c.

◆ tor_mutex_uninit()

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().

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 107 of file compat_mutex_pthreads.c.

Variable Documentation

◆ attr_initialized

int attr_initialized = 0
static

True iff attr_recursive has been initialized.

Definition at line 23 of file compat_mutex_pthreads.c.

Referenced by tor_locking_init(), and tor_mutex_init_nonrecursive().

◆ attr_recursive

pthread_mutexattr_t attr_recursive
static

A mutex attribute that we're going to use to tell pthreads that we want "recursive" mutexes (i.e., once we can re-lock if we're already holding them.)

Definition at line 19 of file compat_mutex_pthreads.c.

Referenced by tor_locking_init().