Skip to content

File dp_thread.h

FileList > doppler > dp_thread.h

Go to the source code of this file

The threading primitives doppler uses, with one platform split. More...

  • #include "doppler/dp_once.h"
  • #include <errno.h>
  • #include <pthread.h>
  • #include <sched.h>
  • #include <time.h>
  • #include <unistd.h>

Public Types

Type Name
typedef pthread_cond_t dp_cond_t
A condition variable, waited on with a dp_mutex_t held.
typedef pthread_mutex_t dp_mutex_t
A non-recursive mutex (a default pthread_mutex_t on POSIX).
typedef pthread_t dp_thread_t
A joinable thread handle (a pthread_t on POSIX).

Public Static Functions

Type Name
void dp_cond_broadcast (dp_cond_t * c)
Wakes every thread waiting on c .
void dp_cond_destroy (dp_cond_t * c)
Releases c (a no-op on Windows); no thread may be waiting.
void dp_cond_init (dp_cond_t * c)
Initializes c with no waiters.
void dp_cond_signal (dp_cond_t * c)
Wakes at most one thread waiting on c .
void dp_cond_wait (dp_cond_t * c, dp_mutex_t * m)
Atomically unlocks m and waits onc , relockingm before returning. No timeout. Wakeups may be spurious, so wait in a loop that re-checks the predicate.
int dp_cpu_count (void)
Processors this process may run on, or 1 if unknown.
void dp_mutex_destroy (dp_mutex_t * m)
Releases m (a no-op on Windows).m must be unlocked.
void dp_mutex_init (dp_mutex_t * m)
Initializes m , unlocked.
void dp_mutex_lock (dp_mutex_t * m)
Locks m , blocking until it is free. Not recursive: locking a mutex this thread already holds deadlocks or is undefined.
void dp_mutex_unlock (dp_mutex_t * m)
Unlocks m , which this thread must hold.
int dp_thread_create (dp_thread_t * t, void *(*)(void *) fn, void * arg)
Starts fn on a new thread. Returns 0 on success.
int dp_thread_join (dp_thread_t t)
Waits for t to finish and releases it. Returns 0 on success, as pthread_join does, so a caller that asserted the join can still.
void dp_thread_sleep_us (unsigned us)
Sleeps for at least us _microseconds. For "let the other thread
get there", never for timing. A signal does not cut the sleep short: nanosleep() reports the time it had left, and the sleep resumes with that remainder until it is spent (#2647)._
void dp_thread_yield (void)
Gives up the rest of this thread's time slice.

Macros

Type Name
define DP_THREAD_FN (name, arg) static void \*name (void \*arg)
Declares a thread entry point portably. arg is a declarator name, so it cannot be parenthesised. NOLINTNEXTLINE(bugprone-macro-parentheses)
define DP_THREAD_RETURN return NULL
Returns from a DP_THREAD_FN body.

Detailed Description

A mutex, a condition variable, a joinable thread and a core count. That is the entire set doppler needs dp_tlm_capture (a writer thread behind a mutex and two condvars) and dp_parallel (a bounded parallel-for) both use exactly these and nothing more.

Why a shim rather than #ifdef at each call site: those two files carry thirty-three and twenty-odd pthread calls respectively, and scattering a platform conditional through a working producer/consumer is how a deadlock gets introduced by a port that "only changed includes". One home, both platforms, and the call sites stay readable.

The Windows side is Vista-era and deliberately not pthreads-win32:

pthread Win32
pthread_mutex_t SRWLOCK (smaller and faster than a
CRITICAL_SECTION, and doppler never
needs recursion)
pthread_cond_t CONDITION_VARIABLE
pthread_cond_wait SleepConditionVariableSRW
pthread_create/join _beginthreadex / WaitForSingleObject
sched_getaffinity (Linux), GetActiveProcessorCount
else sysconf(_SC_NPROCESSORS_ONLN)
pthread_once InitOnceExecuteOnce, both behind
dp_once() (dp_once.h, included here)

