\doxysection{sbthread.\+c}
\label{sbthread_8c_source}\index{src/libsphinxbase/util/sbthread.c@{src/libsphinxbase/util/sbthread.c}}
\textbf{ Go to the documentation of this file.}
\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*\ ====================================================================}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 2008\ Carnegie\ Mellon\ University.\ \ All\ rights\ }}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ reserved.}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ This\ work\ was\ supported\ in\ part\ by\ funding\ from\ the\ Defense\ Advanced\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ Research\ Projects\ Agency\ and\ the\ National\ Science\ Foundation\ of\ the\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ United\ States\ of\ America,\ and\ the\ CMU\ Sphinx\ Speech\ Consortium.}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00037\ }
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00045\ }
\DoxyCodeLine{00046\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/sbthread.h"{}}}
\DoxyCodeLine{00047\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/ckd\_alloc.h"{}}}
\DoxyCodeLine{00048\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/err.h"{}}}
\DoxyCodeLine{00049\ }
\DoxyCodeLine{00050\ \textcolor{comment}{/*}}
\DoxyCodeLine{00051\ \textcolor{comment}{\ *\ Platform-\/specific\ parts:\ threads,\ mutexes,\ and\ signals.}}
\DoxyCodeLine{00052\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00053\ \textcolor{preprocessor}{\#if\ (defined(\_WIN32)\ ||\ defined(\_\_CYGWIN\_\_))\ \&\&\ !defined(\_\_SYMBIAN32\_\_)}}
\DoxyCodeLine{00054\ \textcolor{preprocessor}{\#define\ \_WIN32\_WINNT\ 0x0400}}
\DoxyCodeLine{00055\ \textcolor{preprocessor}{\#include\ <windows.h>}}
\DoxyCodeLine{00056\ }
\DoxyCodeLine{00057\ \textcolor{keyword}{struct\ }sbthread\_s\ \{}
\DoxyCodeLine{00058\ \ \ \ \ cmd\_ln\_t\ *config;}
\DoxyCodeLine{00059\ \ \ \ \ sbmsgq\_t\ *msgq;}
\DoxyCodeLine{00060\ \ \ \ \ sbthread\_main\ func;}
\DoxyCodeLine{00061\ \ \ \ \ \textcolor{keywordtype}{void}\ *arg;}
\DoxyCodeLine{00062\ \ \ \ \ HANDLE\ th;}
\DoxyCodeLine{00063\ \ \ \ \ DWORD\ tid;}
\DoxyCodeLine{00064\ \};}
\DoxyCodeLine{00065\ }
\DoxyCodeLine{00066\ \textcolor{keyword}{struct\ }sbmsgq\_s\ \{}
\DoxyCodeLine{00067\ \ \ \ \ \textcolor{comment}{/*\ Ringbuffer\ for\ passing\ messages.\ */}}
\DoxyCodeLine{00068\ \ \ \ \ \textcolor{keywordtype}{char}\ *data;}
\DoxyCodeLine{00069\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ depth;}
\DoxyCodeLine{00070\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ out;}
\DoxyCodeLine{00071\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ nbytes;}
\DoxyCodeLine{00072\ }
\DoxyCodeLine{00073\ \ \ \ \ \textcolor{comment}{/*\ Current\ message\ is\ stored\ here.\ */}}
\DoxyCodeLine{00074\ \ \ \ \ \textcolor{keywordtype}{char}\ *msg;}
\DoxyCodeLine{00075\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ msglen;}
\DoxyCodeLine{00076\ \ \ \ \ CRITICAL\_SECTION\ mtx;}
\DoxyCodeLine{00077\ \ \ \ \ HANDLE\ evt;}
\DoxyCodeLine{00078\ \};}
\DoxyCodeLine{00079\ }
\DoxyCodeLine{00080\ \textcolor{keyword}{struct\ }sbevent\_s\ \{}
\DoxyCodeLine{00081\ \ \ \ \ HANDLE\ evt;}
\DoxyCodeLine{00082\ \};}
\DoxyCodeLine{00083\ }
\DoxyCodeLine{00084\ \textcolor{keyword}{struct\ }sbmtx\_s\ \{}
\DoxyCodeLine{00085\ \ \ \ \ CRITICAL\_SECTION\ mtx;}
\DoxyCodeLine{00086\ \};}
\DoxyCodeLine{00087\ }
\DoxyCodeLine{00088\ DWORD\ WINAPI}
\DoxyCodeLine{00089\ sbthread\_internal\_main(LPVOID\ arg)}
\DoxyCodeLine{00090\ \{}
\DoxyCodeLine{00091\ \ \ \ \ sbthread\_t\ *th\ =\ (sbthread\_t\ *)arg;}
\DoxyCodeLine{00092\ \ \ \ \ \textcolor{keywordtype}{int}\ rv;}
\DoxyCodeLine{00093\ }
\DoxyCodeLine{00094\ \ \ \ \ rv\ =\ (*th-\/>func)(th);}
\DoxyCodeLine{00095\ \ \ \ \ \textcolor{keywordflow}{return}\ (DWORD)rv;}
\DoxyCodeLine{00096\ \}}
\DoxyCodeLine{00097\ }
\DoxyCodeLine{00098\ sbthread\_t\ *}
\DoxyCodeLine{00099\ sbthread\_start(cmd\_ln\_t\ *config,\ sbthread\_main\ func,\ \textcolor{keywordtype}{void}\ *arg)}
\DoxyCodeLine{00100\ \{}
\DoxyCodeLine{00101\ \ \ \ \ sbthread\_t\ *th;}
\DoxyCodeLine{00102\ }
\DoxyCodeLine{00103\ \ \ \ \ th\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*th));}
\DoxyCodeLine{00104\ \ \ \ \ th-\/>config\ =\ config;}
\DoxyCodeLine{00105\ \ \ \ \ th-\/>func\ =\ func;}
\DoxyCodeLine{00106\ \ \ \ \ th-\/>arg\ =\ arg;}
\DoxyCodeLine{00107\ \ \ \ \ th-\/>msgq\ =\ sbmsgq\_init(256);}
\DoxyCodeLine{00108\ \ \ \ \ th-\/>th\ =\ CreateThread(NULL,\ 0,\ sbthread\_internal\_main,\ th,\ 0,\ \&th-\/>tid);}
\DoxyCodeLine{00109\ \ \ \ \ \textcolor{keywordflow}{if}\ (th-\/>th\ ==\ NULL)\ \{}
\DoxyCodeLine{00110\ \ \ \ \ \ \ \ \ sbthread\_free(th);}
\DoxyCodeLine{00111\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00112\ \ \ \ \ \}}
\DoxyCodeLine{00113\ \ \ \ \ \textcolor{keywordflow}{return}\ th;}
\DoxyCodeLine{00114\ \}}
\DoxyCodeLine{00115\ }
\DoxyCodeLine{00116\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00117\ sbthread\_wait(sbthread\_t\ *th)}
\DoxyCodeLine{00118\ \{}
\DoxyCodeLine{00119\ \ \ \ \ DWORD\ rv,\ exit;}
\DoxyCodeLine{00120\ }
\DoxyCodeLine{00121\ \ \ \ \ \textcolor{comment}{/*\ It\ has\ already\ been\ joined.\ */}}
\DoxyCodeLine{00122\ \ \ \ \ \textcolor{keywordflow}{if}\ (th-\/>th\ ==\ NULL)}
\DoxyCodeLine{00123\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00124\ }
\DoxyCodeLine{00125\ \ \ \ \ rv\ =\ WaitForSingleObject(th-\/>th,\ INFINITE);}
\DoxyCodeLine{00126\ \ \ \ \ \textcolor{keywordflow}{if}\ (rv\ ==\ WAIT\_FAILED)\ \{}
\DoxyCodeLine{00127\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ join\ thread:\ WAIT\_FAILED\(\backslash\)n"{}});}
\DoxyCodeLine{00128\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00129\ \ \ \ \ \}}
\DoxyCodeLine{00130\ \ \ \ \ GetExitCodeThread(th-\/>th,\ \&exit);}
\DoxyCodeLine{00131\ \ \ \ \ CloseHandle(th-\/>th);}
\DoxyCodeLine{00132\ \ \ \ \ th-\/>th\ =\ NULL;}
\DoxyCodeLine{00133\ \ \ \ \ \textcolor{keywordflow}{return}\ (\textcolor{keywordtype}{int})exit;}
\DoxyCodeLine{00134\ \}}
\DoxyCodeLine{00135\ }
\DoxyCodeLine{00136\ \textcolor{keyword}{static}\ DWORD}
\DoxyCodeLine{00137\ cond\_timed\_wait(HANDLE\ cond,\ \textcolor{keywordtype}{int}\ sec,\ \textcolor{keywordtype}{int}\ nsec)}
\DoxyCodeLine{00138\ \{}
\DoxyCodeLine{00139\ \ \ \ \ DWORD\ rv;}
\DoxyCodeLine{00140\ \ \ \ \ \textcolor{keywordflow}{if}\ (sec\ ==\ -\/1)\ \{}
\DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ rv\ =\ WaitForSingleObject(cond,\ INFINITE);}
\DoxyCodeLine{00142\ \ \ \ \ \}}
\DoxyCodeLine{00143\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00144\ \ \ \ \ \ \ \ \ DWORD\ ms;}
\DoxyCodeLine{00145\ }
\DoxyCodeLine{00146\ \ \ \ \ \ \ \ \ ms\ =\ sec\ *\ 1000\ +\ nsec\ /\ (1000*1000);}
\DoxyCodeLine{00147\ \ \ \ \ \ \ \ \ rv\ =\ WaitForSingleObject(cond,\ ms);}
\DoxyCodeLine{00148\ \ \ \ \ \}}
\DoxyCodeLine{00149\ \ \ \ \ \textcolor{keywordflow}{return}\ rv;}
\DoxyCodeLine{00150\ \}}
\DoxyCodeLine{00151\ }
\DoxyCodeLine{00152\ \textcolor{comment}{/*\ Silvio\ Moioli:\ updated\ to\ use\ Unicode\ */}}
\DoxyCodeLine{00153\ sbevent\_t\ *}
\DoxyCodeLine{00154\ sbevent\_init(\textcolor{keywordtype}{void})}
\DoxyCodeLine{00155\ \{}
\DoxyCodeLine{00156\ \ \ \ \ sbevent\_t\ *evt;}
\DoxyCodeLine{00157\ }
\DoxyCodeLine{00158\ \ \ \ \ evt\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*evt));}
\DoxyCodeLine{00159\ \ \ \ \ evt-\/>evt\ =\ CreateEventW(NULL,\ FALSE,\ FALSE,\ NULL);}
\DoxyCodeLine{00160\ \ \ \ \ \textcolor{keywordflow}{if}\ (evt-\/>evt\ ==\ NULL)\ \{}
\DoxyCodeLine{00161\ \ \ \ \ \ \ \ \ ckd\_free(evt);}
\DoxyCodeLine{00162\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00163\ \ \ \ \ \}}
\DoxyCodeLine{00164\ \ \ \ \ \textcolor{keywordflow}{return}\ evt;}
\DoxyCodeLine{00165\ \}}
\DoxyCodeLine{00166\ }
\DoxyCodeLine{00167\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00168\ sbevent\_free(sbevent\_t\ *evt)}
\DoxyCodeLine{00169\ \{}
\DoxyCodeLine{00170\ \ \ \ \ CloseHandle(evt-\/>evt);}
\DoxyCodeLine{00171\ \ \ \ \ ckd\_free(evt);}
\DoxyCodeLine{00172\ \}}
\DoxyCodeLine{00173\ }
\DoxyCodeLine{00174\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00175\ sbevent\_signal(sbevent\_t\ *evt)}
\DoxyCodeLine{00176\ \{}
\DoxyCodeLine{00177\ \ \ \ \ \textcolor{keywordflow}{return}\ SetEvent(evt-\/>evt)\ ?\ 0\ :\ -\/1;}
\DoxyCodeLine{00178\ \}}
\DoxyCodeLine{00179\ }
\DoxyCodeLine{00180\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00181\ sbevent\_wait(sbevent\_t\ *evt,\ \textcolor{keywordtype}{int}\ sec,\ \textcolor{keywordtype}{int}\ nsec)}
\DoxyCodeLine{00182\ \{}
\DoxyCodeLine{00183\ \ \ \ \ DWORD\ rv;}
\DoxyCodeLine{00184\ }
\DoxyCodeLine{00185\ \ \ \ \ rv\ =\ cond\_timed\_wait(evt-\/>evt,\ sec,\ nsec);}
\DoxyCodeLine{00186\ \ \ \ \ \textcolor{keywordflow}{return}\ rv;}
\DoxyCodeLine{00187\ \}}
\DoxyCodeLine{00188\ }
\DoxyCodeLine{00189\ sbmtx\_t\ *}
\DoxyCodeLine{00190\ sbmtx\_init(\textcolor{keywordtype}{void})}
\DoxyCodeLine{00191\ \{}
\DoxyCodeLine{00192\ \ \ \ \ sbmtx\_t\ *mtx;}
\DoxyCodeLine{00193\ }
\DoxyCodeLine{00194\ \ \ \ \ mtx\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*mtx));}
\DoxyCodeLine{00195\ \ \ \ \ InitializeCriticalSection(\&mtx-\/>mtx);}
\DoxyCodeLine{00196\ \ \ \ \ \textcolor{keywordflow}{return}\ mtx;}
\DoxyCodeLine{00197\ \}}
\DoxyCodeLine{00198\ }
\DoxyCodeLine{00199\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00200\ sbmtx\_trylock(sbmtx\_t\ *mtx)}
\DoxyCodeLine{00201\ \{}
\DoxyCodeLine{00202\ \ \ \ \ \textcolor{keywordflow}{return}\ TryEnterCriticalSection(\&mtx-\/>mtx)\ ?\ 0\ :\ -\/1;}
\DoxyCodeLine{00203\ \}}
\DoxyCodeLine{00204\ }
\DoxyCodeLine{00205\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00206\ sbmtx\_lock(sbmtx\_t\ *mtx)}
\DoxyCodeLine{00207\ \{}
\DoxyCodeLine{00208\ \ \ \ \ EnterCriticalSection(\&mtx-\/>mtx);}
\DoxyCodeLine{00209\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00210\ \}}
\DoxyCodeLine{00211\ }
\DoxyCodeLine{00212\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00213\ sbmtx\_unlock(sbmtx\_t\ *mtx)}
\DoxyCodeLine{00214\ \{}
\DoxyCodeLine{00215\ \ \ \ \ LeaveCriticalSection(\&mtx-\/>mtx);}
\DoxyCodeLine{00216\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00217\ \}}
\DoxyCodeLine{00218\ }
\DoxyCodeLine{00219\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00220\ sbmtx\_free(sbmtx\_t\ *mtx)}
\DoxyCodeLine{00221\ \{}
\DoxyCodeLine{00222\ \ \ \ \ DeleteCriticalSection(\&mtx-\/>mtx);}
\DoxyCodeLine{00223\ \ \ \ \ ckd\_free(mtx);}
\DoxyCodeLine{00224\ \}}
\DoxyCodeLine{00225\ }
\DoxyCodeLine{00226\ sbmsgq\_t\ *}
\DoxyCodeLine{00227\ sbmsgq\_init(\textcolor{keywordtype}{size\_t}\ depth)}
\DoxyCodeLine{00228\ \{}
\DoxyCodeLine{00229\ \ \ \ \ sbmsgq\_t\ *msgq;}
\DoxyCodeLine{00230\ }
\DoxyCodeLine{00231\ \ \ \ \ msgq\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*msgq));}
\DoxyCodeLine{00232\ \ \ \ \ msgq-\/>depth\ =\ depth;}
\DoxyCodeLine{00233\ \ \ \ \ msgq-\/>evt\ =\ CreateEventW(NULL,\ FALSE,\ FALSE,\ NULL);}
\DoxyCodeLine{00234\ \ \ \ \ \textcolor{keywordflow}{if}\ (msgq-\/>evt\ ==\ NULL)\ \{}
\DoxyCodeLine{00235\ \ \ \ \ \ \ \ \ ckd\_free(msgq);}
\DoxyCodeLine{00236\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00237\ \ \ \ \ \}}
\DoxyCodeLine{00238\ \ \ \ \ InitializeCriticalSection(\&msgq-\/>mtx);}
\DoxyCodeLine{00239\ \ \ \ \ msgq-\/>data\ =\ ckd\_calloc(depth,\ 1);}
\DoxyCodeLine{00240\ \ \ \ \ msgq-\/>msg\ =\ ckd\_calloc(depth,\ 1);}
\DoxyCodeLine{00241\ \ \ \ \ \textcolor{keywordflow}{return}\ msgq;}
\DoxyCodeLine{00242\ \}}
\DoxyCodeLine{00243\ }
\DoxyCodeLine{00244\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00245\ sbmsgq\_free(sbmsgq\_t\ *msgq)}
\DoxyCodeLine{00246\ \{}
\DoxyCodeLine{00247\ \ \ \ \ CloseHandle(msgq-\/>evt);}
\DoxyCodeLine{00248\ \ \ \ \ ckd\_free(msgq-\/>data);}
\DoxyCodeLine{00249\ \ \ \ \ ckd\_free(msgq-\/>msg);}
\DoxyCodeLine{00250\ \ \ \ \ ckd\_free(msgq);}
\DoxyCodeLine{00251\ \}}
\DoxyCodeLine{00252\ }
\DoxyCodeLine{00253\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00254\ sbmsgq\_send(sbmsgq\_t\ *q,\ \textcolor{keywordtype}{size\_t}\ len,\ \textcolor{keywordtype}{void}\ \textcolor{keyword}{const}\ *data)}
\DoxyCodeLine{00255\ \{}
\DoxyCodeLine{00256\ \ \ \ \ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *cdata\ =\ (\textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *)data;}
\DoxyCodeLine{00257\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ in;}
\DoxyCodeLine{00258\ }
\DoxyCodeLine{00259\ \ \ \ \ \textcolor{comment}{/*\ Don't\ allow\ things\ bigger\ than\ depth\ to\ be\ sent!\ */}}
\DoxyCodeLine{00260\ \ \ \ \ \textcolor{keywordflow}{if}\ (len\ +\ \textcolor{keyword}{sizeof}(len)\ >\ q-\/>depth)}
\DoxyCodeLine{00261\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00262\ }
\DoxyCodeLine{00263\ \ \ \ \ \textcolor{keywordflow}{if}\ (q-\/>nbytes\ +\ len\ +\ \textcolor{keyword}{sizeof}(len)\ >\ q-\/>depth)}
\DoxyCodeLine{00264\ \ \ \ \ \ \ \ \ WaitForSingleObject(q-\/>evt,\ INFINITE);}
\DoxyCodeLine{00265\ }
\DoxyCodeLine{00266\ \ \ \ \ \textcolor{comment}{/*\ Lock\ things\ while\ we\ manipulate\ the\ buffer\ (FIXME:\ this}}
\DoxyCodeLine{00267\ \textcolor{comment}{\ \ \ \ \ \ \ actually\ should\ have\ been\ atomic\ with\ the\ wait\ above\ ...)\ */}}
\DoxyCodeLine{00268\ \ \ \ \ EnterCriticalSection(\&q-\/>mtx);}
\DoxyCodeLine{00269\ \ \ \ \ in\ =\ (q-\/>out\ +\ q-\/>nbytes)\ \%\ q-\/>depth;}
\DoxyCodeLine{00270\ \ \ \ \ \textcolor{comment}{/*\ First\ write\ the\ size\ of\ the\ message.\ */}}
\DoxyCodeLine{00271\ \ \ \ \ \textcolor{keywordflow}{if}\ (in\ +\ \textcolor{keyword}{sizeof}(len)\ >\ q-\/>depth)\ \{}
\DoxyCodeLine{00272\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Handle\ the\ annoying\ case\ where\ the\ size\ field\ gets\ wrapped\ around.\ */}}
\DoxyCodeLine{00273\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len1\ =\ q-\/>depth\ -\/\ in;}
\DoxyCodeLine{00274\ \ \ \ \ \ \ \ \ memcpy(q-\/>data\ +\ in,\ \&len,\ len1);}
\DoxyCodeLine{00275\ \ \ \ \ \ \ \ \ memcpy(q-\/>data,\ ((\textcolor{keywordtype}{char}\ *)\&len)\ +\ len1,\ \textcolor{keyword}{sizeof}(len)\ -\/\ len1);}
\DoxyCodeLine{00276\ \ \ \ \ \ \ \ \ q-\/>nbytes\ +=\ \textcolor{keyword}{sizeof}(len);}
\DoxyCodeLine{00277\ \ \ \ \ \ \ \ \ in\ =\ \textcolor{keyword}{sizeof}(len)\ -\/\ len1;}
\DoxyCodeLine{00278\ \ \ \ \ \}}
\DoxyCodeLine{00279\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00280\ \ \ \ \ \ \ \ \ memcpy(q-\/>data\ +\ in,\ \&len,\ \textcolor{keyword}{sizeof}(len));}
\DoxyCodeLine{00281\ \ \ \ \ \ \ \ \ q-\/>nbytes\ +=\ \textcolor{keyword}{sizeof}(len);}
\DoxyCodeLine{00282\ \ \ \ \ \ \ \ \ in\ +=\ \textcolor{keyword}{sizeof}(len);}
\DoxyCodeLine{00283\ \ \ \ \ \}}
\DoxyCodeLine{00284\ }
\DoxyCodeLine{00285\ \ \ \ \ \textcolor{comment}{/*\ Now\ write\ the\ message\ body.\ */}}
\DoxyCodeLine{00286\ \ \ \ \ \textcolor{keywordflow}{if}\ (in\ +\ len\ >\ q-\/>depth)\ \{}
\DoxyCodeLine{00287\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Handle\ wraparound.\ */}}
\DoxyCodeLine{00288\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len1\ =\ q-\/>depth\ -\/\ in;}
\DoxyCodeLine{00289\ \ \ \ \ \ \ \ \ memcpy(q-\/>data\ +\ in,\ cdata,\ len1);}
\DoxyCodeLine{00290\ \ \ \ \ \ \ \ \ q-\/>nbytes\ +=\ len1;}
\DoxyCodeLine{00291\ \ \ \ \ \ \ \ \ cdata\ +=\ len1;}
\DoxyCodeLine{00292\ \ \ \ \ \ \ \ \ len\ -\/=\ len1;}
\DoxyCodeLine{00293\ \ \ \ \ \ \ \ \ in\ =\ 0;}
\DoxyCodeLine{00294\ \ \ \ \ \}}
\DoxyCodeLine{00295\ \ \ \ \ memcpy(q-\/>data\ +\ in,\ cdata,\ len);}
\DoxyCodeLine{00296\ \ \ \ \ q-\/>nbytes\ +=\ len;}
\DoxyCodeLine{00297\ }
\DoxyCodeLine{00298\ \ \ \ \ \textcolor{comment}{/*\ Signal\ the\ condition\ variable.\ */}}
\DoxyCodeLine{00299\ \ \ \ \ SetEvent(q-\/>evt);}
\DoxyCodeLine{00300\ \ \ \ \ \textcolor{comment}{/*\ Unlock.\ */}}
\DoxyCodeLine{00301\ \ \ \ \ LeaveCriticalSection(\&q-\/>mtx);}
\DoxyCodeLine{00302\ }
\DoxyCodeLine{00303\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00304\ \}}
\DoxyCodeLine{00305\ }
\DoxyCodeLine{00306\ \textcolor{keywordtype}{void}\ *}
\DoxyCodeLine{00307\ sbmsgq\_wait(sbmsgq\_t\ *q,\ \textcolor{keywordtype}{size\_t}\ *out\_len,\ \textcolor{keywordtype}{int}\ sec,\ \textcolor{keywordtype}{int}\ nsec)}
\DoxyCodeLine{00308\ \{}
\DoxyCodeLine{00309\ \ \ \ \ \textcolor{keywordtype}{char}\ *outptr;}
\DoxyCodeLine{00310\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len;}
\DoxyCodeLine{00311\ }
\DoxyCodeLine{00312\ \ \ \ \ \textcolor{comment}{/*\ Wait\ for\ data\ to\ be\ available.\ */}}
\DoxyCodeLine{00313\ \ \ \ \ \textcolor{keywordflow}{if}\ (q-\/>nbytes\ ==\ 0)\ \{}
\DoxyCodeLine{00314\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (cond\_timed\_wait(q-\/>evt,\ sec,\ nsec)\ ==\ WAIT\_FAILED)}
\DoxyCodeLine{00315\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Timed\ out\ or\ something...\ */}}
\DoxyCodeLine{00316\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00317\ \ \ \ \ \}}
\DoxyCodeLine{00318\ \ \ \ \ \textcolor{comment}{/*\ Lock\ to\ manipulate\ the\ queue\ (FIXME)\ */}}
\DoxyCodeLine{00319\ \ \ \ \ EnterCriticalSection(\&q-\/>mtx);}
\DoxyCodeLine{00320\ \ \ \ \ \textcolor{comment}{/*\ Get\ the\ message\ size.\ */}}
\DoxyCodeLine{00321\ \ \ \ \ \textcolor{keywordflow}{if}\ (q-\/>out\ +\ \textcolor{keyword}{sizeof}(q-\/>msglen)\ >\ q-\/>depth)\ \{}
\DoxyCodeLine{00322\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Handle\ annoying\ wraparound\ case.\ */}}
\DoxyCodeLine{00323\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len1\ =\ q-\/>depth\ -\/\ q-\/>out;}
\DoxyCodeLine{00324\ \ \ \ \ \ \ \ \ memcpy(\&q-\/>msglen,\ q-\/>data\ +\ q-\/>out,\ len1);}
\DoxyCodeLine{00325\ \ \ \ \ \ \ \ \ memcpy(((\textcolor{keywordtype}{char}\ *)\&q-\/>msglen)\ +\ len1,\ q-\/>data,}
\DoxyCodeLine{00326\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(q-\/>msglen)\ -\/\ len1);}
\DoxyCodeLine{00327\ \ \ \ \ \ \ \ \ q-\/>out\ =\ \textcolor{keyword}{sizeof}(q-\/>msglen)\ -\/\ len1;}
\DoxyCodeLine{00328\ \ \ \ \ \}}
\DoxyCodeLine{00329\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00330\ \ \ \ \ \ \ \ \ memcpy(\&q-\/>msglen,\ q-\/>data\ +\ q-\/>out,\ \textcolor{keyword}{sizeof}(q-\/>msglen));}
\DoxyCodeLine{00331\ \ \ \ \ \ \ \ \ q-\/>out\ +=\ \textcolor{keyword}{sizeof}(q-\/>msglen);}
\DoxyCodeLine{00332\ \ \ \ \ \}}
\DoxyCodeLine{00333\ \ \ \ \ q-\/>nbytes\ -\/=\ \textcolor{keyword}{sizeof}(q-\/>msglen);}
\DoxyCodeLine{00334\ \ \ \ \ \textcolor{comment}{/*\ Get\ the\ message\ body.\ */}}
\DoxyCodeLine{00335\ \ \ \ \ outptr\ =\ q-\/>msg;}
\DoxyCodeLine{00336\ \ \ \ \ len\ =\ q-\/>msglen;}
\DoxyCodeLine{00337\ \ \ \ \ \textcolor{keywordflow}{if}\ (q-\/>out\ +\ q-\/>msglen\ >\ q-\/>depth)\ \{}
\DoxyCodeLine{00338\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Handle\ wraparound.\ */}}
\DoxyCodeLine{00339\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len1\ =\ q-\/>depth\ -\/\ q-\/>out;}
\DoxyCodeLine{00340\ \ \ \ \ \ \ \ \ memcpy(outptr,\ q-\/>data\ +\ q-\/>out,\ len1);}
\DoxyCodeLine{00341\ \ \ \ \ \ \ \ \ outptr\ +=\ len1;}
\DoxyCodeLine{00342\ \ \ \ \ \ \ \ \ len\ -\/=\ len1;}
\DoxyCodeLine{00343\ \ \ \ \ \ \ \ \ q-\/>nbytes\ -\/=\ len1;}
\DoxyCodeLine{00344\ \ \ \ \ \ \ \ \ q-\/>out\ =\ 0;}
\DoxyCodeLine{00345\ \ \ \ \ \}}
\DoxyCodeLine{00346\ \ \ \ \ memcpy(outptr,\ q-\/>data\ +\ q-\/>out,\ len);}
\DoxyCodeLine{00347\ \ \ \ \ q-\/>nbytes\ -\/=\ len;}
\DoxyCodeLine{00348\ \ \ \ \ q-\/>out\ +=\ len;}
\DoxyCodeLine{00349\ }
\DoxyCodeLine{00350\ \ \ \ \ \textcolor{comment}{/*\ Signal\ the\ condition\ variable.\ */}}
\DoxyCodeLine{00351\ \ \ \ \ SetEvent(q-\/>evt);}
\DoxyCodeLine{00352\ \ \ \ \ \textcolor{comment}{/*\ Unlock.\ */}}
\DoxyCodeLine{00353\ \ \ \ \ LeaveCriticalSection(\&q-\/>mtx);}
\DoxyCodeLine{00354\ \ \ \ \ \textcolor{keywordflow}{if}\ (out\_len)}
\DoxyCodeLine{00355\ \ \ \ \ \ \ \ \ *out\_len\ =\ q-\/>msglen;}
\DoxyCodeLine{00356\ \ \ \ \ \textcolor{keywordflow}{return}\ q-\/>msg;}
\DoxyCodeLine{00357\ \}}
\DoxyCodeLine{00358\ }
\DoxyCodeLine{00359\ \textcolor{preprocessor}{\#else\ }\textcolor{comment}{/*\ POSIX\ */}\textcolor{preprocessor}{}}
\DoxyCodeLine{00360\ \textcolor{preprocessor}{\#include\ <pthread.h>}}
\DoxyCodeLine{00361\ \textcolor{preprocessor}{\#include\ <sys/time.h>}}
\DoxyCodeLine{00362\ }
\DoxyCodeLine{00363\ \textcolor{keyword}{struct\ }sbthread\_s\ \{}
\DoxyCodeLine{00364\ \ \ \ \ cmd\_ln\_t\ *config;}
\DoxyCodeLine{00365\ \ \ \ \ sbmsgq\_t\ *msgq;}
\DoxyCodeLine{00366\ \ \ \ \ sbthread\_main\ func;}
\DoxyCodeLine{00367\ \ \ \ \ \textcolor{keywordtype}{void}\ *arg;}
\DoxyCodeLine{00368\ \ \ \ \ pthread\_t\ th;}
\DoxyCodeLine{00369\ \};}
\DoxyCodeLine{00370\ }
\DoxyCodeLine{00371\ \textcolor{keyword}{struct\ }sbmsgq\_s\ \{}
\DoxyCodeLine{00372\ \ \ \ \ \textcolor{comment}{/*\ Ringbuffer\ for\ passing\ messages.\ */}}
\DoxyCodeLine{00373\ \ \ \ \ \textcolor{keywordtype}{char}\ *data;}
\DoxyCodeLine{00374\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ depth;}
\DoxyCodeLine{00375\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ out;}
\DoxyCodeLine{00376\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ nbytes;}
\DoxyCodeLine{00377\ }
\DoxyCodeLine{00378\ \ \ \ \ \textcolor{comment}{/*\ Current\ message\ is\ stored\ here.\ */}}
\DoxyCodeLine{00379\ \ \ \ \ \textcolor{keywordtype}{char}\ *msg;}
\DoxyCodeLine{00380\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ msglen;}
\DoxyCodeLine{00381\ \ \ \ \ pthread\_mutex\_t\ mtx;}
\DoxyCodeLine{00382\ \ \ \ \ pthread\_cond\_t\ cond;}
\DoxyCodeLine{00383\ \};}
\DoxyCodeLine{00384\ }
\DoxyCodeLine{00385\ \textcolor{keyword}{struct\ }sbevent\_s\ \{}
\DoxyCodeLine{00386\ \ \ \ \ pthread\_mutex\_t\ mtx;}
\DoxyCodeLine{00387\ \ \ \ \ pthread\_cond\_t\ cond;}
\DoxyCodeLine{00388\ \ \ \ \ \textcolor{keywordtype}{int}\ signalled;}
\DoxyCodeLine{00389\ \};}
\DoxyCodeLine{00390\ }
\DoxyCodeLine{00391\ \textcolor{keyword}{struct\ }sbmtx\_s\ \{}
\DoxyCodeLine{00392\ \ \ \ \ pthread\_mutex\_t\ mtx;}
\DoxyCodeLine{00393\ \};}
\DoxyCodeLine{00394\ }
\DoxyCodeLine{00395\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}\ *}
\DoxyCodeLine{00396\ sbthread\_internal\_main(\textcolor{keywordtype}{void}\ *arg)}
\DoxyCodeLine{00397\ \{}
\DoxyCodeLine{00398\ \ \ \ \ sbthread\_t\ *th\ =\ (sbthread\_t\ *)arg;}
\DoxyCodeLine{00399\ \ \ \ \ \textcolor{keywordtype}{int}\ rv;}
\DoxyCodeLine{00400\ }
\DoxyCodeLine{00401\ \ \ \ \ rv\ =\ (*th-\/>func)(th);}
\DoxyCodeLine{00402\ \ \ \ \ \textcolor{keywordflow}{return}\ (\textcolor{keywordtype}{void}\ *)(long)rv;}
\DoxyCodeLine{00403\ \}}
\DoxyCodeLine{00404\ }
\DoxyCodeLine{00405\ sbthread\_t\ *}
\DoxyCodeLine{00406\ sbthread\_start(cmd\_ln\_t\ *config,\ sbthread\_main\ func,\ \textcolor{keywordtype}{void}\ *arg)}
\DoxyCodeLine{00407\ \{}
\DoxyCodeLine{00408\ \ \ \ \ sbthread\_t\ *th;}
\DoxyCodeLine{00409\ \ \ \ \ \textcolor{keywordtype}{int}\ rv;}
\DoxyCodeLine{00410\ }
\DoxyCodeLine{00411\ \ \ \ \ th\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*th));}
\DoxyCodeLine{00412\ \ \ \ \ th-\/>config\ =\ config;}
\DoxyCodeLine{00413\ \ \ \ \ th-\/>func\ =\ func;}
\DoxyCodeLine{00414\ \ \ \ \ th-\/>arg\ =\ arg;}
\DoxyCodeLine{00415\ \ \ \ \ th-\/>msgq\ =\ sbmsgq\_init(1024);}
\DoxyCodeLine{00416\ \ \ \ \ \textcolor{keywordflow}{if}\ ((rv\ =\ pthread\_create(\&th-\/>th,\ NULL,\ \&sbthread\_internal\_main,\ th))\ !=\ 0)\ \{}
\DoxyCodeLine{00417\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ create\ thread:\ \%d\(\backslash\)n"{}},\ rv);}
\DoxyCodeLine{00418\ \ \ \ \ \ \ \ \ sbthread\_free(th);}
\DoxyCodeLine{00419\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00420\ \ \ \ \ \}}
\DoxyCodeLine{00421\ \ \ \ \ \textcolor{keywordflow}{return}\ th;}
\DoxyCodeLine{00422\ \}}
\DoxyCodeLine{00423\ }
\DoxyCodeLine{00424\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00425\ sbthread\_wait(sbthread\_t\ *th)}
\DoxyCodeLine{00426\ \{}
\DoxyCodeLine{00427\ \ \ \ \ \textcolor{keywordtype}{void}\ *exit;}
\DoxyCodeLine{00428\ \ \ \ \ \textcolor{keywordtype}{int}\ rv;}
\DoxyCodeLine{00429\ }
\DoxyCodeLine{00430\ \ \ \ \ \textcolor{comment}{/*\ It\ has\ already\ been\ joined.\ */}}
\DoxyCodeLine{00431\ \ \ \ \ \textcolor{keywordflow}{if}\ (th-\/>th\ ==\ (pthread\_t)-\/1)}
\DoxyCodeLine{00432\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00433\ }
\DoxyCodeLine{00434\ \ \ \ \ rv\ =\ pthread\_join(th-\/>th,\ \&exit);}
\DoxyCodeLine{00435\ \ \ \ \ \textcolor{keywordflow}{if}\ (rv\ !=\ 0)\ \{}
\DoxyCodeLine{00436\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ join\ thread:\ \%d\(\backslash\)n"{}},\ rv);}
\DoxyCodeLine{00437\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00438\ \ \ \ \ \}}
\DoxyCodeLine{00439\ \ \ \ \ th-\/>th\ =\ (pthread\_t)-\/1;}
\DoxyCodeLine{00440\ \ \ \ \ \textcolor{keywordflow}{return}\ (\textcolor{keywordtype}{int})(long)exit;}
\DoxyCodeLine{00441\ \}}
\DoxyCodeLine{00442\ }
\DoxyCodeLine{00443\ sbmsgq\_t\ *}
\DoxyCodeLine{00444\ sbmsgq\_init(\textcolor{keywordtype}{size\_t}\ depth)}
\DoxyCodeLine{00445\ \{}
\DoxyCodeLine{00446\ \ \ \ \ sbmsgq\_t\ *msgq;}
\DoxyCodeLine{00447\ }
\DoxyCodeLine{00448\ \ \ \ \ msgq\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*msgq));}
\DoxyCodeLine{00449\ \ \ \ \ msgq-\/>depth\ =\ depth;}
\DoxyCodeLine{00450\ \ \ \ \ \textcolor{keywordflow}{if}\ (pthread\_cond\_init(\&msgq-\/>cond,\ NULL)\ !=\ 0)\ \{}
\DoxyCodeLine{00451\ \ \ \ \ \ \ \ \ ckd\_free(msgq);}
\DoxyCodeLine{00452\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00453\ \ \ \ \ \}}
\DoxyCodeLine{00454\ \ \ \ \ \textcolor{keywordflow}{if}\ (pthread\_mutex\_init(\&msgq-\/>mtx,\ NULL)\ !=\ 0)\ \{}
\DoxyCodeLine{00455\ \ \ \ \ \ \ \ \ pthread\_cond\_destroy(\&msgq-\/>cond);}
\DoxyCodeLine{00456\ \ \ \ \ \ \ \ \ ckd\_free(msgq);}
\DoxyCodeLine{00457\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00458\ \ \ \ \ \}}
\DoxyCodeLine{00459\ \ \ \ \ msgq-\/>data\ =\ ckd\_calloc(depth,\ 1);}
\DoxyCodeLine{00460\ \ \ \ \ msgq-\/>msg\ =\ ckd\_calloc(depth,\ 1);}
\DoxyCodeLine{00461\ \ \ \ \ \textcolor{keywordflow}{return}\ msgq;}
\DoxyCodeLine{00462\ \}}
\DoxyCodeLine{00463\ }
\DoxyCodeLine{00464\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00465\ sbmsgq\_free(sbmsgq\_t\ *msgq)}
\DoxyCodeLine{00466\ \{}
\DoxyCodeLine{00467\ \ \ \ \ pthread\_mutex\_destroy(\&msgq-\/>mtx);}
\DoxyCodeLine{00468\ \ \ \ \ pthread\_cond\_destroy(\&msgq-\/>cond);}
\DoxyCodeLine{00469\ \ \ \ \ ckd\_free(msgq-\/>data);}
\DoxyCodeLine{00470\ \ \ \ \ ckd\_free(msgq-\/>msg);}
\DoxyCodeLine{00471\ \ \ \ \ ckd\_free(msgq);}
\DoxyCodeLine{00472\ \}}
\DoxyCodeLine{00473\ }
\DoxyCodeLine{00474\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00475\ sbmsgq\_send(sbmsgq\_t\ *q,\ \textcolor{keywordtype}{size\_t}\ len,\ \textcolor{keywordtype}{void}\ \textcolor{keyword}{const}\ *data)}
\DoxyCodeLine{00476\ \{}
\DoxyCodeLine{00477\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ in;}
\DoxyCodeLine{00478\ }
\DoxyCodeLine{00479\ \ \ \ \ \textcolor{comment}{/*\ Don't\ allow\ things\ bigger\ than\ depth\ to\ be\ sent!\ */}}
\DoxyCodeLine{00480\ \ \ \ \ \textcolor{keywordflow}{if}\ (len\ +\ \textcolor{keyword}{sizeof}(len)\ >\ q-\/>depth)}
\DoxyCodeLine{00481\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00482\ }
\DoxyCodeLine{00483\ \ \ \ \ \textcolor{comment}{/*\ Lock\ the\ condition\ variable\ while\ we\ manipulate\ the\ buffer.\ */}}
\DoxyCodeLine{00484\ \ \ \ \ pthread\_mutex\_lock(\&q-\/>mtx);}
\DoxyCodeLine{00485\ \ \ \ \ \textcolor{keywordflow}{if}\ (q-\/>nbytes\ +\ len\ +\ \textcolor{keyword}{sizeof}(len)\ >\ q-\/>depth)\ \{}
\DoxyCodeLine{00486\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Unlock\ and\ wait\ for\ space\ to\ be\ available.\ */}}
\DoxyCodeLine{00487\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (pthread\_cond\_wait(\&q-\/>cond,\ \&q-\/>mtx)\ !=\ 0)\ \{}
\DoxyCodeLine{00488\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Timed\ out,\ don't\ send\ anything.\ */}}
\DoxyCodeLine{00489\ \ \ \ \ \ \ \ \ \ \ \ \ pthread\_mutex\_unlock(\&q-\/>mtx);}
\DoxyCodeLine{00490\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00491\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00492\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Condition\ is\ now\ locked\ again.\ */}}
\DoxyCodeLine{00493\ \ \ \ \ \}}
\DoxyCodeLine{00494\ \ \ \ \ in\ =\ (q-\/>out\ +\ q-\/>nbytes)\ \%\ q-\/>depth;}
\DoxyCodeLine{00495\ }
\DoxyCodeLine{00496\ \ \ \ \ \textcolor{comment}{/*\ First\ write\ the\ size\ of\ the\ message.\ */}}
\DoxyCodeLine{00497\ \ \ \ \ \textcolor{keywordflow}{if}\ (in\ +\ \textcolor{keyword}{sizeof}(len)\ >\ q-\/>depth)\ \{}
\DoxyCodeLine{00498\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Handle\ the\ annoying\ case\ where\ the\ size\ field\ gets\ wrapped\ around.\ */}}
\DoxyCodeLine{00499\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len1\ =\ q-\/>depth\ -\/\ in;}
\DoxyCodeLine{00500\ \ \ \ \ \ \ \ \ memcpy(q-\/>data\ +\ in,\ \&len,\ len1);}
\DoxyCodeLine{00501\ \ \ \ \ \ \ \ \ memcpy(q-\/>data,\ ((\textcolor{keywordtype}{char}\ *)\&len)\ +\ len1,\ \textcolor{keyword}{sizeof}(len)\ -\/\ len1);}
\DoxyCodeLine{00502\ \ \ \ \ \ \ \ \ q-\/>nbytes\ +=\ \textcolor{keyword}{sizeof}(len);}
\DoxyCodeLine{00503\ \ \ \ \ \ \ \ \ in\ =\ \textcolor{keyword}{sizeof}(len)\ -\/\ len1;}
\DoxyCodeLine{00504\ \ \ \ \ \}}
\DoxyCodeLine{00505\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00506\ \ \ \ \ \ \ \ \ memcpy(q-\/>data\ +\ in,\ \&len,\ \textcolor{keyword}{sizeof}(len));}
\DoxyCodeLine{00507\ \ \ \ \ \ \ \ \ q-\/>nbytes\ +=\ \textcolor{keyword}{sizeof}(len);}
\DoxyCodeLine{00508\ \ \ \ \ \ \ \ \ in\ +=\ \textcolor{keyword}{sizeof}(len);}
\DoxyCodeLine{00509\ \ \ \ \ \}}
\DoxyCodeLine{00510\ }
\DoxyCodeLine{00511\ \ \ \ \ \textcolor{comment}{/*\ Now\ write\ the\ message\ body.\ */}}
\DoxyCodeLine{00512\ \ \ \ \ \textcolor{keywordflow}{if}\ (in\ +\ len\ >\ q-\/>depth)\ \{}
\DoxyCodeLine{00513\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Handle\ wraparound.\ */}}
\DoxyCodeLine{00514\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len1\ =\ q-\/>depth\ -\/\ in;}
\DoxyCodeLine{00515\ \ \ \ \ \ \ \ \ memcpy(q-\/>data\ +\ in,\ data,\ len1);}
\DoxyCodeLine{00516\ \ \ \ \ \ \ \ \ q-\/>nbytes\ +=\ len1;}
\DoxyCodeLine{00517\ \ \ \ \ \ \ \ \ data\ =\ (\textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *)data\ +\ len1;}
\DoxyCodeLine{00518\ \ \ \ \ \ \ \ \ len\ -\/=\ len1;}
\DoxyCodeLine{00519\ \ \ \ \ \ \ \ \ in\ =\ 0;}
\DoxyCodeLine{00520\ \ \ \ \ \}}
\DoxyCodeLine{00521\ \ \ \ \ memcpy(q-\/>data\ +\ in,\ data,\ len);}
\DoxyCodeLine{00522\ \ \ \ \ q-\/>nbytes\ +=\ len;}
\DoxyCodeLine{00523\ }
\DoxyCodeLine{00524\ \ \ \ \ \textcolor{comment}{/*\ Signal\ the\ condition\ variable.\ */}}
\DoxyCodeLine{00525\ \ \ \ \ pthread\_cond\_signal(\&q-\/>cond);}
\DoxyCodeLine{00526\ \ \ \ \ \textcolor{comment}{/*\ Unlock\ it,\ we\ have\ nothing\ else\ to\ do.\ */}}
\DoxyCodeLine{00527\ \ \ \ \ pthread\_mutex\_unlock(\&q-\/>mtx);}
\DoxyCodeLine{00528\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00529\ \}}
\DoxyCodeLine{00530\ }
\DoxyCodeLine{00531\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00532\ cond\_timed\_wait(pthread\_cond\_t\ *cond,\ pthread\_mutex\_t\ *mtx,\ \textcolor{keywordtype}{int}\ sec,\ \textcolor{keywordtype}{int}\ nsec)}
\DoxyCodeLine{00533\ \{}
\DoxyCodeLine{00534\ \ \ \ \ \textcolor{keywordtype}{int}\ rv;}
\DoxyCodeLine{00535\ \ \ \ \ \textcolor{keywordflow}{if}\ (sec\ ==\ -\/1)\ \{}
\DoxyCodeLine{00536\ \ \ \ \ \ \ \ \ rv\ =\ pthread\_cond\_wait(cond,\ mtx);}
\DoxyCodeLine{00537\ \ \ \ \ \}}
\DoxyCodeLine{00538\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00539\ \ \ \ \ \ \ \ \ \textcolor{keyword}{struct\ }timeval\ now;}
\DoxyCodeLine{00540\ \ \ \ \ \ \ \ \ \textcolor{keyword}{struct\ }timespec\ end;}
\DoxyCodeLine{00541\ }
\DoxyCodeLine{00542\ \ \ \ \ \ \ \ \ gettimeofday(\&now,\ NULL);}
\DoxyCodeLine{00543\ \ \ \ \ \ \ \ \ end.tv\_sec\ =\ now.tv\_sec\ +\ sec;}
\DoxyCodeLine{00544\ \ \ \ \ \ \ \ \ end.tv\_nsec\ =\ now.tv\_usec\ *\ 1000\ +\ nsec;}
\DoxyCodeLine{00545\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (end.tv\_nsec\ >\ (1000*1000*1000))\ \{}
\DoxyCodeLine{00546\ \ \ \ \ \ \ \ \ \ \ \ \ sec\ +=\ end.tv\_nsec\ /\ (1000*1000*1000);}
\DoxyCodeLine{00547\ \ \ \ \ \ \ \ \ \ \ \ \ end.tv\_nsec\ =\ end.tv\_nsec\ \%\ (1000*1000*1000);}
\DoxyCodeLine{00548\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00549\ \ \ \ \ \ \ \ \ rv\ =\ pthread\_cond\_timedwait(cond,\ mtx,\ \&end);}
\DoxyCodeLine{00550\ \ \ \ \ \}}
\DoxyCodeLine{00551\ \ \ \ \ \textcolor{keywordflow}{return}\ rv;}
\DoxyCodeLine{00552\ \}}
\DoxyCodeLine{00553\ }
\DoxyCodeLine{00554\ \textcolor{keywordtype}{void}\ *}
\DoxyCodeLine{00555\ sbmsgq\_wait(sbmsgq\_t\ *q,\ \textcolor{keywordtype}{size\_t}\ *out\_len,\ \textcolor{keywordtype}{int}\ sec,\ \textcolor{keywordtype}{int}\ nsec)}
\DoxyCodeLine{00556\ \{}
\DoxyCodeLine{00557\ \ \ \ \ \textcolor{keywordtype}{char}\ *outptr;}
\DoxyCodeLine{00558\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len;}
\DoxyCodeLine{00559\ }
\DoxyCodeLine{00560\ \ \ \ \ \textcolor{comment}{/*\ Lock\ the\ condition\ variable\ while\ we\ manipulate\ nmsg.\ */}}
\DoxyCodeLine{00561\ \ \ \ \ pthread\_mutex\_lock(\&q-\/>mtx);}
\DoxyCodeLine{00562\ \ \ \ \ \textcolor{keywordflow}{if}\ (q-\/>nbytes\ ==\ 0)\ \{}
\DoxyCodeLine{00563\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Unlock\ the\ condition\ variable\ and\ wait\ for\ a\ signal.\ */}}
\DoxyCodeLine{00564\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (cond\_timed\_wait(\&q-\/>cond,\ \&q-\/>mtx,\ sec,\ nsec)\ !=\ 0)\ \{}
\DoxyCodeLine{00565\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Timed\ out\ or\ something...\ */}}
\DoxyCodeLine{00566\ \ \ \ \ \ \ \ \ \ \ \ \ pthread\_mutex\_unlock(\&q-\/>mtx);}
\DoxyCodeLine{00567\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00568\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00569\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Condition\ variable\ is\ now\ locked\ again.\ */}}
\DoxyCodeLine{00570\ \ \ \ \ \}}
\DoxyCodeLine{00571\ \ \ \ \ \textcolor{comment}{/*\ Get\ the\ message\ size.\ */}}
\DoxyCodeLine{00572\ \ \ \ \ \textcolor{keywordflow}{if}\ (q-\/>out\ +\ \textcolor{keyword}{sizeof}(q-\/>msglen)\ >\ q-\/>depth)\ \{}
\DoxyCodeLine{00573\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Handle\ annoying\ wraparound\ case.\ */}}
\DoxyCodeLine{00574\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len1\ =\ q-\/>depth\ -\/\ q-\/>out;}
\DoxyCodeLine{00575\ \ \ \ \ \ \ \ \ memcpy(\&q-\/>msglen,\ q-\/>data\ +\ q-\/>out,\ len1);}
\DoxyCodeLine{00576\ \ \ \ \ \ \ \ \ memcpy(((\textcolor{keywordtype}{char}\ *)\&q-\/>msglen)\ +\ len1,\ q-\/>data,}
\DoxyCodeLine{00577\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(q-\/>msglen)\ -\/\ len1);}
\DoxyCodeLine{00578\ \ \ \ \ \ \ \ \ q-\/>out\ =\ \textcolor{keyword}{sizeof}(q-\/>msglen)\ -\/\ len1;}
\DoxyCodeLine{00579\ \ \ \ \ \}}
\DoxyCodeLine{00580\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00581\ \ \ \ \ \ \ \ \ memcpy(\&q-\/>msglen,\ q-\/>data\ +\ q-\/>out,\ \textcolor{keyword}{sizeof}(q-\/>msglen));}
\DoxyCodeLine{00582\ \ \ \ \ \ \ \ \ q-\/>out\ +=\ \textcolor{keyword}{sizeof}(q-\/>msglen);}
\DoxyCodeLine{00583\ \ \ \ \ \}}
\DoxyCodeLine{00584\ \ \ \ \ q-\/>nbytes\ -\/=\ \textcolor{keyword}{sizeof}(q-\/>msglen);}
\DoxyCodeLine{00585\ \ \ \ \ \textcolor{comment}{/*\ Get\ the\ message\ body.\ */}}
\DoxyCodeLine{00586\ \ \ \ \ outptr\ =\ q-\/>msg;}
\DoxyCodeLine{00587\ \ \ \ \ len\ =\ q-\/>msglen;}
\DoxyCodeLine{00588\ \ \ \ \ \textcolor{keywordflow}{if}\ (q-\/>out\ +\ q-\/>msglen\ >\ q-\/>depth)\ \{}
\DoxyCodeLine{00589\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Handle\ wraparound.\ */}}
\DoxyCodeLine{00590\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len1\ =\ q-\/>depth\ -\/\ q-\/>out;}
\DoxyCodeLine{00591\ \ \ \ \ \ \ \ \ memcpy(outptr,\ q-\/>data\ +\ q-\/>out,\ len1);}
\DoxyCodeLine{00592\ \ \ \ \ \ \ \ \ outptr\ +=\ len1;}
\DoxyCodeLine{00593\ \ \ \ \ \ \ \ \ len\ -\/=\ len1;}
\DoxyCodeLine{00594\ \ \ \ \ \ \ \ \ q-\/>nbytes\ -\/=\ len1;}
\DoxyCodeLine{00595\ \ \ \ \ \ \ \ \ q-\/>out\ =\ 0;}
\DoxyCodeLine{00596\ \ \ \ \ \}}
\DoxyCodeLine{00597\ \ \ \ \ memcpy(outptr,\ q-\/>data\ +\ q-\/>out,\ len);}
\DoxyCodeLine{00598\ \ \ \ \ q-\/>nbytes\ -\/=\ len;}
\DoxyCodeLine{00599\ \ \ \ \ q-\/>out\ +=\ len;}
\DoxyCodeLine{00600\ }
\DoxyCodeLine{00601\ \ \ \ \ \textcolor{comment}{/*\ Signal\ the\ condition\ variable.\ */}}
\DoxyCodeLine{00602\ \ \ \ \ pthread\_cond\_signal(\&q-\/>cond);}
\DoxyCodeLine{00603\ \ \ \ \ \textcolor{comment}{/*\ Unlock\ the\ condition\ variable,\ we\ are\ done.\ */}}
\DoxyCodeLine{00604\ \ \ \ \ pthread\_mutex\_unlock(\&q-\/>mtx);}
\DoxyCodeLine{00605\ \ \ \ \ \textcolor{keywordflow}{if}\ (out\_len)}
\DoxyCodeLine{00606\ \ \ \ \ \ \ \ \ *out\_len\ =\ q-\/>msglen;}
\DoxyCodeLine{00607\ \ \ \ \ \textcolor{keywordflow}{return}\ q-\/>msg;}
\DoxyCodeLine{00608\ \}}
\DoxyCodeLine{00609\ }
\DoxyCodeLine{00610\ sbevent\_t\ *}
\DoxyCodeLine{00611\ sbevent\_init(\textcolor{keywordtype}{void})}
\DoxyCodeLine{00612\ \{}
\DoxyCodeLine{00613\ \ \ \ \ sbevent\_t\ *evt;}
\DoxyCodeLine{00614\ \ \ \ \ \textcolor{keywordtype}{int}\ rv;}
\DoxyCodeLine{00615\ }
\DoxyCodeLine{00616\ \ \ \ \ evt\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*evt));}
\DoxyCodeLine{00617\ \ \ \ \ \textcolor{keywordflow}{if}\ ((rv\ =\ pthread\_mutex\_init(\&evt-\/>mtx,\ NULL))\ !=\ 0)\ \{}
\DoxyCodeLine{00618\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ initialize\ mutex:\ \%d\(\backslash\)n"{}},\ rv);}
\DoxyCodeLine{00619\ \ \ \ \ \ \ \ \ ckd\_free(evt);}
\DoxyCodeLine{00620\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00621\ \ \ \ \ \}}
\DoxyCodeLine{00622\ \ \ \ \ \textcolor{keywordflow}{if}\ ((rv\ =\ pthread\_cond\_init(\&evt-\/>cond,\ NULL))\ !=\ 0)\ \{}
\DoxyCodeLine{00623\ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ initialize\ mutex:\ \%d\(\backslash\)n"{}},\ rv);}
\DoxyCodeLine{00624\ \ \ \ \ \ \ \ \ pthread\_mutex\_destroy(\&evt-\/>mtx);}
\DoxyCodeLine{00625\ \ \ \ \ \ \ \ \ ckd\_free(evt);}
\DoxyCodeLine{00626\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00627\ \ \ \ \ \}}
\DoxyCodeLine{00628\ \ \ \ \ \textcolor{keywordflow}{return}\ evt;}
\DoxyCodeLine{00629\ \}}
\DoxyCodeLine{00630\ }
\DoxyCodeLine{00631\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00632\ sbevent\_free(sbevent\_t\ *evt)}
\DoxyCodeLine{00633\ \{}
\DoxyCodeLine{00634\ \ \ \ \ pthread\_mutex\_destroy(\&evt-\/>mtx);}
\DoxyCodeLine{00635\ \ \ \ \ pthread\_cond\_destroy(\&evt-\/>cond);}
\DoxyCodeLine{00636\ \ \ \ \ ckd\_free(evt);}
\DoxyCodeLine{00637\ \}}
\DoxyCodeLine{00638\ }
\DoxyCodeLine{00639\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00640\ sbevent\_signal(sbevent\_t\ *evt)}
\DoxyCodeLine{00641\ \{}
\DoxyCodeLine{00642\ \ \ \ \ \textcolor{keywordtype}{int}\ rv;}
\DoxyCodeLine{00643\ }
\DoxyCodeLine{00644\ \ \ \ \ pthread\_mutex\_lock(\&evt-\/>mtx);}
\DoxyCodeLine{00645\ \ \ \ \ evt-\/>signalled\ =\ TRUE;}
\DoxyCodeLine{00646\ \ \ \ \ rv\ =\ pthread\_cond\_signal(\&evt-\/>cond);}
\DoxyCodeLine{00647\ \ \ \ \ pthread\_mutex\_unlock(\&evt-\/>mtx);}
\DoxyCodeLine{00648\ \ \ \ \ \textcolor{keywordflow}{return}\ rv;}
\DoxyCodeLine{00649\ \}}
\DoxyCodeLine{00650\ }
\DoxyCodeLine{00651\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00652\ sbevent\_wait(sbevent\_t\ *evt,\ \textcolor{keywordtype}{int}\ sec,\ \textcolor{keywordtype}{int}\ nsec)}
\DoxyCodeLine{00653\ \{}
\DoxyCodeLine{00654\ \ \ \ \ \textcolor{keywordtype}{int}\ rv\ =\ 0;}
\DoxyCodeLine{00655\ }
\DoxyCodeLine{00656\ \ \ \ \ \textcolor{comment}{/*\ Lock\ the\ mutex\ before\ we\ check\ its\ signalled\ state.\ */}}
\DoxyCodeLine{00657\ \ \ \ \ pthread\_mutex\_lock(\&evt-\/>mtx);}
\DoxyCodeLine{00658\ \ \ \ \ \textcolor{comment}{/*\ If\ it's\ not\ signalled,\ then\ wait\ until\ it\ is.\ */}}
\DoxyCodeLine{00659\ \ \ \ \ \textcolor{keywordflow}{if}\ (!evt-\/>signalled)}
\DoxyCodeLine{00660\ \ \ \ \ \ \ \ \ rv\ =\ cond\_timed\_wait(\&evt-\/>cond,\ \&evt-\/>mtx,\ sec,\ nsec);}
\DoxyCodeLine{00661\ \ \ \ \ \textcolor{comment}{/*\ Set\ its\ state\ to\ unsignalled\ if\ we\ were\ successful.\ */}}
\DoxyCodeLine{00662\ \ \ \ \ \textcolor{keywordflow}{if}\ (rv\ ==\ 0)}
\DoxyCodeLine{00663\ \ \ \ \ \ \ \ \ evt-\/>signalled\ =\ FALSE;}
\DoxyCodeLine{00664\ \ \ \ \ \textcolor{comment}{/*\ And\ unlock\ its\ mutex.\ */}}
\DoxyCodeLine{00665\ \ \ \ \ pthread\_mutex\_unlock(\&evt-\/>mtx);}
\DoxyCodeLine{00666\ }
\DoxyCodeLine{00667\ \ \ \ \ \textcolor{keywordflow}{return}\ rv;}
\DoxyCodeLine{00668\ \}}
\DoxyCodeLine{00669\ }
\DoxyCodeLine{00670\ sbmtx\_t\ *}
\DoxyCodeLine{00671\ sbmtx\_init(\textcolor{keywordtype}{void})}
\DoxyCodeLine{00672\ \{}
\DoxyCodeLine{00673\ \ \ \ \ sbmtx\_t\ *mtx;}
\DoxyCodeLine{00674\ }
\DoxyCodeLine{00675\ \ \ \ \ mtx\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*mtx));}
\DoxyCodeLine{00676\ \ \ \ \ \textcolor{keywordflow}{if}\ (pthread\_mutex\_init(\&mtx-\/>mtx,\ NULL)\ !=\ 0)\ \{}
\DoxyCodeLine{00677\ \ \ \ \ \ \ \ \ ckd\_free(mtx);}
\DoxyCodeLine{00678\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00679\ \ \ \ \ \}}
\DoxyCodeLine{00680\ \ \ \ \ \textcolor{keywordflow}{return}\ mtx;}
\DoxyCodeLine{00681\ \}}
\DoxyCodeLine{00682\ }
\DoxyCodeLine{00683\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00684\ sbmtx\_trylock(sbmtx\_t\ *mtx)}
\DoxyCodeLine{00685\ \{}
\DoxyCodeLine{00686\ \ \ \ \ \textcolor{keywordflow}{return}\ pthread\_mutex\_trylock(\&mtx-\/>mtx);}
\DoxyCodeLine{00687\ \}}
\DoxyCodeLine{00688\ }
\DoxyCodeLine{00689\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00690\ sbmtx\_lock(sbmtx\_t\ *mtx)}
\DoxyCodeLine{00691\ \{}
\DoxyCodeLine{00692\ \ \ \ \ \textcolor{keywordflow}{return}\ pthread\_mutex\_lock(\&mtx-\/>mtx);}
\DoxyCodeLine{00693\ \}}
\DoxyCodeLine{00694\ }
\DoxyCodeLine{00695\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00696\ sbmtx\_unlock(sbmtx\_t\ *mtx)}
\DoxyCodeLine{00697\ \{}
\DoxyCodeLine{00698\ \ \ \ \ \textcolor{keywordflow}{return}\ pthread\_mutex\_unlock(\&mtx-\/>mtx);}
\DoxyCodeLine{00699\ \}}
\DoxyCodeLine{00700\ }
\DoxyCodeLine{00701\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00702\ sbmtx\_free(sbmtx\_t\ *mtx)}
\DoxyCodeLine{00703\ \{}
\DoxyCodeLine{00704\ \ \ \ \ pthread\_mutex\_destroy(\&mtx-\/>mtx);}
\DoxyCodeLine{00705\ \ \ \ \ ckd\_free(mtx);}
\DoxyCodeLine{00706\ \}}
\DoxyCodeLine{00707\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{/*\ not\ WIN32\ */}\textcolor{preprocessor}{}}
\DoxyCodeLine{00708\ }
\DoxyCodeLine{00709\ cmd\_ln\_t\ *}
\DoxyCodeLine{00710\ sbthread\_config(sbthread\_t\ *th)}
\DoxyCodeLine{00711\ \{}
\DoxyCodeLine{00712\ \ \ \ \ \textcolor{keywordflow}{return}\ th-\/>config;}
\DoxyCodeLine{00713\ \}}
\DoxyCodeLine{00714\ }
\DoxyCodeLine{00715\ \textcolor{keywordtype}{void}\ *}
\DoxyCodeLine{00716\ sbthread\_arg(sbthread\_t\ *th)}
\DoxyCodeLine{00717\ \{}
\DoxyCodeLine{00718\ \ \ \ \ \textcolor{keywordflow}{return}\ th-\/>arg;}
\DoxyCodeLine{00719\ \}}
\DoxyCodeLine{00720\ }
\DoxyCodeLine{00721\ sbmsgq\_t\ *}
\DoxyCodeLine{00722\ sbthread\_msgq(sbthread\_t\ *th)}
\DoxyCodeLine{00723\ \{}
\DoxyCodeLine{00724\ \ \ \ \ \textcolor{keywordflow}{return}\ th-\/>msgq;}
\DoxyCodeLine{00725\ \}}
\DoxyCodeLine{00726\ }
\DoxyCodeLine{00727\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00728\ sbthread\_send(sbthread\_t\ *th,\ \textcolor{keywordtype}{size\_t}\ len,\ \textcolor{keywordtype}{void}\ \textcolor{keyword}{const}\ *data)}
\DoxyCodeLine{00729\ \{}
\DoxyCodeLine{00730\ \ \ \ \ \textcolor{keywordflow}{return}\ sbmsgq\_send(th-\/>msgq,\ len,\ data);}
\DoxyCodeLine{00731\ \}}
\DoxyCodeLine{00732\ }
\DoxyCodeLine{00733\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00734\ sbthread\_free(sbthread\_t\ *th)}
\DoxyCodeLine{00735\ \{}
\DoxyCodeLine{00736\ \ \ \ \ sbthread\_wait(th);}
\DoxyCodeLine{00737\ \ \ \ \ sbmsgq\_free(th-\/>msgq);}
\DoxyCodeLine{00738\ \ \ \ \ ckd\_free(th);}
\DoxyCodeLine{00739\ \}}

\end{DoxyCode}
