\doxysection{src/libsphinxbase/util/sbthread.c File Reference}
\label{sbthread_8c}\index{src/libsphinxbase/util/sbthread.c@{src/libsphinxbase/util/sbthread.c}}


Simple portable thread functions.  


{\ttfamily \#include $<$string.\+h$>$}\newline
{\ttfamily \#include "{}sphinxbase/sbthread.\+h"{}}\newline
{\ttfamily \#include "{}sphinxbase/ckd\+\_\+alloc.\+h"{}}\newline
{\ttfamily \#include "{}sphinxbase/err.\+h"{}}\newline
{\ttfamily \#include $<$pthread.\+h$>$}\newline
{\ttfamily \#include $<$sys/time.\+h$>$}\newline
\doxysubsubsection*{Data Structures}
\begin{DoxyCompactItemize}
\item 
struct \textbf{ sbthread\+\_\+s}
\item 
struct \textbf{ sbmsgq\+\_\+s}
\item 
struct \textbf{ sbevent\+\_\+s}
\item 
struct \textbf{ sbmtx\+\_\+s}
\end{DoxyCompactItemize}
\doxysubsubsection*{Functions}
\begin{DoxyCompactItemize}
\item 
\textbf{ sbthread\+\_\+t} $\ast$ \textbf{ sbthread\+\_\+start} (\textbf{ cmd\+\_\+ln\+\_\+t} $\ast$config, \textbf{ sbthread\+\_\+main} func, void $\ast$arg)
\begin{DoxyCompactList}\small\item\em Start a new thread. \end{DoxyCompactList}\item 
int \textbf{ sbthread\+\_\+wait} (\textbf{ sbthread\+\_\+t} $\ast$th)
\begin{DoxyCompactList}\small\item\em Wait for a thread to complete. \end{DoxyCompactList}\item 
\textbf{ sbmsgq\+\_\+t} $\ast$ \textbf{ sbmsgq\+\_\+init} (size\+\_\+t depth)
\begin{DoxyCompactList}\small\item\em Create a message queue. \end{DoxyCompactList}\item 
void \textbf{ sbmsgq\+\_\+free} (\textbf{ sbmsgq\+\_\+t} $\ast$msgq)
\begin{DoxyCompactList}\small\item\em Free a message queue. \end{DoxyCompactList}\item 
int \textbf{ sbmsgq\+\_\+send} (\textbf{ sbmsgq\+\_\+t} $\ast$q, size\+\_\+t len, void const $\ast$data)
\begin{DoxyCompactList}\small\item\em Post a message to a queue. \end{DoxyCompactList}\item 
void $\ast$ \textbf{ sbmsgq\+\_\+wait} (\textbf{ sbmsgq\+\_\+t} $\ast$q, size\+\_\+t $\ast$out\+\_\+len, int sec, int nsec)
\begin{DoxyCompactList}\small\item\em Wait for a message from a queue. \end{DoxyCompactList}\item 
\textbf{ sbevent\+\_\+t} $\ast$ \textbf{ sbevent\+\_\+init} (void)
\begin{DoxyCompactList}\small\item\em Initialize an event. \end{DoxyCompactList}\item 
void \textbf{ sbevent\+\_\+free} (\textbf{ sbevent\+\_\+t} $\ast$evt)
\begin{DoxyCompactList}\small\item\em Free an event. \end{DoxyCompactList}\item 
int \textbf{ sbevent\+\_\+signal} (\textbf{ sbevent\+\_\+t} $\ast$evt)
\begin{DoxyCompactList}\small\item\em Signal an event. \end{DoxyCompactList}\item 
int \textbf{ sbevent\+\_\+wait} (\textbf{ sbevent\+\_\+t} $\ast$evt, int sec, int nsec)
\begin{DoxyCompactList}\small\item\em Wait for an event to be signalled. \end{DoxyCompactList}\item 
\textbf{ sbmtx\+\_\+t} $\ast$ \textbf{ sbmtx\+\_\+init} (void)
\begin{DoxyCompactList}\small\item\em Create a mutex. \end{DoxyCompactList}\item 
int \textbf{ sbmtx\+\_\+trylock} (\textbf{ sbmtx\+\_\+t} $\ast$mtx)
\begin{DoxyCompactList}\small\item\em Try to acquire a mutex. \end{DoxyCompactList}\item 
int \textbf{ sbmtx\+\_\+lock} (\textbf{ sbmtx\+\_\+t} $\ast$mtx)
\begin{DoxyCompactList}\small\item\em Acquire a mutex. \end{DoxyCompactList}\item 
int \textbf{ sbmtx\+\_\+unlock} (\textbf{ sbmtx\+\_\+t} $\ast$mtx)
\begin{DoxyCompactList}\small\item\em Release a mutex. \end{DoxyCompactList}\item 
void \textbf{ sbmtx\+\_\+free} (\textbf{ sbmtx\+\_\+t} $\ast$mtx)
\begin{DoxyCompactList}\small\item\em Dispose of a mutex. \end{DoxyCompactList}\item 
\textbf{ cmd\+\_\+ln\+\_\+t} $\ast$ \textbf{ sbthread\+\_\+config} (\textbf{ sbthread\+\_\+t} $\ast$th)
\begin{DoxyCompactList}\small\item\em Get configuration object from a thread. \end{DoxyCompactList}\item 
void $\ast$ \textbf{ sbthread\+\_\+arg} (\textbf{ sbthread\+\_\+t} $\ast$th)
\begin{DoxyCompactList}\small\item\em Get argument pointer from a thread. \end{DoxyCompactList}\item 
\textbf{ sbmsgq\+\_\+t} $\ast$ \textbf{ sbthread\+\_\+msgq} (\textbf{ sbthread\+\_\+t} $\ast$th)
\begin{DoxyCompactList}\small\item\em Get message queue from a thread. \end{DoxyCompactList}\item 
int \textbf{ sbthread\+\_\+send} (\textbf{ sbthread\+\_\+t} $\ast$th, size\+\_\+t len, void const $\ast$data)
\begin{DoxyCompactList}\small\item\em Send an asynchronous message to a thread. \end{DoxyCompactList}\item 
void \textbf{ sbthread\+\_\+free} (\textbf{ sbthread\+\_\+t} $\ast$th)
\begin{DoxyCompactList}\small\item\em Free a thread object. \end{DoxyCompactList}\end{DoxyCompactItemize}


\doxysubsection{Detailed Description}
Simple portable thread functions. 

\begin{DoxyAuthor}{Author}
David Huggins-\/\+Daines {\texttt{ dhuggins@cs.\+cmu.\+edu}} 
\end{DoxyAuthor}


Definition in file \textbf{ sbthread.\+c}.



\doxysubsection{Function Documentation}
\label{sbthread_8c_abba6d18d863334a98eecbaf5ffe81ab1} 
\index{sbthread.c@{sbthread.c}!sbevent\_free@{sbevent\_free}}
\index{sbevent\_free@{sbevent\_free}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbevent\_free()}
{\footnotesize\ttfamily void sbevent\+\_\+free (\begin{DoxyParamCaption}\item[{\textbf{ sbevent\+\_\+t} $\ast$}]{evt }\end{DoxyParamCaption})}



Free an event. 



Definition at line \textbf{ 632} of file \textbf{ sbthread.\+c}.



References \textbf{ ckd\+\_\+free()}.

\label{sbthread_8c_a3d4808917ac42153d456ceb7d1467823} 
\index{sbthread.c@{sbthread.c}!sbevent\_init@{sbevent\_init}}
\index{sbevent\_init@{sbevent\_init}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbevent\_init()}
{\footnotesize\ttfamily \textbf{ sbevent\+\_\+t} $\ast$ sbevent\+\_\+init (\begin{DoxyParamCaption}\item[{void}]{ }\end{DoxyParamCaption})}



Initialize an event. 



Definition at line \textbf{ 611} of file \textbf{ sbthread.\+c}.



References \textbf{ ckd\+\_\+calloc}, \textbf{ ckd\+\_\+free()}, \textbf{ E\+\_\+\+ERROR}, and \textbf{ E\+\_\+\+ERROR\+\_\+\+SYSTEM}.

\label{sbthread_8c_a89f88d25e27146dc7e4956211e69a6d5} 
\index{sbthread.c@{sbthread.c}!sbevent\_signal@{sbevent\_signal}}
\index{sbevent\_signal@{sbevent\_signal}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbevent\_signal()}
{\footnotesize\ttfamily int sbevent\+\_\+signal (\begin{DoxyParamCaption}\item[{\textbf{ sbevent\+\_\+t} $\ast$}]{evt }\end{DoxyParamCaption})}



Signal an event. 



Definition at line \textbf{ 640} of file \textbf{ sbthread.\+c}.

\label{sbthread_8c_a9f48001ca75395a3b192902b37edd5e0} 
\index{sbthread.c@{sbthread.c}!sbevent\_wait@{sbevent\_wait}}
\index{sbevent\_wait@{sbevent\_wait}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbevent\_wait()}
{\footnotesize\ttfamily int sbevent\+\_\+wait (\begin{DoxyParamCaption}\item[{\textbf{ sbevent\+\_\+t} $\ast$}]{evt,  }\item[{int}]{sec,  }\item[{int}]{nsec }\end{DoxyParamCaption})}



Wait for an event to be signalled. 



Definition at line \textbf{ 652} of file \textbf{ sbthread.\+c}.

\label{sbthread_8c_aa8483d06a1456207643abcfebc3b2e0a} 
\index{sbthread.c@{sbthread.c}!sbmsgq\_free@{sbmsgq\_free}}
\index{sbmsgq\_free@{sbmsgq\_free}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbmsgq\_free()}
{\footnotesize\ttfamily void sbmsgq\+\_\+free (\begin{DoxyParamCaption}\item[{\textbf{ sbmsgq\+\_\+t} $\ast$}]{msgq }\end{DoxyParamCaption})}



Free a message queue. 



Definition at line \textbf{ 465} of file \textbf{ sbthread.\+c}.



References \textbf{ ckd\+\_\+free()}.



Referenced by \textbf{ sbthread\+\_\+free()}.

\label{sbthread_8c_a981bc6b9c25f899bf80ce2485addd4a1} 
\index{sbthread.c@{sbthread.c}!sbmsgq\_init@{sbmsgq\_init}}
\index{sbmsgq\_init@{sbmsgq\_init}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbmsgq\_init()}
{\footnotesize\ttfamily \textbf{ sbmsgq\+\_\+t} $\ast$ sbmsgq\+\_\+init (\begin{DoxyParamCaption}\item[{size\+\_\+t}]{depth }\end{DoxyParamCaption})}



Create a message queue. 


\begin{DoxyParams}{Parameters}
{\em depth} & Depth of the queue. \\
\hline
\end{DoxyParams}


Definition at line \textbf{ 444} of file \textbf{ sbthread.\+c}.



References \textbf{ ckd\+\_\+calloc}, and \textbf{ ckd\+\_\+free()}.



Referenced by \textbf{ sbthread\+\_\+start()}.

\label{sbthread_8c_ae6c214bf1fab2f10cb51648b7e736698} 
\index{sbthread.c@{sbthread.c}!sbmsgq\_send@{sbmsgq\_send}}
\index{sbmsgq\_send@{sbmsgq\_send}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbmsgq\_send()}
{\footnotesize\ttfamily int sbmsgq\+\_\+send (\begin{DoxyParamCaption}\item[{\textbf{ sbmsgq\+\_\+t} $\ast$}]{q,  }\item[{size\+\_\+t}]{len,  }\item[{void const $\ast$}]{data }\end{DoxyParamCaption})}



Post a message to a queue. 



Definition at line \textbf{ 475} of file \textbf{ sbthread.\+c}.



Referenced by \textbf{ sbthread\+\_\+send()}.

\label{sbthread_8c_a1ad854b97692956c6a2bb7ef8e9de284} 
\index{sbthread.c@{sbthread.c}!sbmsgq\_wait@{sbmsgq\_wait}}
\index{sbmsgq\_wait@{sbmsgq\_wait}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbmsgq\_wait()}
{\footnotesize\ttfamily void $\ast$ sbmsgq\+\_\+wait (\begin{DoxyParamCaption}\item[{\textbf{ sbmsgq\+\_\+t} $\ast$}]{q,  }\item[{size\+\_\+t $\ast$}]{out\+\_\+len,  }\item[{int}]{sec,  }\item[{int}]{nsec }\end{DoxyParamCaption})}



Wait for a message from a queue. 



Definition at line \textbf{ 555} of file \textbf{ sbthread.\+c}.

\label{sbthread_8c_af1f9eb876ca9fc93d6c548120af14197} 
\index{sbthread.c@{sbthread.c}!sbmtx\_free@{sbmtx\_free}}
\index{sbmtx\_free@{sbmtx\_free}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbmtx\_free()}
{\footnotesize\ttfamily void sbmtx\+\_\+free (\begin{DoxyParamCaption}\item[{\textbf{ sbmtx\+\_\+t} $\ast$}]{mtx }\end{DoxyParamCaption})}



Dispose of a mutex. 



Definition at line \textbf{ 702} of file \textbf{ sbthread.\+c}.



References \textbf{ ckd\+\_\+free()}.

\label{sbthread_8c_a0d5ebafbb506be851f5a2c4eceab22eb} 
\index{sbthread.c@{sbthread.c}!sbmtx\_init@{sbmtx\_init}}
\index{sbmtx\_init@{sbmtx\_init}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbmtx\_init()}
{\footnotesize\ttfamily \textbf{ sbmtx\+\_\+t} $\ast$ sbmtx\+\_\+init (\begin{DoxyParamCaption}\item[{void}]{ }\end{DoxyParamCaption})}



Create a mutex. 



Definition at line \textbf{ 671} of file \textbf{ sbthread.\+c}.



References \textbf{ ckd\+\_\+calloc}, and \textbf{ ckd\+\_\+free()}.

\label{sbthread_8c_a000d9d0610c6b91649edde95e3bafa6c} 
\index{sbthread.c@{sbthread.c}!sbmtx\_lock@{sbmtx\_lock}}
\index{sbmtx\_lock@{sbmtx\_lock}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbmtx\_lock()}
{\footnotesize\ttfamily int sbmtx\+\_\+lock (\begin{DoxyParamCaption}\item[{\textbf{ sbmtx\+\_\+t} $\ast$}]{mtx }\end{DoxyParamCaption})}



Acquire a mutex. 



Definition at line \textbf{ 690} of file \textbf{ sbthread.\+c}.

\label{sbthread_8c_aaca10c92363d8979100c8fddeb783f9a} 
\index{sbthread.c@{sbthread.c}!sbmtx\_trylock@{sbmtx\_trylock}}
\index{sbmtx\_trylock@{sbmtx\_trylock}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbmtx\_trylock()}
{\footnotesize\ttfamily int sbmtx\+\_\+trylock (\begin{DoxyParamCaption}\item[{\textbf{ sbmtx\+\_\+t} $\ast$}]{mtx }\end{DoxyParamCaption})}



Try to acquire a mutex. 



Definition at line \textbf{ 684} of file \textbf{ sbthread.\+c}.

\label{sbthread_8c_ab3fdc55976e2b0581e598bd20187ce3a} 
\index{sbthread.c@{sbthread.c}!sbmtx\_unlock@{sbmtx\_unlock}}
\index{sbmtx\_unlock@{sbmtx\_unlock}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbmtx\_unlock()}
{\footnotesize\ttfamily int sbmtx\+\_\+unlock (\begin{DoxyParamCaption}\item[{\textbf{ sbmtx\+\_\+t} $\ast$}]{mtx }\end{DoxyParamCaption})}



Release a mutex. 



Definition at line \textbf{ 696} of file \textbf{ sbthread.\+c}.

\label{sbthread_8c_a81bba064345852e81945ceb93c7c9b5f} 
\index{sbthread.c@{sbthread.c}!sbthread\_arg@{sbthread\_arg}}
\index{sbthread\_arg@{sbthread\_arg}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbthread\_arg()}
{\footnotesize\ttfamily void $\ast$ sbthread\+\_\+arg (\begin{DoxyParamCaption}\item[{\textbf{ sbthread\+\_\+t} $\ast$}]{th }\end{DoxyParamCaption})}



Get argument pointer from a thread. 



Definition at line \textbf{ 716} of file \textbf{ sbthread.\+c}.

\label{sbthread_8c_acf71d443cbe011a2a308cc854db03d2e} 
\index{sbthread.c@{sbthread.c}!sbthread\_config@{sbthread\_config}}
\index{sbthread\_config@{sbthread\_config}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbthread\_config()}
{\footnotesize\ttfamily \textbf{ cmd\+\_\+ln\+\_\+t} $\ast$ sbthread\+\_\+config (\begin{DoxyParamCaption}\item[{\textbf{ sbthread\+\_\+t} $\ast$}]{th }\end{DoxyParamCaption})}



Get configuration object from a thread. 



Definition at line \textbf{ 710} of file \textbf{ sbthread.\+c}.

\label{sbthread_8c_a2b80d955dab72ff602d9048551dc7489} 
\index{sbthread.c@{sbthread.c}!sbthread\_free@{sbthread\_free}}
\index{sbthread\_free@{sbthread\_free}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbthread\_free()}
{\footnotesize\ttfamily void sbthread\+\_\+free (\begin{DoxyParamCaption}\item[{\textbf{ sbthread\+\_\+t} $\ast$}]{th }\end{DoxyParamCaption})}



Free a thread object. 



Definition at line \textbf{ 734} of file \textbf{ sbthread.\+c}.



References \textbf{ ckd\+\_\+free()}, \textbf{ sbmsgq\+\_\+free()}, and \textbf{ sbthread\+\_\+wait()}.



Referenced by \textbf{ sbthread\+\_\+start()}.

\label{sbthread_8c_a2a09616d8a701c133a45652b35939241} 
\index{sbthread.c@{sbthread.c}!sbthread\_msgq@{sbthread\_msgq}}
\index{sbthread\_msgq@{sbthread\_msgq}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbthread\_msgq()}
{\footnotesize\ttfamily \textbf{ sbmsgq\+\_\+t} $\ast$ sbthread\+\_\+msgq (\begin{DoxyParamCaption}\item[{\textbf{ sbthread\+\_\+t} $\ast$}]{th }\end{DoxyParamCaption})}



Get message queue from a thread. 



Definition at line \textbf{ 722} of file \textbf{ sbthread.\+c}.

\label{sbthread_8c_afae533b302b310a828cf2eed1f2f5995} 
\index{sbthread.c@{sbthread.c}!sbthread\_send@{sbthread\_send}}
\index{sbthread\_send@{sbthread\_send}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbthread\_send()}
{\footnotesize\ttfamily int sbthread\+\_\+send (\begin{DoxyParamCaption}\item[{\textbf{ sbthread\+\_\+t} $\ast$}]{th,  }\item[{size\+\_\+t}]{len,  }\item[{void const $\ast$}]{data }\end{DoxyParamCaption})}



Send an asynchronous message to a thread. 

Each thread gets a message queue by default, so this is just a wrapper around \doxyref{sbmsgq\+\_\+send()}{p.}{sbthread_8h_ab0591b5cb626be52dd22edf8fa13a3e3}. 

Definition at line \textbf{ 728} of file \textbf{ sbthread.\+c}.



References \textbf{ sbmsgq\+\_\+send()}.

\label{sbthread_8c_a0b5f86a42f5b57ab725814b57d1d9eff} 
\index{sbthread.c@{sbthread.c}!sbthread\_start@{sbthread\_start}}
\index{sbthread\_start@{sbthread\_start}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbthread\_start()}
{\footnotesize\ttfamily \textbf{ sbthread\+\_\+t} $\ast$ sbthread\+\_\+start (\begin{DoxyParamCaption}\item[{\textbf{ cmd\+\_\+ln\+\_\+t} $\ast$}]{config,  }\item[{\textbf{ sbthread\+\_\+main}}]{func,  }\item[{void $\ast$}]{arg }\end{DoxyParamCaption})}



Start a new thread. 



Definition at line \textbf{ 406} of file \textbf{ sbthread.\+c}.



References \textbf{ ckd\+\_\+calloc}, \textbf{ E\+\_\+\+ERROR}, \textbf{ sbmsgq\+\_\+init()}, and \textbf{ sbthread\+\_\+free()}.

\label{sbthread_8c_ad4cc725834be72ea986f80c4f4ecb24d} 
\index{sbthread.c@{sbthread.c}!sbthread\_wait@{sbthread\_wait}}
\index{sbthread\_wait@{sbthread\_wait}!sbthread.c@{sbthread.c}}
\doxysubsubsection{sbthread\_wait()}
{\footnotesize\ttfamily int sbthread\+\_\+wait (\begin{DoxyParamCaption}\item[{\textbf{ sbthread\+\_\+t} $\ast$}]{th }\end{DoxyParamCaption})}



Wait for a thread to complete. 



Definition at line \textbf{ 425} of file \textbf{ sbthread.\+c}.



References \textbf{ E\+\_\+\+ERROR}.



Referenced by \textbf{ sbthread\+\_\+free()}.

