SphinxBase 5prealpha
sbthread.c File Reference

Simple portable thread functions. More...

#include <string.h>
#include "sphinxbase/sbthread.h"
#include "sphinxbase/ckd_alloc.h"
#include "sphinxbase/err.h"
#include <pthread.h>
#include <sys/time.h>

Go to the source code of this file.

Data Structures

struct  sbthread_s
 
struct  sbmsgq_s
 
struct  sbevent_s
 
struct  sbmtx_s
 

Functions

sbthread_tsbthread_start (cmd_ln_t *config, sbthread_main func, void *arg)
 Start a new thread.
 
int sbthread_wait (sbthread_t *th)
 Wait for a thread to complete.
 
sbmsgq_tsbmsgq_init (size_t depth)
 Create a message queue.
 
void sbmsgq_free (sbmsgq_t *msgq)
 Free a message queue.
 
int sbmsgq_send (sbmsgq_t *q, size_t len, void const *data)
 Post a message to a queue.
 
void * sbmsgq_wait (sbmsgq_t *q, size_t *out_len, int sec, int nsec)
 Wait for a message from a queue.
 
sbevent_tsbevent_init (void)
 Initialize an event.
 
void sbevent_free (sbevent_t *evt)
 Free an event.
 
int sbevent_signal (sbevent_t *evt)
 Signal an event.
 
int sbevent_wait (sbevent_t *evt, int sec, int nsec)
 Wait for an event to be signalled.
 
sbmtx_tsbmtx_init (void)
 Create a mutex.
 
int sbmtx_trylock (sbmtx_t *mtx)
 Try to acquire a mutex.
 
int sbmtx_lock (sbmtx_t *mtx)
 Acquire a mutex.
 
int sbmtx_unlock (sbmtx_t *mtx)
 Release a mutex.
 
void sbmtx_free (sbmtx_t *mtx)
 Dispose of a mutex.
 
cmd_ln_tsbthread_config (sbthread_t *th)
 Get configuration object from a thread.
 
void * sbthread_arg (sbthread_t *th)
 Get argument pointer from a thread.
 
sbmsgq_tsbthread_msgq (sbthread_t *th)
 Get message queue from a thread.
 
int sbthread_send (sbthread_t *th, size_t len, void const *data)
 Send an asynchronous message to a thread.
 
void sbthread_free (sbthread_t *th)
 Free a thread object.
 

Detailed Description

Simple portable thread functions.

Author
David Huggins-Daines dhugg.nosp@m.ins@.nosp@m.cs.cm.nosp@m.u.ed.nosp@m.u

Definition in file sbthread.c.

Function Documentation

◆ sbevent_free()

void sbevent_free ( sbevent_t evt)

Free an event.

Definition at line 632 of file sbthread.c.

References ckd_free().

◆ sbevent_init()

sbevent_t * sbevent_init ( void  )

Initialize an event.

Definition at line 611 of file sbthread.c.

References ckd_calloc, ckd_free(), E_ERROR, and E_ERROR_SYSTEM.

◆ sbevent_signal()

int sbevent_signal ( sbevent_t evt)

Signal an event.

Definition at line 640 of file sbthread.c.

◆ sbevent_wait()

int sbevent_wait ( sbevent_t evt,
int  sec,
int  nsec 
)

Wait for an event to be signalled.

Definition at line 652 of file sbthread.c.

◆ sbmsgq_free()

void sbmsgq_free ( sbmsgq_t msgq)

Free a message queue.

Definition at line 465 of file sbthread.c.

References ckd_free().

Referenced by sbthread_free().

◆ sbmsgq_init()

sbmsgq_t * sbmsgq_init ( size_t  depth)

Create a message queue.

Parameters
depthDepth of the queue.

Definition at line 444 of file sbthread.c.

References ckd_calloc, and ckd_free().

Referenced by sbthread_start().

◆ sbmsgq_send()

int sbmsgq_send ( sbmsgq_t q,
size_t  len,
void const *  data 
)

Post a message to a queue.

Definition at line 475 of file sbthread.c.

Referenced by sbthread_send().

◆ sbmsgq_wait()

void * sbmsgq_wait ( sbmsgq_t q,
size_t *  out_len,
int  sec,
int  nsec 
)

Wait for a message from a queue.

Definition at line 555 of file sbthread.c.

◆ sbmtx_free()

void sbmtx_free ( sbmtx_t mtx)

Dispose of a mutex.

Definition at line 702 of file sbthread.c.

References ckd_free().

◆ sbmtx_init()

sbmtx_t * sbmtx_init ( void  )

Create a mutex.

Definition at line 671 of file sbthread.c.

References ckd_calloc, and ckd_free().

◆ sbmtx_lock()

int sbmtx_lock ( sbmtx_t mtx)

Acquire a mutex.

Definition at line 690 of file sbthread.c.

◆ sbmtx_trylock()

int sbmtx_trylock ( sbmtx_t mtx)

Try to acquire a mutex.

Definition at line 684 of file sbthread.c.

◆ sbmtx_unlock()

int sbmtx_unlock ( sbmtx_t mtx)

Release a mutex.

Definition at line 696 of file sbthread.c.

◆ sbthread_arg()

void * sbthread_arg ( sbthread_t th)

Get argument pointer from a thread.

Definition at line 716 of file sbthread.c.

◆ sbthread_config()

cmd_ln_t * sbthread_config ( sbthread_t th)

Get configuration object from a thread.

Definition at line 710 of file sbthread.c.

◆ sbthread_free()

void sbthread_free ( sbthread_t th)

Free a thread object.

Definition at line 734 of file sbthread.c.

References ckd_free(), sbmsgq_free(), and sbthread_wait().

Referenced by sbthread_start().

◆ sbthread_msgq()

sbmsgq_t * sbthread_msgq ( sbthread_t th)

Get message queue from a thread.

Definition at line 722 of file sbthread.c.

◆ sbthread_send()

int sbthread_send ( sbthread_t th,
size_t  len,
void const *  data 
)

Send an asynchronous message to a thread.

Each thread gets a message queue by default, so this is just a wrapper around sbmsgq_send().

Definition at line 728 of file sbthread.c.

References sbmsgq_send().

◆ sbthread_start()

sbthread_t * sbthread_start ( cmd_ln_t config,
sbthread_main  func,
void *  arg 
)

Start a new thread.

Definition at line 406 of file sbthread.c.

References ckd_calloc, E_ERROR, sbmsgq_init(), and sbthread_free().

◆ sbthread_wait()

int sbthread_wait ( sbthread_t th)

Wait for a thread to complete.

Definition at line 425 of file sbthread.c.

References E_ERROR.

Referenced by sbthread_free().