A one-time build has its own header and its own dp_once.c because the tables it guards live in components that start no thread (AWGN, LO, CCSDS): they include dp_once.h alone, and <windows.h> stays inside dp_once.c (doppler#2328).

_beginthreadex rather than CreateThread: a thread that touches the CRT (these do malloc, and the capture writer does file I/O) needs the CRT's per-thread state initialised, and CreateThread does not do it.

Neither SRWLOCK nor CONDITION_VARIABLE has a destructor, so the destroy calls are no-ops on Windows rather than absent the call sites keep their symmetry and a reader is not left wondering which platform leaked.

Public Types Documentation

typedef dp_cond_t

A condition variable, waited on with a dp_mutex_t held.

typedef pthread_cond_t dp_cond_t;


typedef dp_mutex_t

A non-recursive mutex (a default pthread_mutex_t on POSIX).

typedef pthread_mutex_t dp_mutex_t;


typedef dp_thread_t

A joinable thread handle (a pthread_t on POSIX).

typedef pthread_t dp_thread_t;


Public Static Functions Documentation

function dp_cond_broadcast

Wakes every thread waiting on c .

static inline void dp_cond_broadcast (
    dp_cond_t * c
) 

Parameters:

  • c Condition variable from dp_cond_init().

function dp_cond_destroy

Releases c (a no-op on Windows); no thread may be waiting.

static inline void dp_cond_destroy (
    dp_cond_t * c
) 

Parameters:

  • c Condition variable from dp_cond_init().

function dp_cond_init

Initializes c with no waiters.

static inline void dp_cond_init (
    dp_cond_t * c
) 

Parameters:

  • c Storage for the condition variable.

function dp_cond_signal

Wakes at most one thread waiting on c .

static inline void dp_cond_signal (
    dp_cond_t * c
) 

Parameters:

  • c Condition variable from dp_cond_init().

function dp_cond_wait

Atomically unlocks m and waits onc , relockingm before returning. No timeout. Wakeups may be spurious, so wait in a loop that re-checks the predicate.

static inline void dp_cond_wait (
    dp_cond_t * c,
    dp_mutex_t * m
) 

Parameters:

  • c Condition variable to wait on.
  • m Mutex the caller holds.

function dp_cpu_count

Processors this process may run on, or 1 if unknown.

static inline int dp_cpu_count (
    void
) 

On Linux this is the size of the process's CPU affinity mask, so taskset, a container's cpuset and a cgroup cpuset all shrink it, and a "use every core" caller (nthreads = 0) starts one worker per CPU it can actually use rather than one per CPU the machine has (doppler#2679). The machine's online count is the fallback: on a platform without sched_getaffinity, where CPU_COUNT is not declared (it needs _GNU_SOURCE, which doppler's build and exported targets set), or when the mask does not fit a fixed cpu_set_t (more than 1024 CPUs).

A cgroup CPU quota (docker --cpus=2, cpu.max) is not a mask and is not read here.

Returns:

Number of usable logical processors, always >= 1.


function dp_mutex_destroy

Releases m (a no-op on Windows).m must be unlocked.

static inline void dp_mutex_destroy (
    dp_mutex_t * m
) 

Parameters:

  • m Mutex from dp_mutex_init().

function dp_mutex_init

Initializes m , unlocked.

static inline void dp_mutex_init (
    dp_mutex_t * m
) 

Parameters:

  • m Storage for the mutex.

function dp_mutex_lock

Locks m , blocking until it is free. Not recursive: locking a mutex this thread already holds deadlocks or is undefined.

static inline void dp_mutex_lock (
    dp_mutex_t * m
) 

Parameters:

  • m Mutex from dp_mutex_init().

function dp_mutex_unlock

Unlocks m , which this thread must hold.

static inline void dp_mutex_unlock (
    dp_mutex_t * m
) 

Parameters:

  • m Mutex from dp_mutex_init().

function dp_thread_create

Starts fn on a new thread. Returns 0 on success.

static inline int dp_thread_create (
    dp_thread_t * t,
    void *(*)(void *) fn,
    void * arg
) 

Parameters:

  • t Handle of the new thread; join it with dp_thread_join().
  • fn Entry point, declared with DP_THREAD_FN.
  • arg Passed to fn unchanged.

Returns:

0 on success, else pthread_create()'s error number.


function dp_thread_join

Waits for t to finish and releases it. Returns 0 on success, as pthread_join does, so a caller that asserted the join can still.

static inline int dp_thread_join (
    dp_thread_t t
) 

Parameters:

  • t Thread from dp_thread_create(); not usable afterward.

Returns:

0 on success, else pthread_join()'s error number.


function dp_thread_sleep_us

Sleeps for at least us microseconds. For "let the other thread get there", never for timing. A signal does not cut the sleep short: nanosleep() reports the time it had left, and the sleep resumes with that remainder until it is spent (#2647).

static inline void dp_thread_sleep_us (
    unsigned us
) 

Parameters:

  • us Requested sleep, microseconds.

function dp_thread_yield

Gives up the rest of this thread's time slice.

static inline void dp_thread_yield (
    void
) 


Macro Definition Documentation

define DP_THREAD_FN

Declares a thread entry point portably. arg is a declarator name, so it cannot be parenthesised. NOLINTNEXTLINE(bugprone-macro-parentheses)

#define DP_THREAD_FN (
    name,
    arg
) `static void *name (void *arg)`


define DP_THREAD_RETURN

Returns from a DP_THREAD_FN body.

#define DP_THREAD_RETURN `return NULL`



The documentation for this class was generated from the following file native/inc/doppler/dp_thread.h