reference.html 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120
  1. <HTML>
  2. <HEAD>
  3. <TITLE>State Threads Library Reference</TITLE>
  4. </HEAD>
  5. <BODY BGCOLOR=#FFFFFF>
  6. <H2>State Threads Library Reference</H2>
  7. <DL>
  8. <DD><A HREF=#types><B>Types</B></A></DD>
  9. <DL><DD><A HREF=#thread_t>st_thread_t</A></DD></DL>
  10. <DL><DD><A HREF=#cond_t>st_cond_t</A></DD></DL>
  11. <DL><DD><A HREF=#mutex_t>st_mutex_t</A></DD></DL>
  12. <DL><DD><A HREF=#utime_t>st_utime_t</A></DD></DL>
  13. <DL><DD><A HREF=#netfd_t>st_netfd_t</A></DD></DL>
  14. <DL><DD><A HREF=#switch_cb_t>st_switch_cb_t</A></DD></DL>
  15. <P>
  16. <DD><A HREF=#errors><B>Error Handling</B></A></DD>
  17. <P>
  18. <DD><A HREF=#init><B>Library Initialization</B></A></DD>
  19. <P>
  20. <DL><DD><A HREF=#st_init>st_init()</A></DD></DL>
  21. <DL><DD><A HREF=#getfdlimit>st_getfdlimit()</A></DD></DL>
  22. <DL><DD><A HREF=#set_eventsys>st_set_eventsys()</A></DD></DL>
  23. <DL><DD><A HREF=#get_eventsys>st_get_eventsys()</A></DD></DL>
  24. <DL><DD><A HREF=#get_eventsys_name>st_get_eventsys_name()</A></DD></DL>
  25. <DL><DD><A HREF=#set_utime_function>st_set_utime_function()</A></DD></DL>
  26. <DL><DD><A HREF=#timecache_set>st_timecache_set()</A></DD></DL>
  27. <DL><DD><A HREF=#randomize_stacks>st_randomize_stacks()</A></DD></DL>
  28. <P>
  29. <DL><DD><A HREF=#switch_cb_t><B>st_switch_cb_t</B> type</A></DD></DL>
  30. <DL><DD><A HREF=#set_switch_in_cb>st_set_switch_in_cb()</A></DD></DL>
  31. <DL><DD><A HREF=#set_switch_out_cb>st_set_switch_out_cb()</A></DD></DL>
  32. <P>
  33. <DD><A HREF=#threads><B>Thread Control and Identification</B></A></DD>
  34. <P>
  35. <DL><DD><A HREF=#thread_t><B>st_thread_t</B> type</A></DD></DL>
  36. <DL><DD><A HREF=#thread_create>st_thread_create()</A></DD></DL>
  37. <DL><DD><A HREF=#thread_exit>st_thread_exit()</A></DD></DL>
  38. <DL><DD><A HREF=#thread_join>st_thread_join()</A></DD></DL>
  39. <DL><DD><A HREF=#thread_self>st_thread_self()</A></DD></DL>
  40. <DL><DD><A HREF=#thread_interrupt>st_thread_interrupt()</A></DD></DL>
  41. <DL><DD><A HREF=#sleep>st_sleep()</A></DD></DL>
  42. <DL><DD><A HREF=#sleep>st_usleep()</A></DD></DL>
  43. <DL><DD><A HREF=#randomize_stacks>st_randomize_stacks()</A></DD></DL>
  44. <P>
  45. <DD><A HREF=#priv><B>Per-Thread Private Data</B></A></DD>
  46. <P>
  47. <DL><DD><A HREF=#key_create>st_key_create()</A></DD></DL>
  48. <DL><DD><A HREF=#key_getlimit>st_key_getlimit()</A></DD></DL>
  49. <DL><DD><A HREF=#thread_setspecific>st_thread_setspecific()</A></DD></DL>
  50. <DL><DD><A HREF=#thread_getspecific>st_thread_getspecific()</A></DD></DL>
  51. <P>
  52. <DD><A HREF=#sync><B>Synchronization</B></A></DD>
  53. <P>
  54. <DL><DD><A HREF=#cond_t><B>st_cond_t</B> type</A></DD></DL>
  55. <DL><DD><A HREF=#cond_new>st_cond_new()</A></DD></DL>
  56. <DL><DD><A HREF=#cond_destroy>st_cond_destroy()</A></DD></DL>
  57. <DL><DD><A HREF=#cond_wait>st_cond_wait()</A></DD></DL>
  58. <DL><DD><A HREF=#cond_timedwait>st_cond_timedwait()</A></DD></DL>
  59. <DL><DD><A HREF=#cond_signal>st_cond_signal()</A></DD></DL>
  60. <DL><DD><A HREF=#cond_broadcast>st_cond_broadcast()</A></DD></DL>
  61. <P>
  62. <DL><DD><A HREF=#mutex_t><B>st_mutex_t</B> type</A></DD></DL>
  63. <DL><DD><A HREF=#mutex_new>st_mutex_new()</A></DD></DL>
  64. <DL><DD><A HREF=#mutex_destroy>st_mutex_destroy()</A></DD></DL>
  65. <DL><DD><A HREF=#mutex_lock>st_mutex_lock()</A></DD></DL>
  66. <DL><DD><A HREF=#mutex_trylock>st_mutex_trylock()</A></DD></DL>
  67. <DL><DD><A HREF=#mutex_unlock>st_mutex_unlock()</A></DD></DL>
  68. <P>
  69. <DD><A HREF=#timing><B>Timing</B></A></DD>
  70. <P>
  71. <DL><DD><A HREF=#utime_t><B>st_utime_t</B> type</A></DD></DL>
  72. <DL><DD><A HREF=#utime>st_utime()</A></DD></DL>
  73. <DL><DD><A HREF=#set_utime_function>st_set_utime_function()</A></DD></DL>
  74. <DL><DD><A HREF=#timecache_set>st_timecache_set()</A></DD></DL>
  75. <DL><DD><A HREF=#time>st_time()</A></DD></DL>
  76. <P>
  77. <DD><A HREF=#io><B>I/O Functions</B></A></DD>
  78. <P>
  79. <DL><DD><A HREF=#netfd_t><B>st_netfd_t</B> type</A></DD></DL>
  80. <DL><DD><A HREF=#netfd_open>st_netfd_open()</A></DD></DL>
  81. <DL><DD><A HREF=#netfd_open_socket>st_netfd_open_socket()</A></DD></DL>
  82. <DL><DD><A HREF=#netfd_free>st_netfd_free()</A></DD></DL>
  83. <DL><DD><A HREF=#netfd_close>st_netfd_close()</A></DD></DL>
  84. <DL><DD><A HREF=#netfd_fileno>st_netfd_fileno()</A></DD></DL>
  85. <DL><DD><A HREF=#netfd_setspecific>st_netfd_setspecific()</A></DD></DL>
  86. <DL><DD><A HREF=#netfd_getspecific>st_netfd_getspecific()</A></DD></DL>
  87. <DL><DD><A HREF=#netfd_serialize_accept>st_netfd_serialize_accept()</A></DD>
  88. </DL>
  89. <DL><DD><A HREF=#netfd_poll>st_netfd_poll()</A></DD></DL>
  90. <P>
  91. <DL><DD><A HREF=#accept>st_accept()</A></DD></DL>
  92. <DL><DD><A HREF=#connect>st_connect()</A></DD></DL>
  93. <DL><DD><A HREF=#read>st_read()</A></DD></DL>
  94. <DL><DD><A HREF=#read_fully>st_read_fully()</A></DD></DL>
  95. <DL><DD><A HREF=#read_resid>st_read_resid()</A></DD></DL>
  96. <DL><DD><A HREF=#readv>st_readv()</A></DD></DL>
  97. <DL><DD><A HREF=#readv_resid>st_readv_resid()</A></DD></DL>
  98. <DL><DD><A HREF=#write>st_write()</A></DD></DL>
  99. <DL><DD><A HREF=#write_resid>st_write_resid()</A></DD></DL>
  100. <DL><DD><A HREF=#writev>st_writev()</A></DD></DL>
  101. <DL><DD><A HREF=#writev_resid>st_writev_resid()</A></DD></DL>
  102. <DL><DD><A HREF=#recvfrom>st_recvfrom()</A></DD></DL>
  103. <DL><DD><A HREF=#sendto>st_sendto()</A></DD></DL>
  104. <DL><DD><A HREF=#recvmsg>st_recvmsg()</A></DD></DL>
  105. <DL><DD><A HREF=#sendmsg>st_sendmsg()</A></DD></DL>
  106. <P>
  107. <DL><DD><A HREF=#open>st_open()</A></DD></DL>
  108. <DL><DD><A HREF=#poll>st_poll()</A></DD></DL>
  109. <P>
  110. <DD><A HREF=#progr><B>Program Structure</B></A></DD>
  111. <P>
  112. <DD><A HREF=#block><B>List of Blocking Functions</B></A></DD>
  113. <P>
  114. </DL>
  115. <P>
  116. <HR>
  117. <P>
  118. <A NAME="types">
  119. <H2>Types</H2>
  120. </A>
  121. The State Thread library defines the following types in the <TT>st.h</TT>
  122. header file:
  123. <P>
  124. <DL>
  125. <DD><A HREF=#thread_t>st_thread_t</A></DD>
  126. <DD><A HREF=#cond_t>st_cond_t</A></DD>
  127. <DD><A HREF=#mutex_t>st_mutex_t</A></DD>
  128. <DD><A HREF=#utime_t>st_utime_t</A></DD>
  129. <DD><A HREF=#netfd_t>st_netfd_t</A></DD>
  130. </DL>
  131. <P>
  132. <HR>
  133. <P>
  134. <A NAME="thread_t">
  135. <H4>st_thread_t</H4>
  136. </A>
  137. Thread type.
  138. <P>
  139. <H5>Syntax</H5>
  140. <PRE>
  141. #include &lt;st.h&gt;
  142. typedef void * st_thread_t;
  143. </PRE>
  144. <P>
  145. <H5>Description</H5>
  146. A thread is represented and identified by a pointer to an opaque data
  147. structure. This pointer is a required parameter for most of the functions
  148. that operate on threads.
  149. <P>
  150. The thread identifier remains valid until the thread returns from its root
  151. function and, if the thread was created joinable, is joined.
  152. <P>
  153. <HR>
  154. <P>
  155. <A NAME="cond_t">
  156. <H4>st_cond_t</H4>
  157. </A>
  158. Condition variable type.
  159. <P>
  160. <H5>Syntax</H5>
  161. <PRE>
  162. #include &lt;st.h&gt;
  163. typedef void * st_cond_t;
  164. </PRE>
  165. <P>
  166. <H5>Description</H5>
  167. A condition variable is an opaque object identified by a pointer.
  168. Condition variables provide synchronization primitives to wait for or wake
  169. up threads waiting for certain conditions to be satisfied.
  170. <P>
  171. In the State Threads library there is no need to lock a mutex before
  172. waiting on a condition variable.
  173. <P>
  174. <HR>
  175. <P>
  176. <A NAME="mutex_t">
  177. <H4>st_mutex_t</H4>
  178. </A>
  179. Mutex type.
  180. <P>
  181. <H5>Syntax</H5>
  182. <PRE>
  183. #include &lt;st.h&gt;
  184. typedef void * st_mutex_t;
  185. </PRE>
  186. <P>
  187. <H5>Description</H5>
  188. A mutex is an opaque object identified by a pointer.
  189. Mutual exclusion locks (mutexes) are used to serialize the execution of
  190. threads through critical sections of code.
  191. <P>
  192. If application using the State Threads library is written with no
  193. I/O or control yielding in critical sections (that is no
  194. <A HREF=#block>blocking functions</A> in critical sections), then there is
  195. no need for mutexes.<P>
  196. These mutexes can only be used for intra-process thread synchronization.
  197. They cannot be used for inter-process synchronization.
  198. <P>
  199. <HR>
  200. <P>
  201. <A NAME="utime_t">
  202. <H4>st_utime_t</H4>
  203. </A>
  204. High resolution time type ("u" stands for "micro").
  205. <P>
  206. <H5>Syntax</H5>
  207. <PRE>
  208. #include &lt;st.h&gt;
  209. typedef unsigned long long st_utime_t;
  210. </PRE>
  211. <P>
  212. <H5>Description</H5>
  213. This datatype (unsigned 64-bit integer) represents high-resolution real time
  214. expressed in microseconds since some arbitrary time in the past. It is not
  215. correlated in any way to the time of day.
  216. <P>
  217. <HR>
  218. <P>
  219. <A NAME="netfd_t">
  220. <H4>st_netfd_t</H4>
  221. </A>
  222. File descriptor type.
  223. <P>
  224. <H5>Syntax</H5>
  225. <PRE>
  226. #include &lt;st.h&gt;
  227. typedef void * st_netfd_t;
  228. </PRE>
  229. <P>
  230. <H5>Description</H5>
  231. This datatype typically represents any open end point of network
  232. communication (socket, end point of a pipe, FIFO, etc.) but can
  233. encapsulate any open file descriptor. Objects of this type are
  234. identified by a pointer to an opaque data structure.
  235. <P>
  236. <HR>
  237. <P>
  238. <A NAME="switch_cb_t">
  239. <H4>st_switch_cb_t</H4>
  240. </A>
  241. Context switch callback function type.
  242. <P>
  243. <H5>Syntax</H5>
  244. <PRE>
  245. #include &lt;st.h&gt;
  246. typedef void (*st_switch_cb_t)(void);
  247. </PRE>
  248. <P>
  249. <H5>Description</H5>
  250. This datatype is a convenience type for describing a pointer
  251. to a function that will be called when a thread is set to stop
  252. or set to run.
  253. This feature is available only when <TT>ST_SWITCH_CB</TT> is defined
  254. in <TT>&lt;st.h&gt;</TT>.
  255. <P>
  256. <HR>
  257. <P>
  258. <A NAME="errors">
  259. <H2>Error Handling</H2>
  260. </A>
  261. All State Threads library non-void functions return on success either a
  262. non-negative integer or a pointer to a newly created object (constructor-type
  263. functions). On failure they return either <TT>-1</TT> or a <TT>NULL</TT>
  264. pointer respectively and set global <TT>errno</TT> to indicate the error.
  265. It is safe to use <TT>errno</TT> because it is set right before the function
  266. return and only one thread at a time can modify its value.<P>
  267. The <TT>perror(3)</TT> function can be used to produce an error message on the
  268. standard error output.
  269. <P>
  270. <HR>
  271. <P>
  272. <A NAME="init">
  273. <H2>Library Initialization</H2>
  274. </A>
  275. <P>
  276. <DL>
  277. <DD><A HREF=#st_init>st_init()</A></DD>
  278. <DD><A HREF=#getfdlimit>st_getfdlimit()</A></DD>
  279. <DD><A HREF=#set_eventsys>st_set_eventsys()</A></DD>
  280. <DD><A HREF=#get_eventsys>st_get_eventsys()</A></DD>
  281. <DD><A HREF=#get_eventsys_name>st_get_eventsys_name()</A></DD>
  282. <P>
  283. These functions operate on a callback function of type
  284. <A HREF=#switch_cb_t><B>st_switch_cb_t</B></A>:
  285. <DD><A HREF=#set_switch_in_cb>st_set_switch_in_cb()</A></DD>
  286. <DD><A HREF=#set_switch_out_cb>st_set_switch_out_cb()</A></DD>
  287. </DL>
  288. <P>
  289. <HR>
  290. <P>
  291. <A NAME="st_init">
  292. <H4>st_init()</H4>
  293. </A>
  294. Initializes the runtime.
  295. <P>
  296. <H5>Syntax</H5>
  297. <PRE>
  298. #include &lt;st.h&gt;
  299. int st_init(void);
  300. </PRE>
  301. <P>
  302. <H5>Parameters</H5>
  303. None.
  304. <P>
  305. <H5>Returns</H5>
  306. Upon successful completion, a value of <TT>0</TT> is returned.
  307. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  308. to indicate the error.
  309. <P>
  310. <H5>Description</H5>
  311. This function initializes the library runtime. It should be called near
  312. the beginning of the application's <TT>main()</TT> function before any other
  313. State Threads library function is called.<P>
  314. Among other things, this function limits the number of open file descriptors
  315. to the OS imposed per-process maximum number or, if <TT>select(2)</TT> is
  316. used, to <TT>FD_SETSIZE</TT>, whichever is less (<TT>getrlimit(2)</TT>).
  317. This limit can be
  318. retrieved by <A HREF=#getfdlimit>st_getfdlimit()</A>. It also sets the
  319. disposition of the <TT>SIGPIPE</TT> signal to <TT>SIG_IGN</TT> (to be ignored)
  320. (<TT>signal(5)</TT>).
  321. <P>
  322. Unlike POSIX threads, a new process created by the <TT>fork(2)</TT> system
  323. call is an <I>exact</I> copy of the calling process and all state threads
  324. which are running in the parent do exist in the child. That means that
  325. <TT>st_init()</TT> may be called either before or after multiple processes
  326. are created by <TT>fork(2)</TT>.
  327. <P>
  328. If the library runtime is not properly initialized (e.g., <TT>st_init()</TT>
  329. is accidentally omitted), then the process will receive either an arithmetic
  330. exception (SIGFPE or SIGTRAP) or segmentation fault (SIGSEGV) signal upon
  331. new thread creation or the first context switch, respectively.
  332. <P>
  333. <HR>
  334. <P>
  335. <A NAME="getfdlimit">
  336. <H4>st_getfdlimit()</H4>
  337. </A>
  338. Returns the maximum number of file descriptors that the calling process
  339. can open.
  340. <P>
  341. <H5>Syntax</H5>
  342. <PRE>
  343. #include &lt;st.h&gt;
  344. int st_getfdlimit(void);
  345. </PRE>
  346. <P>
  347. <H5>Parameters</H5>
  348. None.
  349. <P>
  350. <H5>Returns</H5>
  351. The maximum number of file descriptors that the calling process can open.
  352. If this function is called before the library is successfully initialized by
  353. <A HREF=#st_init>st_init()</A>, a value of <TT>-1</TT> is returned.
  354. <P>
  355. <H5>Description</H5>
  356. This function returns the limit on the number of open file descriptors which
  357. is set by the <A HREF=#st_init>st_init()</A> function.
  358. <P>
  359. <HR>
  360. <P>
  361. <A NAME="set_eventsys">
  362. <H4>st_set_eventsys()</H4>
  363. </A>
  364. Sets event notification mechanism.
  365. <P>
  366. <H5>Syntax</H5>
  367. <PRE>
  368. #include &lt;st.h&gt;
  369. int st_set_eventsys(int eventsys);
  370. </PRE>
  371. <P>
  372. <H5>Parameters</H5>
  373. <TT>st_set_eventsys()</TT> has the following parameter:<P>
  374. <TT>eventsys</TT><P>
  375. An integer value identifying selected event notification mechanism. The
  376. following values are defined in the <TT>st.h</TT> header file:
  377. <P>
  378. <TABLE BORDER=0 CELLPADDING=5 CELLSPACING=15>
  379. <TR>
  380. <TD VALIGN=TOP><TT>ST_EVENTSYS_DEFAULT</TT></TD>
  381. <TD>Use default event notification mechanism. Usually it's <TT>select(2)</TT>
  382. but if the library was compiled with the <TT>USE_POLL</TT> macro defined
  383. then the default is <TT>poll(2)</TT>.</TD>
  384. </TR>
  385. <TR>
  386. <TD VALIGN=TOP><TT>ST_EVENTSYS_SELECT</TT></TD>
  387. <TD>Use <TT>select(2)</TT> as an event notification mechanism.</TD>
  388. </TR>
  389. <TD VALIGN=TOP><TT>ST_EVENTSYS_POLL</TT></TD>
  390. <TD>Use <TT>poll(2)</TT> as an event notification mechanism.</TD>
  391. </TR>
  392. <TD VALIGN=TOP><TT>ST_EVENTSYS_ALT</TT></TD>
  393. <TD>Use an alternative event notification mechanism. The actual
  394. mechanism selected depends on OS support. For example, <TT>epoll(4)</TT>
  395. will be used on Linux if supported and <TT>kqueue(2)</TT> will be used
  396. on FreeBSD/OpenBSD. If the OS supports no alternative event
  397. notification mechanism, setting <TT>ST_EVENTSYS_ALT</TT> has no effect
  398. and the <TT>ST_EVENTSYS_DEFAULT</TT> mechanism will be used.</TD>
  399. </TR>
  400. </TABLE>
  401. <P>
  402. <H5>Returns</H5>
  403. Upon successful completion, a value of <TT>0</TT> is returned.
  404. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  405. to indicate the error:<P>
  406. <TABLE BORDER=0 CELLSPACING=15>
  407. <TR><TD><TT>EINVAL</TT></TD>
  408. <TD>
  409. The supplied <TT>eventsys</TT> parameter has an invalid value.
  410. </TD></TR>
  411. <TR><TD><TT>EBUSY</TT></TD>
  412. <TD>
  413. The event notification mechanism has already been set.
  414. </TD></TR>
  415. </TABLE>
  416. <P>
  417. <H5>Description</H5>
  418. This function sets the event notification mechanism that will be used by
  419. the State Threads library. To have any effect, it must be called
  420. <i>before</i> the <A HREF=#st_init>st_init()</A> function which performs
  421. the actual initialization. If <TT>st_set_eventsys()</TT> is not called,
  422. <A HREF=#st_init>st_init()</A> will set the <TT>ST_EVENTSYS_DEFAULT</TT>
  423. mechanism. The mechanism cannot be changed once set.
  424. <P>
  425. There are no strict rules for selecting an event notification
  426. mechanism. The "best" one depends on how your application behaves.
  427. Try a few to see which one works best for you. As a rule of
  428. thumb, you should use the <TT>ST_EVENTSYS_ALT</TT> mechanism if your
  429. application deals with a very large number of network connections of
  430. which only a few are active at once.
  431. <P>
  432. <HR>
  433. <P>
  434. <A NAME="get_eventsys">
  435. <H4>st_get_eventsys()</H4>
  436. </A>
  437. Returns the integer value identifying the event notification mechanism
  438. being used by the State Threads library.
  439. <P>
  440. <H5>Syntax</H5>
  441. <PRE>
  442. #include &lt;st.h&gt;
  443. int st_get_eventsys(void);
  444. </PRE>
  445. <P>
  446. <H5>Parameters</H5>
  447. None.
  448. <P>
  449. <H5>Returns</H5>
  450. The integer value identifying the current event notification mechanism.
  451. This value can be one of the following (see <A
  452. HREF=#set_eventsys>st_set_eventsys()</A>):
  453. <TT>ST_EVENTSYS_SELECT</TT>, <TT>ST_EVENTSYS_POLL</TT>, or
  454. <TT>ST_EVENTSYS_ALT</TT>. Future versions of the library may return other
  455. values. If a mechanism hasn't been set yet, a value of <TT>-1</TT> is returned.
  456. <P>
  457. <H5>Description</H5>
  458. This function returns the integer value identifying the event notification
  459. mechanism which is actually being used by the State Threads library.
  460. <P>
  461. <HR>
  462. <P>
  463. <A NAME="get_eventsys_name">
  464. <H4>st_get_eventsys_name()</H4>
  465. </A>
  466. Returns the name of the event notification mechanism being used by the
  467. State Threads library.
  468. <P>
  469. <H5>Syntax</H5>
  470. <PRE>
  471. #include &lt;st.h&gt;
  472. const char *st_get_eventsys_name(void);
  473. </PRE>
  474. <P>
  475. <H5>Parameters</H5>
  476. None.
  477. <P>
  478. <H5>Returns</H5>
  479. The string identifying the current event notification mechanism. If a
  480. mechanism hasn't been set yet (see <A
  481. HREF=#set_eventsys>st_set_eventsys()</A>), an empty string is
  482. returned. Possible return values are <TT>"select"</TT>,
  483. <TT>"poll"</TT>, <TT>"kqueue"</TT>, or <TT>"epoll"</TT>. Future versions
  484. of the library may return other values.
  485. <P>
  486. <H5>Description</H5>
  487. This function returns the string identifying the event notification
  488. mechanism which is actually being used by the State Threads library.
  489. <P>
  490. <HR>
  491. <P>
  492. <A NAME="set_switch_in_cb">
  493. <H4>st_set_switch_in_cb()</H4>
  494. </A>
  495. <A NAME="set_switch_out_cb">
  496. <H4>st_set_switch_out_cb()</H4>
  497. </A>
  498. Set the optional callback function for thread switches.
  499. <P>
  500. <H5>Syntax</H5>
  501. <PRE>
  502. #include &lt;st.h&gt;
  503. st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb);
  504. st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb);
  505. </PRE>
  506. <P>
  507. <H5>Parameters</H5>
  508. <TT>st_set_switch_in_cb()</TT> and <TT>st_set_switch_out_cb()</TT> have the
  509. following parameter:<P>
  510. <TT>cb</TT><P>
  511. A function to be called when a thread is resumed and stopped respectively.<P>
  512. <H5>Returns</H5>
  513. The previous callback function pointer.
  514. <P>
  515. <H5>Description</H5>
  516. These functions set the callback for when a thread is resumed and stopped
  517. respectively. After being called any thread switch will call the callback.
  518. Use a <TT>NULL</TT> pointer to disable the callback (this is the default).
  519. Use <A HREF=#thread_self>st_thread_self()</A> or <A HREF=#priv>thread
  520. specific data</A> to differentiate between threads.<P>
  521. These functions can be called at any time.<P>
  522. This feature is available only when <TT>ST_SWITCH_CB</TT> is defined
  523. in <TT>&lt;st.h&gt;</TT>.
  524. <P>
  525. <HR>
  526. <P>
  527. <A NAME="threads">
  528. <H2>Thread Control and Identification</H2>
  529. </A>
  530. <P>
  531. These functions operate on a thread object of type
  532. <A HREF=#thread_t><B>st_thread_t</B></A>.
  533. <P>
  534. <DL>
  535. <DD><A HREF=#thread_create>st_thread_create()</A></DD>
  536. <DD><A HREF=#thread_exit>st_thread_exit()</A></DD>
  537. <DD><A HREF=#thread_join>st_thread_join()</A></DD>
  538. <DD><A HREF=#thread_self>st_thread_self()</A></DD>
  539. <DD><A HREF=#thread_interrupt>st_thread_interrupt()</A></DD>
  540. <DD><A HREF=#sleep>st_sleep()</A></DD>
  541. <DD><A HREF=#sleep>st_usleep()</A></DD>
  542. <DD><A HREF=#randomize_stacks>st_randomize_stacks()</A></DD>
  543. </DL>
  544. <P>
  545. <HR>
  546. <P>
  547. <A NAME="thread_create">
  548. <H4>st_thread_create()</H4>
  549. </A>
  550. Creates a new thread.
  551. <P>
  552. <H5>Syntax</H5>
  553. <PRE>
  554. #include &lt;st.h&gt;
  555. st_thread_t st_thread_create(void *(*start)(void *arg), void *arg,
  556. int joinable, int stack_size);
  557. </PRE>
  558. <P>
  559. <H5>Parameters</H5>
  560. <TT>st_thread_create()</TT> has the following parameters:<P>
  561. <TT>start</TT><P>
  562. A pointer to the thread's start function, which is called as the root of the
  563. new thread. Return from this function terminates a thread.<P>
  564. <TT>arg</TT><P>
  565. A pointer to the root function's only parameter.<P>
  566. <TT>joinable</TT><P>
  567. Specifies whether the thread is joinable or unjoinable. If this parameter
  568. is zero, the thread is unjoinable. Otherwise, it is joinable.
  569. See also <A HREF=#thread_join>st_thread_join()</A>.<P>
  570. <TT>stack_size</TT><P>
  571. Specifies your preference for the size of the stack, in bytes, associated
  572. with the newly created thread. If you pass zero in this parameter, the
  573. default stack size will be used. The default stack size is 128 KB on IA-64
  574. and 64 KB on all other platforms. On IA-64 only a half of <TT>stack_size</TT>
  575. bytes is used for the memory stack. The other half is used for the register
  576. stack backing store.
  577. <P>
  578. <H5>Returns</H5>
  579. Upon successful completion, a new thread identifier is returned (this
  580. identifier remains valid until the thread returns from its start function).
  581. Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set
  582. to indicate the error.
  583. <P>
  584. <H5>Description</H5>
  585. This function creates a new thread. Note that the total number of threads
  586. created by the application is limited by the amount of swap space available.
  587. Upon thread creation, <TT>stack_size</TT> bytes are reserved on the swap
  588. space. The stack pages are not actually used (valid) until touched by the
  589. application.
  590. <P>
  591. <HR>
  592. <P>
  593. <A NAME="thread_exit">
  594. <H4>st_thread_exit()</H4>
  595. </A>
  596. Terminates the calling thread.
  597. <P>
  598. <H5>Syntax</H5>
  599. <PRE>
  600. #include &lt;st.h&gt;
  601. void st_thread_exit(void *retval);
  602. </PRE>
  603. <P>
  604. <H5>Parameters</H5>
  605. <TT>st_thread_exit()</TT> has the following parameters:<P>
  606. <TT>retval</TT><P>
  607. If the thread is joinable, then the value <TT>retval</TT> may be retrieved
  608. by <A HREF=#thread_join>st_thread_join()</A>. If a thread returns from its
  609. start function, it acts as if it had called <TT>st_thread_exit()</TT> with
  610. <TT>retval</TT> as the value returned.
  611. <P>
  612. <H5>Returns</H5>
  613. Nothing.
  614. <P>
  615. <H5>Description</H5>
  616. This function terminates the calling thread. When a thread exits, per-thread
  617. private data is destroyed by invoking the destructor function for any
  618. non-<TT>NULL</TT> thread specific values associated with active keys (see
  619. <A HREF=#key_create>st_key_create()</A>). This function is implicitly called
  620. when a thread returns from its start function.<P>
  621. When the last thread terminates the process exits with a zero status value.
  622. <P>
  623. <HR>
  624. <P>
  625. <A NAME="thread_join">
  626. <H4>st_thread_join()</H4>
  627. </A>
  628. Blocks the calling thread until a specified thread terminates.
  629. <P>
  630. <H5>Syntax</H5>
  631. <PRE>
  632. #include &lt;st.h&gt;
  633. int st_thread_join(st_thread_t thread, void **retvalp);
  634. </PRE>
  635. <P>
  636. <H5>Parameters</H5>
  637. <TT>st_thread_join()</TT> has the following parameters:<P>
  638. <TT>thread</TT><P>
  639. A valid identifier for the thread that is to be joined.<P>
  640. <TT>retvalp</TT><P>
  641. If this parameter is not <TT>NULL</TT>, then the exit value of the
  642. <TT>thread</TT> will be placed in the location referenced by this parameter
  643. (see <A HREF=#thread_exit>st_thread_exit()</A>).
  644. <P>
  645. <H5>Returns</H5>
  646. Upon successful completion, a value of <TT>0</TT> is returned.
  647. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  648. to indicate the error:<P>
  649. <TABLE BORDER=0>
  650. <TR><TD><TT>EINVAL</TT></TD><TD>Target thread is unjoinable.</TD></TR>
  651. <TR><TD><TT>EINVAL</TT></TD><TD>Other thread already waits on the same
  652. joinable thread.</TD></TR>
  653. <TR><TD><TT>EDEADLK</TT></TD><TD>Target thread is the same as the
  654. calling thread.</TD></TR>
  655. <TR><TD><TT>EINTR</TT></TD><TD>Current thread was interrupted by
  656. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  657. </TABLE>
  658. <P>
  659. <H5>Description</H5>
  660. This function is used to synchronize the termination of a thread and possibly
  661. retrieve its exit value. Several threads cannot wait for the same thread
  662. to complete - one of the calling threads operates successfully, and the others
  663. terminate with the error. The calling thread is not blocked if the target
  664. thread has already terminated.
  665. <P>
  666. <HR>
  667. <P>
  668. <A NAME="thread_self">
  669. <H4>st_thread_self()</H4>
  670. </A>
  671. Identifies the calling thread.
  672. <P>
  673. <H5>Syntax</H5>
  674. <PRE>
  675. #include &lt;st.h&gt;
  676. st_thread_t st_thread_self(void);
  677. </PRE>
  678. <P>
  679. <H5>Parameters</H5>
  680. None.
  681. <P>
  682. <H5>Returns</H5>
  683. Always returns a valid reference to the calling thread - a self-identity.
  684. <P>
  685. <H5>Description</H5>
  686. This function identifies the calling thread. This is the same identifier
  687. that the creating thread obtains from
  688. <A HREF=#thread_create>st_thread_create()</A>.
  689. <P>
  690. <HR>
  691. <P>
  692. <A NAME="thread_interrupt">
  693. <H4>st_thread_interrupt()</H4>
  694. </A>
  695. Interrupts a target thread.
  696. <P>
  697. <H5>Syntax</H5>
  698. <PRE>
  699. #include &lt;st.h&gt;
  700. void st_thread_interrupt(st_thread_t thread);
  701. </PRE>
  702. <P>
  703. <H5>Parameters</H5>
  704. <TT>st_thread_interrupt()</TT> has the following parameters:<P>
  705. <TT>thread</TT><P>
  706. A valid identifier for the thread being interrupted.
  707. <P>
  708. <H5>Returns</H5>
  709. Nothing.
  710. <P>
  711. <H5>Description</H5>
  712. This function interrupts (unblocks) a target thread that is blocked in one
  713. of the <A HREF=#block>blocking functions</A>. A function that was interrupted
  714. returns an error and sets <TT>errno</TT> to <TT>EINTR</TT>. It is up to
  715. the target thread to act upon an interrupt (e.g., it may exit or just
  716. abort the current transaction).<P>
  717. <B>Note: </B> State Threads library functions are never interrupted by a
  718. caught signal. A blocking library function returns an error and sets
  719. <TT>errno</TT> to <TT>EINTR</TT> <I>only</I> if the current thread was
  720. interrupted via <TT>st_thread_interrupt()</TT>.
  721. <P>
  722. If a target thread is already runnable or running (e.g., it is a newly
  723. created thread or calling thread itself), this function will prevent it
  724. from subsequent blocking. In other words, the interrupt will be "delivered"
  725. only when a target thread is about to block.
  726. <P>
  727. <HR>
  728. <P>
  729. <A NAME="sleep">
  730. <H4>st_sleep(), st_usleep()</H4>
  731. </A>
  732. Suspends current thread for a specified amount of time.
  733. <P>
  734. <H5>Syntax</H5>
  735. <PRE>
  736. #include &lt;st.h&gt;
  737. int st_sleep(int secs);
  738. int st_usleep(st_utime_t usecs);
  739. </PRE>
  740. <P>
  741. <H5>Parameters</H5>
  742. <TT>st_sleep()</TT> has the following parameters:<P>
  743. <TT>secs</TT><P>
  744. The number of seconds you want the thread to sleep for.
  745. <P>
  746. <TT>st_usleep()</TT> has the following parameters:<P>
  747. <TT>usecs</TT><P>
  748. The number of microseconds you want the thread to sleep for. This parameter
  749. is a variable of type <A HREF=#utime_t><B>st_utime_t</B></A>.
  750. <P>
  751. <H5>Returns</H5>
  752. Upon successful completion, a value of <TT>0</TT> is returned.
  753. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  754. to indicate the error:<P>
  755. <TABLE BORDER=0>
  756. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  757. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  758. </TABLE>
  759. <P>
  760. <H5>Description</H5>
  761. These functions suspend the calling thread from execution for a specified
  762. number of seconds (<TT>st_sleep()</TT>) or microseconds (<TT>st_usleep()</TT>).
  763. <P>
  764. If zero is passed as a parameter to <tt>st_sleep()</tt>, or
  765. <tt>ST_UTIME_NO_WAIT</tt> (<tt>0</tt>) is passed to
  766. <tt>st_usleep()</tt>, the calling thread yields, thus potentially
  767. allowing another thread to run.
  768. <P>
  769. If <TT>-1</TT> is passed as a parameter to <tt>st_sleep()</tt>, or
  770. <tt>ST_UTIME_NO_TIMEOUT</tt> (<tt>-1</tt>) is passed to
  771. <tt>st_usleep()</tt>, the calling thread will be suspended permanently.
  772. It can be resumed again by interrupting it via <A
  773. HREF=#thread_interrupt>st_thread_interrupt()</A>.
  774. <P>
  775. <HR>
  776. <P>
  777. <A NAME="randomize_stacks">
  778. <H4>st_randomize_stacks()</H4>
  779. </A>
  780. Turns stack base address randomization on or off.
  781. <P>
  782. <H5>Syntax</H5>
  783. <PRE>
  784. #include &lt;st.h&gt;
  785. int st_randomize_stacks(int on);
  786. </PRE>
  787. <P>
  788. <H5>Parameters</H5>
  789. <TT>st_randomize_stacks()</TT> has the following parameters:<P>
  790. <TT>on</TT><P>
  791. If this parameter has a non-zero value, the State Threads library
  792. randomizes the base addresses of stacks allocated for threads created
  793. after this call. Otherwise new threads' stacks are typically page
  794. aligned.
  795. <P>
  796. <H5>Returns</H5>
  797. The previous state of stack randomization (a value of <TT>0</TT> if it
  798. was off and a non-zero value otherwise).
  799. <P>
  800. <H5>Description</H5>
  801. Randomizing state threads' stack bases may improve cache performance on
  802. some systems when large numbers of state threads all perform roughly the
  803. same work, as when they all start from the same root function. On many
  804. modern systems the performance increase is negligible. You should
  805. compare your application's performance with this feature on and off to
  806. see if you really need it.
  807. <P>
  808. When randomization is enabled, new stacks are allocated one page larger
  809. to accomodate the randomization.
  810. <P>
  811. This call affects only threads created afterward. It has no effect on
  812. existing threads.
  813. <P>
  814. <HR>
  815. <P>
  816. <A NAME="priv">
  817. <H2>Per-Thread Private Data</H2>
  818. </A>
  819. These functions allow to associate private data with each of the threads in
  820. a process.
  821. <P>
  822. <DL>
  823. <DD><A HREF=#key_create>st_key_create()</A></DD>
  824. <DD><A HREF=#key_getlimit>st_key_getlimit()</A></DD>
  825. <DD><A HREF=#thread_setspecific>st_thread_setspecific()</A></DD>
  826. <DD><A HREF=#thread_getspecific>st_thread_getspecific()</A></DD>
  827. </DL>
  828. <P>
  829. <HR>
  830. <P>
  831. <A NAME="key_create">
  832. <H4>st_key_create()</H4>
  833. </A>
  834. Creates a key (non-negative integer) that can be used by all
  835. threads in the process to get and set thread-specific data.
  836. <P>
  837. <H5>Syntax</H5>
  838. <PRE>
  839. #include &lt;st.h&gt;
  840. int st_key_create(int *keyp, void (*destructor)(void *));
  841. </PRE>
  842. <P>
  843. <H5>Parameters</H5>
  844. <TT>st_key_create()</TT> has the following parameters:<P>
  845. <TT>keyp</TT><P>
  846. The newly created key is returned in the memory pointed to by this parameter.
  847. The new key can be used with
  848. <A HREF=#thread_setspecific>st_thread_setspecific()</A> and
  849. <A HREF=#thread_getspecific>st_thread_getspecific()</A>.<P>
  850. <TT>destructor</TT><P>
  851. Specifies an optional destructor function for the private data associated
  852. with the key. This function can be specified as <TT>NULL</TT>.
  853. Upon thread exit (see <A HREF=#thread_exit>st_thread_exit()</A>), if a key
  854. has a non-<TT>NULL</TT> <TT>destructor</TT> and has a non-<TT>NULL</TT> value
  855. associated with that key, then the <TT>destructor</TT> function will be
  856. called with the associated value.
  857. <P>
  858. <H5>Returns</H5>
  859. Upon successful completion, a value of <TT>0</TT> is returned.
  860. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  861. to indicate the error:<P>
  862. <TABLE BORDER=0>
  863. <TR><TD><TT>EAGAIN</TT></TD><TD>The limit on the total number of keys per
  864. process has been exceeded (see <A HREF=#key_getlimit>st_key_getlimit()</A>).
  865. </TD></TR>
  866. </TABLE>
  867. <P>
  868. <H5>Description</H5>
  869. If this function is successful, every thread in the same process is capable
  870. of associating private data with the new key. After a new key is created, all
  871. active threads have the value <TT>NULL</TT> associated with that key.
  872. After a new thread is created, the value <TT>NULL</TT> is associated with
  873. all keys for that thread. If a non-<TT>NULL</TT> destructor function is
  874. registered with a new key, it will be called at one of two times, as long as
  875. the private data is not <TT>NULL</TT>:
  876. <UL>
  877. <LI>when replacement private data is set with
  878. <A HREF=#thread_setspecific>st_thread_setspecific()</A></LI>
  879. <LI>when a thread exits (see <A HREF=#thread_exit>st_thread_exit()</A>)</LI>
  880. </UL>
  881. <P>
  882. The key maintains independent data values for each binding thread. A thread
  883. can get access only to its own thread-specific data. There is no way to
  884. deallocate a private data key once it is allocated.
  885. <P>
  886. <HR>
  887. <P>
  888. <A NAME="key_getlimit">
  889. <H4>st_key_getlimit()</H4>
  890. </A>
  891. Returns the key limit.
  892. <P>
  893. <H5>Syntax</H5>
  894. <PRE>
  895. #include &lt;st.h&gt;
  896. int st_key_getlimit(void);
  897. </PRE>
  898. <P>
  899. <H5>Parameters</H5>
  900. None.
  901. <P>
  902. <H5>Returns</H5>
  903. The limit on the total number of keys per process.
  904. <P>
  905. <H5>Description</H5>
  906. This function can be used to obtain the limit on the total number of keys
  907. per process (see <A HREF=#key_create>st_key_create()</A>).
  908. <P>
  909. <HR>
  910. <P>
  911. <A NAME="thread_setspecific">
  912. <H4>st_thread_setspecific()</H4>
  913. </A>
  914. Sets per-thread private data.
  915. <P>
  916. <H5>Syntax</H5>
  917. <PRE>
  918. #include &lt;st.h&gt;
  919. int st_thread_setspecific(int key, void *value);
  920. </PRE>
  921. <P>
  922. <H5>Parameters</H5>
  923. <TT>st_thread_setspecific()</TT> has the following parameters:<P>
  924. <TT>key</TT><P>
  925. This parameter represents a key with which thread-specific data is associated.
  926. <P>
  927. <TT>value</TT><P>
  928. The per-thread private data, or more likely, a pointer to the data which is
  929. associated with <TT>key</TT>.
  930. <P>
  931. <H5>Returns</H5>
  932. Upon successful completion, a value of <TT>0</TT> is returned.
  933. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  934. to indicate the error:<P>
  935. <TABLE BORDER=0>
  936. <TR><TD><TT>EINVAL</TT></TD><TD>The specified <TT>key</TT> is invalid.</TD>
  937. </TR>
  938. </TABLE>
  939. <P>
  940. <H5>Description</H5>
  941. This function associates a thread-specific <TT>value</TT> with <TT>key</TT>.
  942. Different threads may bind different values to the same key.<P>
  943. If the thread already has non-<TT>NULL</TT> private data associated with
  944. <TT>key</TT>, and if the destructor function for that key is not
  945. <TT>NULL</TT>, this destructor function will be called before setting the
  946. new data value.
  947. <P>
  948. <HR>
  949. <P>
  950. <A NAME="thread_getspecific">
  951. <H4>st_thread_getspecific()</H4>
  952. </A>
  953. Retrieves the per-thread private data for the current thread.
  954. <P>
  955. <H5>Syntax</H5>
  956. <PRE>
  957. #include &lt;st.h&gt;
  958. void *st_thread_getspecific(int key);
  959. </PRE>
  960. <P>
  961. <H5>Parameters</H5>
  962. <TT>st_thread_getspecific()</TT> has the following parameters:<P>
  963. <TT>key</TT><P>
  964. This parameter represents a key with which thread-specific data is associated.
  965. <P>
  966. <H5>Returns</H5>
  967. The thread-specific data associated with <TT>key</TT>. If no data is
  968. associated with <TT>key</TT>, then <TT>NULL</TT> is returned.
  969. <P>
  970. <H5>Description</H5>
  971. This function returns the calling thread's value that is bound to the
  972. specified <TT>key</TT> (see
  973. <A HREF=#thread_setspecific>st_thread_setspecific()</A>).
  974. <P>
  975. <HR>
  976. <P>
  977. <A NAME="sync">
  978. <H2>Synchronization</H2>
  979. </A>
  980. <P>
  981. These functions operate on <A HREF=#cond_t>condition variables</A>
  982. and <A HREF=#mutex_t>mutual exclusion locks</A> (mutexes).<P>
  983. Functions are provided to wait on a condition variable and to wake up
  984. (signal) threads that are waiting on the condition variable.
  985. <P>
  986. <DL>
  987. <DD><A HREF=#cond_new>st_cond_new()</A></DD>
  988. <DD><A HREF=#cond_destroy>st_cond_destroy()</A></DD>
  989. <DD><A HREF=#cond_wait>st_cond_wait()</A></DD>
  990. <DD><A HREF=#cond_timedwait>st_cond_timedwait()</A></DD>
  991. <DD><A HREF=#cond_signal>st_cond_signal()</A></DD>
  992. <DD><A HREF=#cond_broadcast>st_cond_broadcast()</A></DD>
  993. <P>
  994. <DD><A HREF=#mutex_new>st_mutex_new()</A></DD>
  995. <DD><A HREF=#mutex_destroy>st_mutex_destroy()</A></DD>
  996. <DD><A HREF=#mutex_lock>st_mutex_lock()</A></DD>
  997. <DD><A HREF=#mutex_trylock>st_mutex_trylock()</A></DD>
  998. <DD><A HREF=#mutex_unlock>st_mutex_unlock()</A></DD>
  999. </DL>
  1000. <P>
  1001. <HR>
  1002. <P>
  1003. <A NAME="cond_new">
  1004. <H4>st_cond_new()</H4>
  1005. </A>
  1006. Creates a new condition variable.
  1007. <P>
  1008. <H5>Syntax</H5>
  1009. <PRE>
  1010. #include &lt;st.h&gt;
  1011. st_cond_t st_cond_new(void);
  1012. </PRE>
  1013. <P>
  1014. <H5>Parameters</H5>
  1015. None.
  1016. <P>
  1017. <H5>Returns</H5>
  1018. Upon successful completion, a new condition variable identifier is returned.
  1019. Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set
  1020. to indicate the error.
  1021. <P>
  1022. <H5>Description</H5>
  1023. This function creates a new condition variable.
  1024. <P>
  1025. <HR>
  1026. <P>
  1027. <A NAME="cond_destroy">
  1028. <H4>st_cond_destroy()</H4>
  1029. </A>
  1030. Destroys a condition variable.
  1031. <P>
  1032. <H5>Syntax</H5>
  1033. <PRE>
  1034. #include &lt;st.h&gt;
  1035. int st_cond_destroy(st_cond_t cvar);
  1036. </PRE>
  1037. <P>
  1038. <H5>Parameters</H5>
  1039. <TT>st_cond_destroy()</TT> has the following parameters:<P>
  1040. <TT>cvar</TT><P>
  1041. An identifier of the condition variable object to be destroyed.
  1042. <P>
  1043. <H5>Returns</H5>
  1044. Upon successful completion, a value of <TT>0</TT> is returned.
  1045. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  1046. to indicate the error:<P>
  1047. <TABLE BORDER=0>
  1048. <TR><TD><TT>EBUSY</TT></TD><TD>The condition variable is currently being
  1049. used by one or more threads.</TD>
  1050. </TR>
  1051. </TABLE>
  1052. <P>
  1053. <H5>Description</H5>
  1054. This function destroys a condition variable. The caller is responsible for
  1055. ensuring that the condition variable is no longer in use.
  1056. <P>
  1057. <HR>
  1058. <P>
  1059. <A NAME="cond_wait">
  1060. <H4>st_cond_wait()</H4>
  1061. </A>
  1062. Waits on a condition.
  1063. <P>
  1064. <H5>Syntax</H5>
  1065. <PRE>
  1066. #include &lt;st.h&gt;
  1067. int st_cond_wait(st_cond_t cvar);
  1068. </PRE>
  1069. <P>
  1070. <H5>Parameters</H5>
  1071. <TT>st_cond_wait()</TT> has the following parameters:<P>
  1072. <TT>cvar</TT><P>
  1073. The condition variable on which to wait.
  1074. <P>
  1075. <H5>Returns</H5>
  1076. Upon successful completion, a value of <TT>0</TT> is returned.
  1077. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  1078. to indicate the error:<P>
  1079. <TABLE BORDER=0>
  1080. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  1081. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  1082. </TABLE>
  1083. <P>
  1084. <H5>Description</H5>
  1085. This function is used to block on a condition variable. A return from this
  1086. function does not guarantee that the condition or event for which the caller
  1087. was waiting actually occurred. It is the responsibility of the caller
  1088. to recheck the condition wait predicate before proceeding.<P>
  1089. <B>Note:</B> The State Threads library scheduling guarantees that the
  1090. condition cannot change between the checking and blocking, therefore there
  1091. is no need for mutex protection. You must not call any
  1092. <A HREF=#block>blocking functions</A> between the condition checking and
  1093. the <TT>st_cond_wait()</TT> call.
  1094. <P>
  1095. <HR>
  1096. <P>
  1097. <A NAME="cond_timedwait">
  1098. <H4>st_cond_timedwait()</H4>
  1099. </A>
  1100. Waits on a condition.
  1101. <P>
  1102. <H5>Syntax</H5>
  1103. <PRE>
  1104. #include &lt;st.h&gt;
  1105. int st_cond_timedwait(st_cond_t cvar, st_utime_t timeout);
  1106. </PRE>
  1107. <P>
  1108. <H5>Parameters</H5>
  1109. <TT>st_cond_timedwait()</TT> has the following parameters:<P>
  1110. <TT>cvar</TT><P>
  1111. The condition variable on which to wait.<P>
  1112. <TT>timeout</TT><P>
  1113. If the number of microseconds specified by this parameter passes before the
  1114. waiting thread is signalled, an error is returned. This parameter is a
  1115. variable of type <A HREF=#utime_t><B>st_utime_t</B></A>. Note that this
  1116. time value is a <I>time delta</I>; it is not an <I>absolute time</I>.
  1117. Also note that timeouts are measured <A HREF="notes.html#timeouts">since
  1118. the last context switch</A>.
  1119. <P>
  1120. <H5>Returns</H5>
  1121. Upon successful completion, a value of <TT>0</TT> is returned.
  1122. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  1123. to indicate the error:<P>
  1124. <TABLE BORDER=0>
  1125. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  1126. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  1127. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred before the thread was
  1128. awakened by <A HREF=#cond_signal>st_cond_signal()</A> or
  1129. <A HREF=#cond_broadcast>st_cond_broadcast()</A>.</TD></TR>
  1130. </TABLE>
  1131. <P>
  1132. <H5>Description</H5>
  1133. This function works the same way as <A HREF=#cond_wait>st_cond_wait()</A>,
  1134. except that an error is returned if the number of microseconds specified by
  1135. <TT>timeout</TT> passes before the waiting thread is signalled.
  1136. <P>
  1137. <HR>
  1138. <P>
  1139. <A NAME="cond_signal">
  1140. <H4>st_cond_signal()</H4>
  1141. </A>
  1142. Unblocks a thread waiting on a condition variable.
  1143. <P>
  1144. <H5>Syntax</H5>
  1145. <PRE>
  1146. #include &lt;st.h&gt;
  1147. int st_cond_signal(st_cond_t cvar);
  1148. </PRE>
  1149. <P>
  1150. <H5>Parameters</H5>
  1151. <TT>st_cond_signal()</TT> has the following parameters:<P>
  1152. <TT>cvar</TT><P>
  1153. The condition variable to signal.
  1154. <P>
  1155. <H5>Returns</H5>
  1156. Always zero.
  1157. <P>
  1158. <H5>Description</H5>
  1159. This function unblocks (signals) one of the threads that are blocked on
  1160. <TT>cvar</TT> at the time of the call. If no thread is waiting on the
  1161. condition variable, the signal operation is a no-op.
  1162. <P>
  1163. <HR>
  1164. <P>
  1165. <A NAME="cond_broadcast">
  1166. <H4>st_cond_broadcast()</H4>
  1167. </A>
  1168. Unblocks all threads waiting on a condition variable.
  1169. <P>
  1170. <H5>Syntax</H5>
  1171. <PRE>
  1172. #include &lt;st.h&gt;
  1173. int st_cond_broadcast(st_cond_t cvar);
  1174. </PRE>
  1175. <P>
  1176. <H5>Parameters</H5>
  1177. <TT>st_cond_broadcast()</TT> has the following parameters:<P>
  1178. <TT>cvar</TT><P>
  1179. The condition variable to broadcast.
  1180. <P>
  1181. <H5>Returns</H5>
  1182. Always zero.
  1183. <P>
  1184. <H5>Description</H5>
  1185. This function unblocks all threads blocked on the specified condition
  1186. variable at the time of the call. If no threads are waiting, this operation
  1187. is a no-op.
  1188. <P>
  1189. <HR>
  1190. <P>
  1191. <A NAME="mutex_new">
  1192. <H4>st_mutex_new()</H4>
  1193. </A>
  1194. Creates a new mutual exclusion lock (mutex).
  1195. <P>
  1196. <H5>Syntax</H5>
  1197. <PRE>
  1198. #include &lt;st.h&gt;
  1199. st_mutex_t st_mutex_new(void);
  1200. </PRE>
  1201. <P>
  1202. <H5>Parameters</H5>
  1203. None.
  1204. <P>
  1205. <H5>Returns</H5>
  1206. Upon successful completion, a new mutex identifier is returned.
  1207. Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set to
  1208. indicate the error.
  1209. <P>
  1210. <H5>Description</H5>
  1211. This function creates a new opaque mutual exclusion lock (see
  1212. <A HREF=#mutex_t><B>st_mutex_t</B></A>).
  1213. <P>
  1214. <HR>
  1215. <P>
  1216. <A NAME="mutex_destroy">
  1217. <H4>st_mutex_destroy()</H4>
  1218. </A>
  1219. Destroys a specified mutex object.
  1220. <P>
  1221. <H5>Syntax</H5>
  1222. <PRE>
  1223. #include &lt;st.h&gt;
  1224. int st_mutex_destroy(st_mutex_t lock);
  1225. </PRE>
  1226. <P>
  1227. <H5>Parameters</H5>
  1228. <TT>st_mutex_destroy()</TT> has the following parameters:<P>
  1229. <TT>lock</TT><P>
  1230. An identifier of the mutex object to be destroyed.
  1231. <P>
  1232. <H5>Returns</H5>
  1233. Upon successful completion, a value of <TT>0</TT> is returned.
  1234. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  1235. to indicate the error:<P>
  1236. <TABLE BORDER=0>
  1237. <TR><TD><TT>EBUSY</TT></TD><TD>The mutex is currently being used by
  1238. other threads.</TD>
  1239. </TR>
  1240. </TABLE>
  1241. <P>
  1242. <H5>Description</H5>
  1243. This function destroys a mutex. The caller is responsible for ensuring
  1244. that the mutex is no longer in use.
  1245. <P>
  1246. <HR>
  1247. <P>
  1248. <A NAME="mutex_lock">
  1249. <H4>st_mutex_lock()</H4>
  1250. </A>
  1251. Locks a specified mutex object.
  1252. <P>
  1253. <H5>Syntax</H5>
  1254. <PRE>
  1255. #include &lt;st.h&gt;
  1256. int st_mutex_lock(st_mutex_t lock);
  1257. </PRE>
  1258. <P>
  1259. <H5>Parameters</H5>
  1260. <TT>st_mutex_lock()</TT> has the following parameters:<P>
  1261. <TT>lock</TT><P>
  1262. An identifier of the mutex object to be locked.
  1263. <P>
  1264. <H5>Returns</H5>
  1265. Upon successful completion, a value of <TT>0</TT> is returned.
  1266. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  1267. to indicate the error:<P>
  1268. <TABLE BORDER=0>
  1269. <TR><TD><TT>EDEADLK</TT></TD><TD>The current thread already owns the mutex.
  1270. </TD></TR>
  1271. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  1272. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  1273. </TABLE>
  1274. <P>
  1275. <H5>Description</H5>
  1276. A thread that calls this function will block until it can gain exclusive
  1277. ownership of a mutex, and retains ownership until it calls
  1278. <A HREF=#mutex_unlock>st_mutex_unlock()</A>.
  1279. <P>
  1280. <HR>
  1281. <P>
  1282. <A NAME="mutex_trylock">
  1283. <H4>st_mutex_trylock()</H4>
  1284. </A>
  1285. Attempts to acquire a mutex.
  1286. <P>
  1287. <H5>Syntax</H5>
  1288. <PRE>
  1289. #include &lt;st.h&gt;
  1290. int st_mutex_trylock(st_mutex_t lock);
  1291. </PRE>
  1292. <P>
  1293. <H5>Parameters</H5>
  1294. <TT>st_mutex_trylock()</TT> has the following parameters:<P>
  1295. <TT>lock</TT><P>
  1296. An identifier of the mutex object to be locked.
  1297. <P>
  1298. <H5>Returns</H5>
  1299. Upon successful completion, a value of <TT>0</TT> is returned.
  1300. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  1301. to indicate the error:<P>
  1302. <TABLE BORDER=0>
  1303. <TR><TD><TT>EBUSY</TT></TD><TD>The mutex is currently held by another
  1304. thread.</TD></TR>
  1305. </TABLE>
  1306. <P>
  1307. <H5>Description</H5>
  1308. This function attempts to acquire a mutex. If the mutex object is locked
  1309. (by any thread, including the current thread), the call returns immediately
  1310. with an error.
  1311. <P>
  1312. <HR>
  1313. <P>
  1314. <A NAME="mutex_unlock">
  1315. <H4>st_mutex_unlock()</H4>
  1316. </A>
  1317. Releases a specified mutex object.
  1318. <P>
  1319. <H5>Syntax</H5>
  1320. <PRE>
  1321. #include &lt;st.h&gt;
  1322. int st_mutex_unlock(st_mutex_t lock);
  1323. </PRE>
  1324. <P>
  1325. <H5>Parameters</H5>
  1326. <TT>st_mutex_unlock()</TT> has the following parameters:<P>
  1327. <TT>lock</TT><P>
  1328. An identifier of the mutex object to be unlocked.
  1329. <P>
  1330. <H5>Returns</H5>
  1331. Upon successful completion, a value of <TT>0</TT> is returned.
  1332. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  1333. to indicate the error:<P>
  1334. <TABLE BORDER=0>
  1335. <TR><TD><TT>EPERM</TT></TD><TD>The current thread does not own the mutex.
  1336. </TD></TR>
  1337. </TABLE>
  1338. <P>
  1339. <H5>Description</H5>
  1340. This function releases a specified mutex object previously acquired by
  1341. <A HREF=#mutex_lock>st_mutex_lock()</A> or
  1342. <A HREF=#mutex_trylock>st_mutex_trylock()</A>. Only the thread that locked
  1343. a mutex should unlock it.
  1344. <P>
  1345. <HR>
  1346. <P>
  1347. <A NAME="timing">
  1348. <H2>Timing</H2>
  1349. </A>
  1350. <P>
  1351. <DL>
  1352. <DD><A HREF=#utime>st_utime()</A></DD>
  1353. <DD><A HREF=#set_utime_function>st_set_utime_function()</A></DD>
  1354. <DD><A HREF=#timecache_set>st_timecache_set()</A></DD>
  1355. <DD><A HREF=#time>st_time()</A></DD>
  1356. </DL>
  1357. <P>
  1358. <HR>
  1359. <P>
  1360. <A NAME="utime">
  1361. <H4>st_utime()</H4>
  1362. </A>
  1363. Returns current high-resolution time.
  1364. <P>
  1365. <H5>Syntax</H5>
  1366. <PRE>
  1367. #include &lt;st.h&gt;
  1368. st_utime_t st_utime(void);
  1369. </PRE>
  1370. <P>
  1371. <H5>Parameters</H5>
  1372. None.
  1373. <P>
  1374. <H5>Returns</H5>
  1375. Current high-resolution time value of type
  1376. <A HREF=#utime_t><B>st_utime_t</B></A>.
  1377. <P>
  1378. <H5>Description</H5>
  1379. This function returns the current high-resolution time. Time is
  1380. expressed as microseconds since some arbitrary time in the past. It is
  1381. not correlated in any way to the time of day. See also <A
  1382. HREF=#utime_t><B>st_utime_t</B></A> and <A
  1383. HREF="#time"><B>st_time()</B></A>.
  1384. <P>
  1385. <HR>
  1386. <P>
  1387. <A NAME="set_utime_function">
  1388. <H4>st_set_utime_function()</H4>
  1389. </A>
  1390. Set high-resolution time function.
  1391. <P>
  1392. <H5>Syntax</H5>
  1393. <PRE>
  1394. #include &lt;st.h&gt;
  1395. int st_set_utime_function(st_utime_t (*func)(void));
  1396. </PRE>
  1397. <P>
  1398. <H5>Parameters</H5>
  1399. <TT>st_set_utime_function()</TT> has the following parameters:<P>
  1400. <TT>func</TT><P>
  1401. This function will be called to get high-resolution time instead of the
  1402. default <A HREF=#utime>st_utime()</A> function. It must return
  1403. number of microseconds since some arbitrary time in the past.
  1404. <P>
  1405. <H5>Returns</H5>
  1406. Upon successful completion, a value of <TT>0</TT> is returned.
  1407. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  1408. to <TT>EINVAL</TT> to indicate the error.
  1409. <P>
  1410. <H5>Description</H5>
  1411. This function may be called to replace the default implementation of the
  1412. <A HREF=#utime>st_utime()</A> function. It must be called before the ST
  1413. library has been initialized (see <A HREF=#st_init>st_init()</A>).
  1414. The user-provided function <TT>func</TT> will be invoked whenever
  1415. <A HREF=#utime>st_utime()</A> is called to obtain current high-resolution time.
  1416. Replacing default implementation may be useful, for example, for taking
  1417. advantage of high performance CPU cycle counters.
  1418. <P>
  1419. <HR>
  1420. <P>
  1421. <A NAME="timecache_set">
  1422. <H4>st_timecache_set()</H4>
  1423. </A>
  1424. Turns the time caching on or off.
  1425. <P>
  1426. <H5>Syntax</H5>
  1427. <PRE>
  1428. #include &lt;st.h&gt;
  1429. int st_timecache_set(int on);
  1430. </PRE>
  1431. <P>
  1432. <H5>Parameters</H5>
  1433. <TT>st_timecache_set()</TT> has the following parameters:<P>
  1434. <TT>on</TT><P>
  1435. If this parameter has a non-zero value, the time caching is turned on
  1436. (enabled). Otherwise, the time caching is turned off (disabled).
  1437. By default time caching is disabled.
  1438. <P>
  1439. <H5>Returns</H5>
  1440. The previous state of time caching (a value of <TT>0</TT> if it was off and
  1441. a value of <TT>1</TT> otherwise).
  1442. <P>
  1443. <H5>Description</H5>
  1444. The State Threads library has the ability to "cache" the time value that is
  1445. reported by the <TT>time(2)</TT> system call. If the time caching is enabled
  1446. by calling this function with a non-zero argument, then the result value
  1447. of <TT>time(2)</TT> will be stored and updated at most once per second. The
  1448. cached time can be retrieved by <A HREF=#time>st_time()</A>.
  1449. By default time caching is disabled.
  1450. You may enable or disable time caching at any time but generally
  1451. you enable it once (if desired) during program initialization.<P>
  1452. <B>Note:</B> There are some pathological cases (e.g., very heavy loads during
  1453. application benchmarking) when a single thread runs for a long time without
  1454. giving up control and the cached time value is not updated properly. If you
  1455. <I>always</I> need "real-time" time values, don't enable the time caching.
  1456. <P>
  1457. <HR>
  1458. <P>
  1459. <A NAME="time">
  1460. <H4>st_time()</H4>
  1461. </A>
  1462. Returns the value of time in seconds since 00:00:00 UTC, January 1, 1970.
  1463. <P>
  1464. <H5>Syntax</H5>
  1465. <PRE>
  1466. #include &lt;st.h&gt;
  1467. time_t st_time(void);
  1468. </PRE>
  1469. <P>
  1470. <H5>Parameters</H5>
  1471. None.
  1472. <P>
  1473. <H5>Returns</H5>
  1474. The value of time in seconds since 00:00:00 UTC, January 1, 1970 as reported
  1475. by the <TT>time(2)</TT> system call.
  1476. <P>
  1477. <H5>Description</H5>
  1478. If the time caching was enabled by
  1479. <A HREF=#timecache_set>st_timecache_set()</A>, then this function returns
  1480. the cached result. Otherwise, it just calls <TT>time(2)</TT>.
  1481. <P>
  1482. <HR>
  1483. <P>
  1484. <A NAME="io">
  1485. <H2>I/O Functions</H2>
  1486. </A>
  1487. <P>
  1488. Most State Threads library I/O functions look like corresponding C library
  1489. functions with two exceptions:
  1490. <UL>
  1491. <LI>They operate on file descriptor objects of type
  1492. <A HREF=#netfd_t><B>st_netfd_t</B></A>.</LI>
  1493. <LI>They take an additional argument of type
  1494. <A HREF=#utime_t><B>st_utime_t</B></A> which represents an <I>inactivity
  1495. timeout</I>: if no I/O is possible during this amount of time, I/O functions
  1496. return an error code and set <TT>errno</TT> to <TT>ETIME</TT>.
  1497. The boundary values <tt>ST_UTIME_NO_WAIT</tt> (<TT>0</TT>) and
  1498. <tt>ST_UTIME_NO_TIMEOUT</tt> (<TT>-1</TT>) for this argument indicate
  1499. that the thread should wait no time (function returns immediately) or
  1500. wait forever (never time out), respectively.
  1501. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  1502. last context switch</A>.
  1503. </LI>
  1504. </UL>
  1505. <P>
  1506. <DL>
  1507. <DD><A HREF=#netfd_open>st_netfd_open()</A></DD>
  1508. <DD><A HREF=#netfd_open_socket>st_netfd_open_socket()</A></DD>
  1509. <DD><A HREF=#netfd_free>st_netfd_free()</A></DD>
  1510. <DD><A HREF=#netfd_close>st_netfd_close()</A></DD>
  1511. <DD><A HREF=#netfd_fileno>st_netfd_fileno()</A></DD>
  1512. <DD><A HREF=#netfd_setspecific>st_netfd_setspecific()</A></DD>
  1513. <DD><A HREF=#netfd_getspecific>st_netfd_getspecific()</A></DD>
  1514. <DD><A HREF=#netfd_serialize_accept>st_netfd_serialize_accept()</A></DD>
  1515. <DD><A HREF=#netfd_poll>st_netfd_poll()</A></DD>
  1516. <P>
  1517. <DD><A HREF=#accept>st_accept()</A></DD>
  1518. <DD><A HREF=#connect>st_connect()</A></DD>
  1519. <DD><A HREF=#read>st_read()</A></DD>
  1520. <DD><A HREF=#read_fully>st_read_fully()</A></DD>
  1521. <DD><A HREF=#read_resid>st_read_resid()</A></DD>
  1522. <DD><A HREF=#readv>st_readv()</A></DD>
  1523. <DD><A HREF=#readv_resid>st_read_resid()</A></DD>
  1524. <DD><A HREF=#write>st_write()</A></DD>
  1525. <DD><A HREF=#write_resid>st_write_resid()</A></DD>
  1526. <DD><A HREF=#writev>st_writev()</A></DD>
  1527. <DD><A HREF=#writev_resid>st_writev_resid()</A></DD>
  1528. <DD><A HREF=#recvfrom>st_recvfrom()</A></DD>
  1529. <DD><A HREF=#sendto>st_sendto()</A></DD>
  1530. <DD><A HREF=#recvmsg>st_recvmsg()</A></DD>
  1531. <DD><A HREF=#sendmsg>st_sendmsg()</A></DD>
  1532. <DD><A HREF=#open>st_open()</A></DD>
  1533. <DD><A HREF=#poll>st_poll()</A></DD>
  1534. </DL>
  1535. <P>
  1536. <HR>
  1537. <P>
  1538. <A NAME="netfd_open">
  1539. <H4>st_netfd_open()</H4>
  1540. </A>
  1541. Creates a new file descriptor object.
  1542. <P>
  1543. <H5>Syntax</H5>
  1544. <PRE>
  1545. #include &lt;st.h&gt;
  1546. st_netfd_t st_netfd_open(int osfd);
  1547. </PRE>
  1548. <P>
  1549. <H5>Parameters</H5>
  1550. <TT>st_netfd_open()</TT> has the following parameters:<P>
  1551. <TT>osfd</TT><P>
  1552. Any open OS file descriptor; can be obtained from calls to
  1553. functions including, but not restricted to, <TT>pipe(2), socket(3),
  1554. socketpair(3), fcntl(2), dup(2),</TT> etc.
  1555. <P>
  1556. <H5>Returns</H5>
  1557. Upon successful completion, a new file descriptor object identifier is
  1558. returned. Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set
  1559. to indicate the error.
  1560. <P>
  1561. <H5>Description</H5>
  1562. This function creates a new file descriptor object of type
  1563. <A HREF=#netfd_t><B>st_netfd_t</B></A>.<P>
  1564. <B>Note:</B> Among other things, this function sets a non-blocking
  1565. flag on the underlying OS file descriptor. You should not modify this
  1566. flag directly. Also, once an <A HREF=#netfd_t><B>st_netfd_t</B></A>
  1567. has been created with a given file descriptor, you should avoid
  1568. passing that descriptor to normal I/O or stdio functions. Since the
  1569. O_NONBLOCK flag is shared across <TT>dup(2)</TT>, this applies to
  1570. <TT>dup()</TT>'ed file descriptors as well - for instance, if you pass
  1571. standard output or standard input to <TT>st_netfd_open()</TT>, then
  1572. you should use <A HREF=#write>st_write()</A> instead of <TT>write</TT>
  1573. or <TT>fprintf</TT> when writing to standard error as well - since all
  1574. three descriptors could point to the same terminal. If necessary, you
  1575. can still use <TT>write</TT> directly if you remember to check
  1576. <TT>errno</TT> for <TT>EAGAIN</TT>, but <TT>fprintf</TT> and other
  1577. stdio functions should be avoided completely because, at least on
  1578. Linux, the stdio library cannot be made to work reliably with
  1579. non-blocking files. (This only applies to file descriptors which are
  1580. passed to <TT>st_netfd_open()</TT> or <A
  1581. HREF=#netfd_open_socket>st_netfd_open_socket()</A>, or which are
  1582. related to such descriptors through <TT>dup()</TT>; other file
  1583. descriptors are untouched by State Threads.)
  1584. <P>
  1585. <HR>
  1586. <P>
  1587. <A NAME="netfd_open_socket">
  1588. <H4>st_netfd_open_socket()</H4>
  1589. </A>
  1590. Creates a new file descriptor object from a socket.
  1591. <P>
  1592. <H5>Syntax</H5>
  1593. <PRE>
  1594. #include &lt;st.h&gt;
  1595. st_netfd_t st_netfd_open_socket(int osfd);
  1596. </PRE>
  1597. <P>
  1598. <H5>Parameters</H5>
  1599. <TT>st_netfd_open_socket()</TT> has the following parameters:<P>
  1600. <TT>osfd</TT><P>
  1601. An open OS file descriptor which is a socket initially obtained from a<TT>
  1602. socket(3) or socketpair(3)</TT> call.
  1603. <P>
  1604. <H5>Returns</H5>
  1605. Upon successful completion, a new file descriptor object identifier is
  1606. returned. Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set
  1607. to indicate the error.
  1608. <P>
  1609. <H5>Description</H5>
  1610. This function creates a new file descriptor object of type
  1611. <A HREF=#netfd_t><B>st_netfd_t</B></A> which represents an open end
  1612. point of network communication.<P>
  1613. Unlike the <A HREF=#netfd_open>st_netfd_open()</A> function which may be used
  1614. on OS file descriptors of any origin, <TT>st_netfd_open_socket()</TT> must
  1615. be used only on sockets. It is slightly more efficient than
  1616. <A HREF=#netfd_open>st_netfd_open()</A>.<P>
  1617. <B>Note:</B> Among other things, this function sets a non-blocking flag
  1618. on the underlying OS socket. You should not modify this flag directly.
  1619. See <A HREF=#netfd_open>st_netfd_open()</A>.
  1620. <P>
  1621. <HR>
  1622. <P>
  1623. <A NAME="netfd_free">
  1624. <H4>st_netfd_free()</H4>
  1625. </A>
  1626. Frees a file descriptor object without closing the underlying OS file
  1627. descriptor.
  1628. <P>
  1629. <H5>Syntax</H5>
  1630. <PRE>
  1631. #include &lt;st.h&gt;
  1632. void st_netfd_free(st_netfd_t fd);
  1633. </PRE>
  1634. <P>
  1635. <H5>Parameters</H5>
  1636. <TT>st_netfd_free()</TT> has the following parameters:<P>
  1637. <TT>fd</TT><P>
  1638. A file descriptor object identifier (see
  1639. <A HREF=#netfd_t><B>st_netfd_t</B></A>).
  1640. <P>
  1641. <H5>Returns</H5>
  1642. Nothing.
  1643. <P>
  1644. <H5>Description</H5>
  1645. This function frees the memory and other resources identified by the
  1646. <TT>fd</TT> parameter without closing the underlying OS file descriptor.
  1647. Any non-<TT>NULL</TT> descriptor-specific data is destroyed by invoking
  1648. the specified destructor function (see <A
  1649. HREF=#netfd_setspecific>st_netfd_setspecific()</A>).<P> A thread should
  1650. not free file descriptor objects that are in use by other threads
  1651. because it may lead to unpredictable results (e.g., a freed file
  1652. descriptor may be reused without other threads knowing that).
  1653. <P>
  1654. <HR>
  1655. <P>
  1656. <A NAME="netfd_close">
  1657. <H4>st_netfd_close()</H4>
  1658. </A>
  1659. Closes a file descriptor.
  1660. <P>
  1661. <H5>Syntax</H5>
  1662. <PRE>
  1663. #include &lt;st.h&gt;
  1664. int st_netfd_close(st_netfd_t fd);
  1665. </PRE>
  1666. <P>
  1667. <H5>Parameters</H5>
  1668. <TT>st_netfd_close()</TT> has the following parameters:<P>
  1669. <TT>fd</TT><P>
  1670. A file descriptor object identifier (see
  1671. <A HREF=#netfd_t><B>st_netfd_t</B></A>).
  1672. <P>
  1673. <H5>Returns</H5>
  1674. Upon successful completion, a value of <TT>0</TT> is returned.
  1675. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  1676. to indicate the error.
  1677. <P>
  1678. <H5>Description</H5>
  1679. This function closes the underlying OS file descriptor, frees the memory and
  1680. other resources identified by the <TT>fd</TT> parameter. Any non-<TT>NULL</TT>
  1681. descriptor-specific data is destroyed by invoking the specified destructor
  1682. function (see <A HREF=#netfd_setspecific>st_netfd_setspecific()</A>).<P>
  1683. A thread should not close file descriptor objects that are in use by other
  1684. threads because it may lead to unpredictable results (e.g., a closed
  1685. file descriptor may be reused without other threads knowing that).
  1686. <P>
  1687. <HR>
  1688. <P>
  1689. <A NAME="netfd_fileno">
  1690. <H4>st_netfd_fileno()</H4>
  1691. </A>
  1692. Returns an underlying OS file descriptor.
  1693. <P>
  1694. <H5>Syntax</H5>
  1695. <PRE>
  1696. #include &lt;st.h&gt;
  1697. int st_netfd_fileno(st_netfd_t fd);
  1698. </PRE>
  1699. <P>
  1700. <H5>Parameters</H5>
  1701. <TT>st_netfd_fileno()</TT> has the following parameters:<P>
  1702. <TT>fd</TT><P>
  1703. A file descriptor object identifier (see
  1704. <A HREF=#netfd_t><B>st_netfd_t</B></A>).
  1705. <P>
  1706. <H5>Returns</H5>
  1707. An underlying OS file descriptor.
  1708. <P>
  1709. <H5>Description</H5>
  1710. This function returns the integer OS file descriptor associated with the named
  1711. file descriptor object.
  1712. <P>
  1713. <HR>
  1714. <P>
  1715. <A NAME="netfd_setspecific">
  1716. <H4>st_netfd_setspecific()</H4>
  1717. </A>
  1718. Sets per-descriptor private data.
  1719. <P>
  1720. <H5>Syntax</H5>
  1721. <PRE>
  1722. #include &lt;st.h&gt;
  1723. void st_netfd_setspecific(st_netfd_t fd, void *value,
  1724. void (*destructor)(void *));
  1725. </PRE>
  1726. <P>
  1727. <H5>Parameters</H5>
  1728. <TT>st_netfd_setspecific()</TT> has the following parameters:<P>
  1729. <TT>fd</TT><P>
  1730. A valid file descriptor object identifier (see
  1731. <A HREF=#netfd_t><B>st_netfd_t</B></A>).
  1732. <P>
  1733. <TT>value</TT><P>
  1734. The per-descriptor private data, or more likely, a pointer to the data which
  1735. is being associated with the named file descriptor object.
  1736. <P>
  1737. <TT>destructor</TT><P>
  1738. Specifies an optional destructor function for the private data associated
  1739. with <TT>fd</TT>. This function can be specified as <TT>NULL</TT>.
  1740. If <TT>value</TT> is not <TT>NULL</TT>, then this destructor function will
  1741. be called with <TT>value</TT> as an argument upon freeing the file descriptor
  1742. object (see <A HREF=#netfd_free>st_netfd_free()</A> and
  1743. <A HREF=#netfd_close>st_netfd_close()</A>).
  1744. <P>
  1745. <H5>Returns</H5>
  1746. Nothing.
  1747. <P>
  1748. <H5>Description</H5>
  1749. This function allows to associate any data with the specified file
  1750. descriptor object (network connection). If a non-<TT>NULL</TT> destructor
  1751. function is registered, it will be called at one of two times, as long as
  1752. the associated data is not <TT>NULL</TT>:
  1753. <UL>
  1754. <LI>when private data is replaced by calling
  1755. <TT>st_netfd_setspecific()</TT> again
  1756. <LI>upon freeing the file descriptor object (see
  1757. <A HREF=#netfd_free>st_netfd_free()</A> and
  1758. <A HREF=#netfd_close>st_netfd_close()</A>)
  1759. </UL>
  1760. <P>
  1761. <HR>
  1762. <P>
  1763. <A NAME="netfd_getspecific">
  1764. <H4>st_netfd_getspecific()</H4>
  1765. </A>
  1766. Retrieves the per-descriptor private data.
  1767. <P>
  1768. <H5>Syntax</H5>
  1769. <PRE>
  1770. #include &lt;st.h&gt;
  1771. void *st_netfd_getspecific(st_netfd_t fd);
  1772. </PRE>
  1773. <P>
  1774. <H5>Parameters</H5>
  1775. <TT>st_netfd_getspecific()</TT> has the following parameters:<P>
  1776. <TT>fd</TT><P>
  1777. A valid file descriptor object identifier (see
  1778. <A HREF=#netfd_t><B>st_netfd_t</B></A>).
  1779. <P>
  1780. <H5>Returns</H5>
  1781. The data associated with the named file descriptor object. If no data is
  1782. associated with <TT>fd</TT>, then <TT>NULL</TT> is returned.
  1783. <P>
  1784. <H5>Description</H5>
  1785. This function allows to retrieve the data that was associated with the
  1786. specified file descriptor object (see
  1787. <A HREF=#netfd_setspecific>st_netfd_setspecific()</A>).
  1788. <P>
  1789. <HR>
  1790. <P>
  1791. <A NAME="netfd_serialize_accept">
  1792. <H4>st_netfd_serialize_accept()</H4>
  1793. </A>
  1794. Serializes all subsequent <TT>accept(3)</TT> calls on a specified file
  1795. descriptor object.
  1796. <P>
  1797. <H5>Syntax</H5>
  1798. <PRE>
  1799. #include &lt;st.h&gt;
  1800. int st_netfd_serialize_accept(st_netfd_t fd);
  1801. </PRE>
  1802. <P>
  1803. <H5>Parameters</H5>
  1804. <TT>st_netfd_serialize_accept()</TT> has the following parameters:<P>
  1805. <TT>fd</TT><P>
  1806. A file descriptor object identifier (see
  1807. <A HREF=#netfd_t><B>st_netfd_t</B></A>) which has been successfully created
  1808. from a valid listening socket by <A HREF=#netfd_open>st_netfd_open()</A> or
  1809. <A HREF=#netfd_open_socket>st_netfd_open_socket()</A>.
  1810. <P>
  1811. <H5>Returns</H5>
  1812. Upon successful completion, a value of <TT>0</TT> is returned.
  1813. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  1814. to indicate the error.
  1815. <P>
  1816. <H5>Description</H5>
  1817. On some platforms (e.g., Solaris 2.5 and possibly other SVR4 implementations)
  1818. <TT>accept(3)</TT> calls from different processes on
  1819. the same listening socket (see <TT>bind(3)</TT>, <TT>listen(3)</TT>) must be
  1820. serialized. This function causes all subsequent <TT>accept(3)</TT> calls
  1821. made by <A HREF=#accept>st_accept()</A> on the specified file descriptor
  1822. object to be serialized.
  1823. <P>
  1824. <TT>st_netfd_serialize_accept()</TT> must be called <I>before</I>
  1825. creating multiple server processes via <TT>fork(2)</TT>. If the application
  1826. does not create multiple processes to accept network connections on
  1827. the same listening socket, there is no need to call this function.
  1828. <P>
  1829. Deciding whether or not to serialize accepts is tricky. On some
  1830. platforms (IRIX, Linux) it's not needed at all and
  1831. <TT>st_netfd_serialize_accept()</TT> is a no-op. On other platforms
  1832. it depends on the version of the OS (Solaris 2.6 doesn't need it but
  1833. earlier versions do). Serializing accepts does incur a slight
  1834. performance penalty so you want to enable it only if necessary. Read
  1835. your system's manual pages for <tt>accept(2)</tt> and <tt>select(2)</tt>
  1836. to see if accept serialization is necessary on your system.
  1837. <P>
  1838. <TT>st_netfd_serialize_accept()</TT> allocates resources that are
  1839. freed upon freeing of the specified file descriptor object (see
  1840. <A HREF=#netfd_free>st_netfd_free()</A> and
  1841. <A HREF=#netfd_close>st_netfd_close()</A>).
  1842. <P>
  1843. <HR>
  1844. <P>
  1845. <A NAME="netfd_poll">
  1846. <H4>st_netfd_poll()</H4>
  1847. </A>
  1848. Waits for I/O on a single file descriptor object.
  1849. <P>
  1850. <H5>Syntax</H5>
  1851. <PRE>
  1852. #include &lt;st.h&gt;
  1853. int st_netfd_poll(st_netfd_t fd, int how, st_utime_t timeout);
  1854. </PRE>
  1855. <P>
  1856. <H5>Parameters</H5>
  1857. <TT>st_netfd_poll()</TT> has the following parameters:<P>
  1858. <TT>fd</TT><P>
  1859. A file descriptor object identifier (see
  1860. <A HREF=#netfd_t><B>st_netfd_t</B></A>).
  1861. <P>
  1862. <TT>how</TT><P>
  1863. Specifies I/O events of interest. This parameter can be constructed by
  1864. OR-ing any combination of the following event flags which are defined
  1865. in the <TT>poll.h</TT> header file:<P>
  1866. <TABLE BORDER=0>
  1867. <TR><TD><TT>POLLIN</TT></TD><TD><TT>fd</TT> is readable.</TD></TR>
  1868. <TR><TD><TT>POLLOUT</TT></TD><TD><TT>fd</TT> is is writable.</TD></TR>
  1869. <TR><TD><TT>POLLPRI</TT></TD><TD><TT>fd</TT> has an exception condition.</TD>
  1870. </TR>
  1871. </TABLE>
  1872. <P>
  1873. <TT>timeout</TT><P>
  1874. Amount of time in microseconds the call will block waiting for I/O
  1875. to become ready. This parameter is a variable of type
  1876. <A HREF=#utime_t><B>st_utime_t</B></A>. If this time expires without any
  1877. I/O becoming ready, <TT>st_netfd_poll()</TT> returns an error and sets
  1878. <TT>errno</TT> to <TT>ETIME</TT>.
  1879. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  1880. last context switch</A>.
  1881. <P>
  1882. <H5>Returns</H5>
  1883. If the named file descriptor object is ready for I/O within the specified
  1884. amount of time, a value of <TT>0</TT> is returned. Otherwise, a value
  1885. of <TT>-1</TT> is returned and <TT>errno</TT> is set to indicate the error:
  1886. <P>
  1887. <TABLE BORDER=0>
  1888. <TR><TD><TT>EBADF</TT></TD><TD>The underlying OS file descriptor is invalid.
  1889. </TD></TR>
  1890. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  1891. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  1892. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred without any I/O
  1893. becoming ready.</TD></TR>
  1894. </TABLE>
  1895. <P>
  1896. <H5>Description</H5>
  1897. This function returns as soon as I/O is ready on the named file
  1898. descriptor object or the specified amount of time expires. The
  1899. <TT>how</TT> parameter should be set to the I/O events (readable,
  1900. writable, exception, or some combination) that the caller is interested
  1901. in. If the value of <TT>timeout</TT> is <tt>ST_UTIME_NO_TIMEOUT</tt>
  1902. (<TT>-1</TT>), this function blocks until a requested I/O event occurs
  1903. or until the call is interrupted by <A
  1904. HREF=#thread_interrupt>st_thread_interrupt()</A>.<p>
  1905. Despite having an interface like <tt>poll(2)</tt>, this function uses
  1906. the same event notification mechanism as the rest of the library. For
  1907. instance if an alternative event nofication mechanism was set using <a
  1908. href=#set_eventsys>st_set_eventsys()</a>, this function uses that
  1909. mechanism to check for events.<p>
  1910. <b>Note: </b> if <TT>kqueue(2)</TT> is used as an alternative event
  1911. notification mechanism (see <A
  1912. HREF=#set_eventsys>st_set_eventsys()</A>), the <TT>POLLPRI</TT>
  1913. event flag is not supported and <TT>st_netfd_poll()</TT> will return an error
  1914. if it's set (<TT>errno</TT> will be set to <TT>EINVAL</TT>).
  1915. <P>
  1916. <HR>
  1917. <P>
  1918. <A NAME="accept">
  1919. <H4>st_accept()</H4>
  1920. </A>
  1921. Accepts a connection on a specified file descriptor object.
  1922. <P>
  1923. <H5>Syntax</H5>
  1924. <PRE>
  1925. #include &lt;st.h&gt;
  1926. st_netfd_t st_accept(st_netfd_t fd, struct sockaddr *addr, int *addrlen,
  1927. st_utime_t timeout);
  1928. </PRE>
  1929. <P>
  1930. <H5>Parameters</H5>
  1931. <TT>st_accept()</TT> has the following parameters:<P>
  1932. <TT>fd</TT><P>
  1933. A file descriptor object identifier (see
  1934. <A HREF=#netfd_t><B>st_netfd_t</B></A>) representing the rendezvous socket
  1935. on which the caller is willing to accept new connections. This object has been
  1936. created from a valid listening socket by
  1937. <A HREF=#netfd_open>st_netfd_open()</A> or
  1938. <A HREF=#netfd_open_socket>st_netfd_open_socket()</A>.<P>
  1939. <TT>addr</TT><P>
  1940. If this value is non-zero, it is a result parameter that is filled
  1941. in with the address of the connecting entity, as known to the communications
  1942. layer (see <TT>accept(3)</TT>).<P>
  1943. <TT>addrlen</TT><P>
  1944. This parameter should initially contain the amount of space pointed to by
  1945. <TT>addr</TT>; on return it will contain the actual length (in bytes) of the
  1946. address returned (see <TT>accept(3)</TT>).<P>
  1947. <TT>timeout</TT><P>
  1948. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
  1949. limit in microseconds for completion of the accept operation.
  1950. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  1951. last context switch</A>.
  1952. <P>
  1953. <H5>Returns</H5>
  1954. Upon successful completion, a new file descriptor object identifier
  1955. representing the newly accepted connection is returned. Otherwise,
  1956. <TT>NULL</TT> is returned and <TT>errno</TT> is set to indicate the error.
  1957. Possible <TT>errno</TT> values are the same as set by the <TT>accept(3)</TT>
  1958. call with two exceptions:<P>
  1959. <TABLE BORDER=0>
  1960. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  1961. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  1962. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no pending
  1963. connection was accepted.</TD></TR>
  1964. </TABLE>
  1965. <P>
  1966. <H5>Description</H5>
  1967. This function accepts the first connection from the queue of pending
  1968. connections and creates a new file descriptor object for the newly
  1969. accepted connection. The rendezvous socket can still be used to accept
  1970. more connections.<P>
  1971. <TT>st_accept()</TT> blocks the calling thread until either a new connection
  1972. is successfully accepted or an error occurs. If no pending connection can
  1973. be accepted before the time limit, this function returns <TT>NULL</TT>
  1974. and sets <TT>errno</TT> to <TT>ETIME</TT>.
  1975. <P>
  1976. <HR>
  1977. <P>
  1978. <A NAME="connect">
  1979. <H4>st_connect()</H4>
  1980. </A>
  1981. Initiates a connection on a specified file descriptor object.
  1982. <P>
  1983. <H5>Syntax</H5>
  1984. <PRE>
  1985. #include &lt;st.h&gt;
  1986. int st_connect(st_netfd_t fd, struct sockaddr *addr, int addrlen,
  1987. st_utime_t timeout);
  1988. </PRE>
  1989. <P>
  1990. <H5>Parameters</H5>
  1991. <TT>st_connect()</TT> has the following parameters:<P>
  1992. <TT>fd</TT><P>
  1993. A file descriptor object identifier (see
  1994. <A HREF=#netfd_t><B>st_netfd_t</B></A>) representing a socket.<P>
  1995. <TT>addr</TT><P>
  1996. A pointer to the address of the peer to which the socket is to be connected.
  1997. <P>
  1998. <TT>addrlen</TT><P>
  1999. This parameter specifies the amount of space pointed to by <TT>addr</TT>.
  2000. <P>
  2001. <TT>timeout</TT><P>
  2002. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
  2003. limit in microseconds for completion of the connect operation.
  2004. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2005. last context switch</A>.
  2006. <P>
  2007. <H5>Returns</H5>
  2008. Upon successful completion, a value of <TT>0</TT> is returned.
  2009. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  2010. to indicate the error. Possible <TT>errno</TT> values are the same as set
  2011. by the <TT>connect(3)</TT> call with two exceptions:<P>
  2012. <TABLE BORDER=0>
  2013. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2014. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2015. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and connection setup
  2016. was not completed.</TD></TR>
  2017. </TABLE>
  2018. <P>
  2019. <H5>Description</H5>
  2020. This function is usually invoked on a file descriptor object representing
  2021. a TCP socket. Upon completion it establishes a TCP connection to the peer.
  2022. If the underlying OS socket is not bound, it will be bound to an arbitrary
  2023. local address (see <TT>connect(3)</TT>).<P>
  2024. <TT>st_connect()</TT> blocks the calling thread until either the connection
  2025. is successfully established or an error occurs. If the connection setup
  2026. cannot complete before the specified time limit, this function fails with
  2027. <TT>errno</TT> set to <TT>ETIME</TT>.
  2028. <P>
  2029. <HR>
  2030. <P>
  2031. <A NAME="read">
  2032. <H4>st_read()</H4>
  2033. </A>
  2034. Reads data from a specified file descriptor object.
  2035. <P>
  2036. <H5>Syntax</H5>
  2037. <PRE>
  2038. #include &lt;st.h&gt;
  2039. ssize_t st_read(st_netfd_t fd, void *buf, size_t nbyte, st_utime_t timeout);
  2040. </PRE>
  2041. <P>
  2042. <H5>Parameters</H5>
  2043. <TT>st_read()</TT> has the following parameters:<P>
  2044. <TT>fd</TT><P>
  2045. A file descriptor object identifier (see
  2046. <A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
  2047. <TT>buf</TT><P>
  2048. A pointer to a buffer to hold the data read in. On output the buffer
  2049. contains the data.<P>
  2050. <TT>nbyte</TT><P>
  2051. The size of <TT>buf</TT> in bytes.<P>
  2052. <TT>timeout</TT><P>
  2053. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
  2054. limit in microseconds for completion of the read operation.
  2055. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2056. last context switch</A>.
  2057. <P>
  2058. <H5>Returns</H5>
  2059. On success a non-negative integer indicating the number of bytes actually
  2060. read is returned (a value of <TT>0</TT> means the network connection is
  2061. closed or end of file is reached). Otherwise, a value of <TT>-1</TT> is
  2062. returned and <TT>errno</TT> is set to indicate the error.
  2063. Possible <TT>errno</TT> values are the same as set by the <TT>read(2)</TT>
  2064. call with two exceptions:<P>
  2065. <TABLE BORDER=0>
  2066. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2067. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2068. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was read.
  2069. </TD></TR>
  2070. </TABLE>
  2071. <P>
  2072. <H5>Description</H5>
  2073. This function blocks the calling thread until it encounters an end-of-stream
  2074. indication, some positive number of bytes (but no more than <TT>nbyte</TT>
  2075. bytes) are read in, a timeout occurs, or an error occurs.
  2076. <P>
  2077. <HR>
  2078. <P>
  2079. <A NAME="read_fully">
  2080. <H4>st_read_fully()</H4>
  2081. </A>
  2082. Reads the specified amount of data in full from a file descriptor object.
  2083. <P>
  2084. <H5>Syntax</H5>
  2085. <PRE>
  2086. #include &lt;st.h&gt;
  2087. ssize_t st_read_fully(st_netfd_t fd, void *buf, size_t nbyte,
  2088. st_utime_t timeout);
  2089. </PRE>
  2090. <P>
  2091. <H5>Parameters</H5>
  2092. <TT>st_read_fully()</TT> has the following parameters:<P>
  2093. <TT>fd</TT><P>
  2094. A file descriptor object identifier (see
  2095. <A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
  2096. <TT>buf</TT><P>
  2097. A pointer to a buffer to hold the data read in. On output the buffer
  2098. contains the data.<P>
  2099. <TT>nbyte</TT><P>
  2100. The amount of data to be read in full (in bytes). It must not exceed the
  2101. size of <TT>buf</TT>.<P>
  2102. <TT>timeout</TT><P>
  2103. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
  2104. inactivity timeout (in microseconds).
  2105. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2106. last context switch</A>.
  2107. <P>
  2108. <H5>Returns</H5>
  2109. On success a non-negative integer indicating the number of bytes actually
  2110. read is returned (a value less than <TT>nbyte</TT> means the network
  2111. connection is closed or end of file is reached). Otherwise, a value of
  2112. <TT>-1</TT> is returned and <TT>errno</TT> is set to indicate the error.
  2113. Possible <TT>errno</TT> values are the same as set by the <TT>read(2)</TT>
  2114. call with two exceptions:<P>
  2115. <TABLE BORDER=0>
  2116. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2117. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2118. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
  2119. </TD></TR>
  2120. </TABLE>
  2121. <P>
  2122. <H5>Description</H5>
  2123. This function blocks the calling thread until the specified amount of data
  2124. is read in full, it encounters an end-of-stream indication, a timeout occurs,
  2125. or an error occurs.
  2126. <P>
  2127. <HR>
  2128. <P>
  2129. <A NAME="read_resid">
  2130. <H4>st_read_resid()</H4>
  2131. </A>
  2132. Reads the specified amount of data in full from a file descriptor object.
  2133. <P>
  2134. <H5>Syntax</H5>
  2135. <PRE>
  2136. #include &lt;st.h&gt;
  2137. int st_read_resid(st_netfd_t fd, void *buf, size_t *resid,
  2138. st_utime_t timeout);
  2139. </PRE>
  2140. <P>
  2141. <H5>Parameters</H5>
  2142. <TT>st_read_resid()</TT> has the following parameters:<P>
  2143. <TT>fd</TT><P>
  2144. A file descriptor object identifier (see
  2145. <A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
  2146. <TT>buf</TT><P>
  2147. A pointer to a buffer to hold the data read in. On output the buffer
  2148. contains the data.<P>
  2149. <TT>resid</TT><P>
  2150. A pointer to a number of bytes.
  2151. On entry, the amount of data to be read in full.
  2152. It must not exceed the size of <TT>buf</TT>.
  2153. On return, the amount of data remaining to be read.
  2154. (A non-zero returned value means some but not all of the data was read.)<P>
  2155. <TT>timeout</TT><P>
  2156. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
  2157. inactivity timeout (in microseconds).
  2158. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2159. last context switch</A>.
  2160. <P>
  2161. <H5>Returns</H5>
  2162. On success, zero is returned. <TT>*resid</TT> may be zero, indicating
  2163. a complete read, or non-zero, indicating the network
  2164. connection is closed or end of file is reached.
  2165. <P>
  2166. Otherwise, a value of <TT>-1</TT> is returned, <TT>*resid</TT> is non-zero,
  2167. and <TT>errno</TT> is set to indicate the error.
  2168. Possible <TT>errno</TT> values are the same as set by the <TT>read(2)</TT>
  2169. call with two exceptions:<P>
  2170. <TABLE BORDER=0>
  2171. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2172. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2173. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
  2174. </TD></TR>
  2175. </TABLE>
  2176. <P>
  2177. <H5>Description</H5>
  2178. This function blocks the calling thread until the specified amount of data
  2179. is read in full, it encounters an end-of-stream indication, a timeout occurs,
  2180. or an error occurs. It differs from <TT>st_read_fully()</TT> only in that
  2181. it allows the caller to know how many bytes were transferred before an error
  2182. occurred.
  2183. <P>
  2184. <HR>
  2185. <P>
  2186. <A NAME="readv">
  2187. <H4>st_readv()</H4>
  2188. </A>
  2189. Reads data from a specified file descriptor object into multiple buffers.
  2190. <P>
  2191. <H5>Syntax</H5>
  2192. <PRE>
  2193. #include &lt;st.h&gt;
  2194. ssize_t st_readv(st_netfd_t fd, const struct iovec *iov, int iov_size,
  2195. st_utime_t timeout);
  2196. </PRE>
  2197. <P>
  2198. <H5>Parameters</H5>
  2199. <TT>st_readv()</TT> has the following parameters:<P>
  2200. <TT>fd</TT><P>
  2201. A file descriptor object identifier (see
  2202. <A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
  2203. <TT>iov</TT><P>
  2204. An array of <tt>iovec</tt> structures that identify the buffers for holding
  2205. the data read in.
  2206. On return the buffers contain the data.<P>
  2207. <TT>iov_size</TT><P>
  2208. The number of <tt>iovec</tt> structures in the <tt>iov</tt> array.<P>
  2209. <TT>timeout</TT><P>
  2210. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
  2211. limit in microseconds for completion of the read operation.
  2212. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2213. last context switch</A>.
  2214. <P>
  2215. <H5>Returns</H5>
  2216. On success a non-negative integer indicating the number of bytes actually
  2217. read is returned (a value of <TT>0</TT> means the network connection is
  2218. closed or end of file is reached). Otherwise, a value of <TT>-1</TT> is
  2219. returned and <TT>errno</TT> is set to indicate the error.
  2220. Possible <TT>errno</TT> values are the same as set by the <TT>readv(2)</TT>
  2221. call with two exceptions:<P>
  2222. <TABLE BORDER=0>
  2223. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2224. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2225. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was read.
  2226. </TD></TR>
  2227. </TABLE>
  2228. <P>
  2229. <H5>Description</H5>
  2230. This function blocks the calling thread until it encounters an end-of-stream
  2231. indication, some positive number of bytes (but no more than fit in the buffers)
  2232. are read in, a timeout occurs, or an error occurs.
  2233. <P>
  2234. <HR>
  2235. <P>
  2236. <A NAME="readv_resid">
  2237. <H4>st_readv_resid()</H4>
  2238. </A>
  2239. Reads the specified amount of data in full from a file descriptor object
  2240. into multiple buffers.
  2241. <P>
  2242. <H5>Syntax</H5>
  2243. <PRE>
  2244. #include &lt;st.h&gt;
  2245. int st_readv_resid(st_netfd_t fd, struct iovec **iov, int *iov_size,
  2246. st_utime_t timeout);
  2247. </PRE>
  2248. <P>
  2249. <H5>Parameters</H5>
  2250. <TT>st_readv_resid()</TT> has the following parameters:<P>
  2251. <TT>fd</TT><P>
  2252. A file descriptor object identifier (see
  2253. <A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
  2254. <TT>iov</TT><P>
  2255. A pointer to an array of <tt>iovec</tt> structures.
  2256. On entry, the iovecs identify the buffers for holding the data read in.
  2257. On return, the incomplete iovecs.
  2258. This function modifies both the pointer and the array to which it points.<P>
  2259. <TT>iov_size</TT><P>
  2260. A pointer to a number of iovec structures.
  2261. On entry, the number of iovec structures pointed to by <tt>*iov</tt>.
  2262. On return, the number of incomplete or unused iovec structures.
  2263. (A non-zero returned value means some but not all of the data was read.)<P>
  2264. <TT>timeout</TT><P>
  2265. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
  2266. inactivity timeout (in microseconds).
  2267. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2268. last context switch</A>.
  2269. <P>
  2270. <H5>Returns</H5>
  2271. On success, zero is returned. <TT>*iov_size</TT> may be zero, indicating
  2272. a complete read, or non-zero, indicating the network connection is
  2273. closed or end of file is reached. <tt>*iov</tt> points to the first
  2274. iovec after the end of the original array on a complete read, or to the
  2275. first incomplete iovec on an incomplete read.
  2276. <P>
  2277. Otherwise, a value of <TT>-1</TT> is returned, <TT>*iov_size</TT> is non-zero,
  2278. and <TT>errno</TT> is set to indicate the error. <tt>*iov</tt> points to the
  2279. first unused iovec.
  2280. Possible <TT>errno</TT> values are the same as set by the <TT>readv(2)</TT>
  2281. call with two exceptions:<P>
  2282. <TABLE BORDER=0>
  2283. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2284. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2285. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
  2286. </TD></TR>
  2287. </TABLE>
  2288. <P>All of the iovecs before <tt>*iov</tt> are modified such that
  2289. <tt>iov_base</tt> points to the end of the original buffer and
  2290. <tt>iov_len</tt> is zero.
  2291. <P>
  2292. <H5>Description</H5>
  2293. This function blocks the calling thread until the specified amount of data
  2294. is read in full, it encounters an end-of-stream indication, a timeout occurs,
  2295. or an error occurs. Like <tt>st_read_resid()</tt> it blocks the thread until
  2296. <i>all</i> of the requested data is read or an error occurs. Use
  2297. <tt>st_readv()</tt> to read <i>up to</i> the requested amount of data.
  2298. <P>
  2299. <HR>
  2300. <P>
  2301. <A NAME="write">
  2302. <H4>st_write()</H4>
  2303. </A>
  2304. Writes a buffer of data to a specified file descriptor object.
  2305. <P>
  2306. <H5>Syntax</H5>
  2307. <PRE>
  2308. #include &lt;st.h&gt;
  2309. ssize_t st_write(st_netfd_t fd, const void *buf, size_t nbyte,
  2310. st_utime_t timeout);
  2311. </PRE>
  2312. <P>
  2313. <H5>Parameters</H5>
  2314. <TT>st_write()</TT> has the following parameters:<P>
  2315. <TT>fd</TT><P>
  2316. A file descriptor object identifier (see
  2317. <A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
  2318. <TT>buf</TT><P>
  2319. A pointer to the buffer holding the data to be written.<P>
  2320. <TT>nbyte</TT><P>
  2321. The amount of data in bytes to be written from the buffer.<P>
  2322. <TT>timeout</TT><P>
  2323. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
  2324. inactivity timeout (in microseconds).
  2325. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2326. last context switch</A>.
  2327. <P>
  2328. <H5>Returns</H5>
  2329. On success a non-negative integer equal to <TT>nbyte</TT> is returned.
  2330. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  2331. to indicate the error. Possible <TT>errno</TT> values are the same as set
  2332. by the <TT>write(2)</TT> call with two exceptions:<P>
  2333. <TABLE BORDER=0>
  2334. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2335. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2336. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
  2337. </TD></TR>
  2338. </TABLE>
  2339. <P>
  2340. <H5>Description</H5>
  2341. This function blocks the calling thread until <I>all</I> the data is written,
  2342. a timeout occurs, or the write operation fails. The return value is equal to
  2343. either <TT>nbyte</TT> (on success) or <TT>-1</TT> (on failure). Note that if
  2344. <TT>st_write()</TT> returns <TT>-1</TT>, some data (less than <TT>nbyte</TT>
  2345. bytes) may have been written before an error occurred.
  2346. <P>
  2347. <HR>
  2348. <P>
  2349. <A NAME="write_resid">
  2350. <H4>st_write_resid()</H4>
  2351. </A>
  2352. Writes a buffer of data to a specified file descriptor object.
  2353. <P>
  2354. <H5>Syntax</H5>
  2355. <PRE>
  2356. #include &lt;st.h&gt;
  2357. int st_write_resid(st_netfd_t fd, const void *buf, size_t *resid,
  2358. st_utime_t timeout);
  2359. </PRE>
  2360. <P>
  2361. <H5>Parameters</H5>
  2362. <TT>st_write_resid()</TT> has the following parameters:<P>
  2363. <TT>fd</TT><P>
  2364. A file descriptor object identifier (see
  2365. <A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
  2366. <TT>buf</TT><P>
  2367. A pointer to the buffer holding the data to be written.<P>
  2368. <TT>resid</TT><P>
  2369. A pointer to a number of bytes.
  2370. On entry, the amount of data to be written from the buffer.
  2371. On return, the amount of data remaining to be written.
  2372. (A non-zero returned value means some but not all of the data was written.)<P>
  2373. <TT>timeout</TT><P>
  2374. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
  2375. inactivity timeout (in microseconds).
  2376. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2377. last context switch</A>.
  2378. <P>
  2379. <H5>Returns</H5>
  2380. On success, zero is returned and <TT>*resid</TT> is zero.
  2381. Otherwise, a value of <TT>-1</TT> is returned, <TT>*resid</TT> is non-zero,
  2382. and <TT>errno</TT> is set
  2383. to indicate the error. Possible <TT>errno</TT> values are the same as set
  2384. by the <TT>write(2)</TT> call with two exceptions:<P>
  2385. <TABLE BORDER=0>
  2386. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2387. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2388. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
  2389. </TD></TR>
  2390. </TABLE>
  2391. <P>
  2392. <H5>Description</H5>
  2393. This function blocks the calling thread until <I>all</I> the data is written,
  2394. a timeout occurs, or the write operation fails. It differs from
  2395. <TT>st_write()</TT> only in that it allows the caller to know how many bytes
  2396. were transferred before an error occurred.
  2397. <P>
  2398. <HR>
  2399. <P>
  2400. <A NAME="writev">
  2401. <H4>st_writev()</H4>
  2402. </A>
  2403. Writes data to a specified file descriptor object from multiple buffers.
  2404. <P>
  2405. <H5>Syntax</H5>
  2406. <PRE>
  2407. #include &lt;st.h&gt;
  2408. ssize_t st_writev(st_netfd_t fd, const struct iovec *iov, int iov_size,
  2409. st_utime_t timeout);
  2410. </PRE>
  2411. <P>
  2412. <H5>Parameters</H5>
  2413. <TT>st_writev()</TT> has the following parameters:<P>
  2414. <TT>fd</TT><P>
  2415. A file descriptor object identifier (see
  2416. <A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
  2417. <TT>iov</TT><P>
  2418. An array of <TT>iovec</TT> structures that describe the buffers to write
  2419. from (see <TT>writev(2)</TT>).<P>
  2420. <TT>iov_size</TT><P>
  2421. Number of <TT>iovec</TT> structures in the <TT>iov</TT> array.<P>
  2422. <TT>timeout</TT><P>
  2423. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
  2424. inactivity timeout (in microseconds).
  2425. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2426. last context switch</A>.
  2427. <P>
  2428. <H5>Returns</H5>
  2429. On success a non-negative integer equal to the sum of all the buffer lengths
  2430. is returned. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT>
  2431. is set to indicate the error. Possible <TT>errno</TT> values are the same as
  2432. set by the <TT>writev(2)</TT> call with two exceptions:<P>
  2433. <TABLE BORDER=0>
  2434. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2435. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2436. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
  2437. </TD></TR>
  2438. </TABLE>
  2439. <P>
  2440. <H5>Description</H5>
  2441. This function blocks the calling thread until <I>all</I> the data is written,
  2442. a timeout occurs, or the write operation fails. The return value is equal to
  2443. either the sum of all the buffer lengths (on success) or <TT>-1</TT> (on
  2444. failure). Note that if <TT>st_writev()</TT> returns <TT>-1</TT>, part of the
  2445. data may have been written before an error occurred.
  2446. <P>
  2447. <HR>
  2448. <P>
  2449. <A NAME="writev_resid">
  2450. <H4>st_writev_resid()</H4>
  2451. </A>
  2452. Writes multiple buffers of data to a specified file descriptor object.
  2453. <P>
  2454. <H5>Syntax</H5>
  2455. <PRE>
  2456. #include &lt;st.h&gt;
  2457. int st_writev_resid(st_netfd_t fd, struct iovec **iov, int *iov_size,
  2458. st_utime_t timeout);
  2459. </PRE>
  2460. <P>
  2461. <H5>Parameters</H5>
  2462. <TT>st_writev_resid()</TT> has the following parameters:<P>
  2463. <TT>fd</TT><P>
  2464. A file descriptor object identifier (see
  2465. <A HREF=#netfd_t><B>st_netfd_t</B></A>).<P>
  2466. <TT>iov</TT><P>
  2467. A pointer to an array of <tt>iovec</tt> structures.
  2468. On entry, the iovecs identify the buffers holding the data to write.
  2469. On return, the incomplete iovecs.
  2470. This function modifies both the pointer and the array to which it points.<P>
  2471. <TT>iov_size</TT><P>
  2472. A pointer to a number of iovec structures.
  2473. On entry, the number of iovec structures pointed to by <tt>*iov</tt>.
  2474. On return, the number of incomplete or unused iovec structures.
  2475. (A non-zero returned value means some but not all of the data was written.)<P>
  2476. <TT>timeout</TT><P>
  2477. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
  2478. inactivity timeout (in microseconds).
  2479. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2480. last context switch</A>.
  2481. <P>
  2482. <H5>Returns</H5>
  2483. On success, zero is returned, <TT>*iov_size</TT> is zero, and <tt>*iov</tt>
  2484. points to the first iovec after the end of the original array.
  2485. Otherwise, a value of <TT>-1</TT> is returned, <TT>*iov_size</TT> is non-zero,
  2486. <tt>*iov</tt> points to the first incomplete iovec, and <TT>errno</TT> is set
  2487. to indicate the error. Possible <TT>errno</TT> values are the same as set
  2488. by the <TT>writev(2)</TT> call with two exceptions:<P>
  2489. <TABLE BORDER=0>
  2490. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2491. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2492. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred.
  2493. </TD></TR>
  2494. </TABLE>
  2495. <P>
  2496. All of the iovecs before <tt>*iov</tt> are modified such that
  2497. <tt>iov_base</tt> points to the end of the original buffer and
  2498. <tt>iov_len</tt> is zero.
  2499. <P>
  2500. <H5>Description</H5>
  2501. This function blocks the calling thread until <I>all</I> the data is written,
  2502. a timeout occurs, or the write operation fails. It differs from
  2503. <TT>st_writev()</TT> only in that it allows the caller to know how many bytes
  2504. were transferred before an error occurred.
  2505. <P>
  2506. <HR>
  2507. <P>
  2508. <A NAME="recvfrom">
  2509. <H4>st_recvfrom()</H4>
  2510. </A>
  2511. Receives bytes from a file descriptor object and stores the sending peer's
  2512. address.
  2513. <P>
  2514. <H5>Syntax</H5>
  2515. <PRE>
  2516. #include &lt;st.h&gt;
  2517. int st_recvfrom(st_netfd_t fd, void *buf, int len, struct sockaddr *from,
  2518. int *fromlen, st_utime_t timeout);
  2519. </PRE>
  2520. <P>
  2521. <H5>Parameters</H5>
  2522. <TT>st_recvfrom()</TT> has the following parameters:<P>
  2523. <TT>fd</TT><P>
  2524. A file descriptor object identifier (see
  2525. <A HREF=#netfd_t><B>st_netfd_t</B></A>) representing a UDP socket.<P>
  2526. <TT>buf</TT><P>
  2527. A pointer to a buffer to hold the data received.<P>
  2528. <TT>len</TT><P>
  2529. The size of <TT>buf</TT> in bytes.<P>
  2530. <TT>from</TT><P>
  2531. If this parameter is not a <TT>NULL</TT> pointer, the source address of the
  2532. message is filled in (see <TT>recvfrom(3)</TT>).<P>
  2533. <TT>fromlen</TT><P>
  2534. This is a value-result parameter, initialized to the size of the buffer
  2535. associated with <TT>from</TT>, and modified on return to indicate the actual
  2536. size of the address stored there.<P>
  2537. <TT>timeout</TT><P>
  2538. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
  2539. limit in microseconds for completion of the receive operation.
  2540. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2541. last context switch</A>.
  2542. <P>
  2543. <H5>Returns</H5>
  2544. On success a non-negative integer indicating the length of the received
  2545. message in bytes is returned. Otherwise, a value of <TT>-1</TT> is returned
  2546. and <TT>errno</TT> is set to indicate the error. Possible <TT>errno</TT>
  2547. values are the same as set by the <TT>recvfrom(3)</TT> call with two
  2548. exceptions:<P>
  2549. <TABLE BORDER=0>
  2550. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2551. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2552. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was received.
  2553. </TD></TR>
  2554. </TABLE>
  2555. <P>
  2556. <H5>Description</H5>
  2557. This function receives up to a specified number of bytes from the specified
  2558. file descriptor object representing a UDP socket.<P>
  2559. <TT>st_recvfrom()</TT> blocks the calling thread until one or more bytes are
  2560. transferred, a timeout has occurred, or there is an error. No more than
  2561. <TT>len</TT> bytes will be transferred.
  2562. <P>
  2563. <HR>
  2564. <P>
  2565. <A NAME="sendto">
  2566. <H4>st_sendto()</H4>
  2567. </A>
  2568. Sends bytes to a specified destination.
  2569. <P>
  2570. <H5>Syntax</H5>
  2571. <PRE>
  2572. #include &lt;st.h&gt;
  2573. int st_sendto(st_netfd_t fd, const void *msg, int len, struct sockaddr *to,
  2574. int tolen, st_utime_t timeout);
  2575. </PRE>
  2576. <P>
  2577. <H5>Parameters</H5>
  2578. <TT>st_sendto()</TT> has the following parameters:<P>
  2579. <TT>fd</TT><P>
  2580. A file descriptor object identifier (see
  2581. <A HREF=#netfd_t><B>st_netfd_t</B></A>) representing a UDP socket.<P>
  2582. <TT>msg</TT><P>
  2583. A pointer to a buffer containing the message to be sent.<P>
  2584. <TT>len</TT><P>
  2585. The length of the message to be sent (in bytes).<P>
  2586. <TT>to</TT><P>
  2587. A pointer to the address of the destination (see <TT>sendto(3)</TT>).<P>
  2588. <TT>tolen</TT><P>
  2589. This parameter specifies the size of the destination address.<P>
  2590. <TT>timeout</TT><P>
  2591. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
  2592. limit in microseconds for completion of the send operation.
  2593. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2594. last context switch</A>.
  2595. <P>
  2596. <H5>Returns</H5>
  2597. On success a non-negative integer indicating the number of bytes sent is
  2598. returned. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is
  2599. set to indicate the error. Possible <TT>errno</TT> values are the same as
  2600. set by the <TT>sendto(3)</TT> call with two exceptions:<P>
  2601. <TABLE BORDER=0>
  2602. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2603. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2604. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was sent.
  2605. </TD></TR>
  2606. </TABLE>
  2607. <P>
  2608. <H5>Description</H5>
  2609. This function sends a specified number of bytes from a file descriptor
  2610. object representing a UDP socket to the specified destination address.
  2611. If no buffer space is available at the underlying OS socket to hold the
  2612. message to be transmitted, then <TT>st_sendto()</TT> blocks the calling
  2613. thread until the space becomes available, a timeout occurs, or an error
  2614. occurs.
  2615. <P>
  2616. <HR>
  2617. <P>
  2618. <A NAME="recvmsg">
  2619. <H4>st_recvmsg()</H4>
  2620. </A>
  2621. Receives a message from a file descriptor object.
  2622. <P>
  2623. <H5>Syntax</H5>
  2624. <PRE>
  2625. #include &lt;st.h&gt;
  2626. int st_recvmsg(st_netfd_t fd, struct msghdr *msg, int flags,
  2627. st_utime_t timeout);
  2628. </PRE>
  2629. <P>
  2630. <H5>Parameters</H5>
  2631. <TT>st_recvmsg()</TT> has the following parameters:<P>
  2632. <TT>fd</TT><P>
  2633. A file descriptor object identifier (see
  2634. <A HREF=#netfd_t><B>st_netfd_t</B></A>) representing a UDP socket.<P>
  2635. <TT>msg</TT><P>
  2636. A pointer to a <TT>msghdr</TT> structure to describe the data received.<P>
  2637. <TT>flags</TT><P>
  2638. Control flags for <TT>recvmsg(3)</TT>.<P>
  2639. <TT>timeout</TT><P>
  2640. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
  2641. limit in microseconds for completion of the receive operation.
  2642. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2643. last context switch</A>.
  2644. <P>
  2645. <H5>Returns</H5>
  2646. On success a non-negative integer indicating the number of bytes received
  2647. is returned. Otherwise, a value of <TT>-1</TT> is returned
  2648. and <TT>errno</TT> is set to indicate the error. Possible <TT>errno</TT>
  2649. values are the same as set by the <TT>recvmsg(3)</TT> call with two
  2650. exceptions:<P>
  2651. <TABLE BORDER=0>
  2652. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2653. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2654. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was received.
  2655. </TD></TR>
  2656. </TABLE>
  2657. <P>
  2658. <H5>Description</H5>
  2659. This function receives bytes from the specified file descriptor object
  2660. representing a UDP socket. The operation is controlled by the in/out
  2661. <TT>msg</TT> parameter.<P>
  2662. <TT>st_recvmsg()</TT> blocks the calling thread until one or more bytes are
  2663. transferred, a timeout has occurred, or there is an error.
  2664. <P>
  2665. <HR>
  2666. <P>
  2667. <A NAME="sendmsg">
  2668. <H4>st_sendmsg()</H4>
  2669. </A>
  2670. Sends a message to a file descriptor object.
  2671. <P>
  2672. <H5>Syntax</H5>
  2673. <PRE>
  2674. #include &lt;st.h&gt;
  2675. int st_sendmsg(st_netfd_t fd, const struct msghdr *msg, int flags,
  2676. st_utime_t timeout);
  2677. </PRE>
  2678. <P>
  2679. <H5>Parameters</H5>
  2680. <TT>st_sendmsg()</TT> has the following parameters:<P>
  2681. <TT>fd</TT><P>
  2682. A file descriptor object identifier (see
  2683. <A HREF=#netfd_t><B>st_netfd_t</B></A>) representing a UDP socket.<P>
  2684. <TT>msg</TT><P>
  2685. A pointer to a <TT>msghdr</TT> structure describing the message to be sent.<P>
  2686. <TT>flags</TT><P>
  2687. Control flags for <TT>sendmsg(3)</TT>.<P>
  2688. <TT>timeout</TT><P>
  2689. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the time
  2690. limit in microseconds for completion of the send operation.
  2691. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2692. last context switch</A>.
  2693. <P>
  2694. <H5>Returns</H5>
  2695. On success a non-negative integer indicating the number of bytes sent is
  2696. returned. Otherwise, a value of <TT>-1</TT> is returned and <TT>errno</TT> is
  2697. set to indicate the error. Possible <TT>errno</TT> values are the same as
  2698. set by the <TT>sendmsg(3)</TT> call with two exceptions:<P>
  2699. <TABLE BORDER=0>
  2700. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2701. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2702. <TR><TD><TT>ETIME</TT></TD><TD>The timeout occurred and no data was sent.
  2703. </TD></TR>
  2704. </TABLE>
  2705. <P>
  2706. <H5>Description</H5>
  2707. This function sends bytes to a file descriptor object representing a UDP
  2708. socket. The operation is controlled by the <TT>msg</TT> parameter.
  2709. If no buffer space is available at the underlying OS socket to hold the
  2710. message to be transmitted, then <TT>st_sendmsg()</TT> blocks the calling
  2711. thread until the space becomes available, a timeout occurs, or an error
  2712. occurs.
  2713. <P>
  2714. <HR>
  2715. <P>
  2716. <A NAME="open">
  2717. <H4>st_open()</H4>
  2718. </A>
  2719. Opens a file for reading, writing, or both.
  2720. <P>
  2721. <H5>Syntax</H5>
  2722. <PRE>
  2723. #include &lt;st.h&gt;
  2724. st_netfd_t st_open(const char *path, int oflags, mode_t mode);
  2725. </PRE>
  2726. <P>
  2727. <H5>Parameters</H5>
  2728. <TT>st_open()</TT> has the following parameters:<P>
  2729. <TT>path</TT><P>
  2730. The pathname of the file to be opened.<P>
  2731. <TT>oflags</TT><P>
  2732. File status flags. These are the same flags that are used by the
  2733. <TT>open(2)</TT> system call.<P>
  2734. <TT>mode</TT><P>
  2735. Access permission bits of the file mode, if the file is created when
  2736. <TT>O_CREAT</TT> is set in <TT>oflags</TT> (see <TT>open(2)</TT>).
  2737. <P>
  2738. <H5>Returns</H5>
  2739. Upon successful completion, a new file descriptor object identifier is
  2740. returned. Otherwise, <TT>NULL</TT> is returned and <TT>errno</TT> is set
  2741. to indicate the error.
  2742. <P>
  2743. <H5>Description</H5>
  2744. This function creates a new file descriptor object of type
  2745. <A HREF=#netfd_t><B>st_netfd_t</B></A> for the file with the pathname
  2746. <TT>path</TT>. This object can be freed by
  2747. <A HREF=#netfd_free>st_netfd_free()</A> or
  2748. <A HREF=#netfd_close>st_netfd_close()</A>.<P>
  2749. The primary purpose of this function is to open FIFOs (named pipes) or
  2750. other special files in order to create an end point of communication.
  2751. However, it can be used on regular files as well.<P>
  2752. Among other things, this function always sets a non-blocking flag on the
  2753. underlying OS file descriptor, so there is no need to include that flag in
  2754. <TT>oflags</TT>.
  2755. <P>
  2756. <HR>
  2757. <P>
  2758. <A NAME="poll">
  2759. <H4>st_poll()</H4>
  2760. </A>
  2761. Detects when I/O is ready for a set of OS file descriptors.
  2762. <P>
  2763. <H5>Syntax</H5>
  2764. <PRE>
  2765. #include &lt;st.h&gt;
  2766. int st_poll(struct pollfd *pds, int npds, st_utime_t timeout);
  2767. </PRE>
  2768. <P>
  2769. <H5>Parameters</H5>
  2770. <TT>st_poll()</TT> has the following parameters:<P>
  2771. <TT>pds</TT><P>
  2772. A pointer to an array of <TT>pollfd</TT> structures (see <TT>poll(2)</TT>).
  2773. <P>
  2774. <TT>npds</TT><P>
  2775. The number of elements in the <TT>pds</TT> array.<P>
  2776. <TT>timeout</TT><P>
  2777. A value of type <A HREF=#utime_t><B>st_utime_t</B></A> specifying the
  2778. amount of time in <I>microseconds</I> the call will block waiting for I/O
  2779. to become ready. If this time expires without any I/O becoming ready,
  2780. <TT>st_poll()</TT> returns zero.
  2781. Note that timeouts are measured <A HREF="notes.html#timeouts">since the
  2782. last context switch</A>.
  2783. <P>
  2784. <H5>Returns</H5>
  2785. Upon successful completion, a non-negative value is returned. A positive
  2786. value indicates the total number of OS file descriptors in <TT>pds</TT>
  2787. that have events. A value of <TT>0</TT> indicates that the call timed out.
  2788. Upon failure, a value of <TT>-1</TT> is returned and <TT>errno</TT> is set
  2789. to indicate the error:<P>
  2790. <TABLE BORDER=0>
  2791. <TR><TD><TT>EINTR</TT></TD><TD>The current thread was interrupted by
  2792. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.</TD></TR>
  2793. </TABLE>
  2794. <P>
  2795. If an alternative event notification mechanism has been set by
  2796. <A HREF=#set_eventsys>st_set_eventsys()</A>, other values of
  2797. <TT>errno</TT> could be set upon failure as well. The values
  2798. depend on the specific mechanism in use.
  2799. <P>
  2800. <H5>Description</H5>
  2801. This function returns as soon as I/O is ready on one or more of the specified
  2802. OS file descriptors. A count of the number of ready descriptors is returned
  2803. unless a timeout occurs, in which case zero is returned.<P>
  2804. The <TT>pollfd</TT> structure is defined in the <TT>poll.h</TT> header file
  2805. and contains the following members:<P>
  2806. <PRE>
  2807. int fd; /* OS file descriptor */
  2808. short events; /* requested events */
  2809. short revents; /* returned events */
  2810. </PRE>
  2811. The <TT>events</TT> field should be set to the I/O events (readable,
  2812. writable, exception, or some combination) that the caller is interested in.
  2813. On return, the <TT>revents</TT> field is set to indicate what kind of I/O
  2814. is ready on the respective descriptor.<P>
  2815. The <TT>events</TT> and <TT>revents</TT> fields are constructed by OR-ing
  2816. any combination of the following event flags (defined in <TT>poll.h</TT>):
  2817. <P>
  2818. <TABLE BORDER=0>
  2819. <TR><TD><TT>POLLIN</TT></TD><TD><TT>fd</TT> is readable.</TD></TR>
  2820. <TR><TD><TT>POLLOUT</TT></TD><TD><TT>fd</TT> is is writable.</TD></TR>
  2821. <TR><TD><TT>POLLPRI</TT></TD><TD><TT>fd</TT> has an exception condition.</TD>
  2822. </TR>
  2823. <TR><TD><TT>POLLNVAL</TT></TD><TD><TT>fd</TT> is bad.</TD></TR>
  2824. </TABLE>
  2825. <P>
  2826. The <TT>POLLNVAL</TT> flag is only valid in the <TT>revents</TT> field;
  2827. it is not used in the <TT>events</TT> field.<P>
  2828. Despite having an interface like <tt>poll(2)</tt>, this function uses
  2829. the same event notification mechanism as the rest of the library. For
  2830. instance if an alternative event nofication mechanism was set using <a
  2831. href=#set_eventsys>st_set_eventsys()</a>, this function uses that
  2832. mechanism to check for events.<p>
  2833. Note that unlike the <TT>poll(2)</TT> call, this function has the
  2834. <TT>timeout</TT> parameter expressed in microseconds. If the value of
  2835. <TT>timeout</TT> is <tt>ST_UTIME_NO_TIMEOUT</tt>
  2836. (<TT>-1</TT>), this function blocks until a requested I/O
  2837. event occurs or until the call is interrupted by
  2838. <A HREF=#thread_interrupt>st_thread_interrupt()</A>.
  2839. <P>
  2840. <b>Note: </b> if <TT>kqueue(2)</TT> is used as an alternative event
  2841. notification mechanism (see <A
  2842. HREF=#set_eventsys>st_set_eventsys()</A>), the <TT>POLLPRI</TT>
  2843. event flag is not supported and <TT>st_poll()</TT> will return an error
  2844. if it's set (<TT>errno</TT> will be set to <TT>EINVAL</TT>).
  2845. <P>
  2846. <HR>
  2847. <P>
  2848. <A NAME="progr">
  2849. <H2>Program Structure</H2>
  2850. </A>
  2851. <P>
  2852. Generally, the following steps should be followed when writing an application
  2853. using the State Threads library:
  2854. <P>
  2855. <OL>
  2856. <LI>Configure the library by calling these pre-init functions, if desired.
  2857. <ul>
  2858. <li><A HREF="#set_utime_function">st_set_utime_function()</A></li>
  2859. <li><A HREF="#set_eventsys">st_set_eventsys()</A></li>
  2860. </ul>
  2861. </LI>
  2862. <P>
  2863. <LI>Initialize the library by calling <A HREF=#st_init>st_init()</A>.</LI>
  2864. <P>
  2865. <LI>Configure the library by calling these post-init functions, if desired.
  2866. <ul>
  2867. <li><A HREF="#timecache_set">st_timecache_set()</A></li>
  2868. <li><A HREF="#randomize_stacks">st_randomize_stacks()</A></li>
  2869. <li><A HREF="#set_switch_in_cb">st_set_switch_in_cb()</A></li>
  2870. <li><A HREF="#set_switch_out_cb">st_set_switch_out_cb()</A></li>
  2871. </ul>
  2872. </LI>
  2873. <P>
  2874. <LI>Create resources that will be shared among different processes:
  2875. create and bind listening sockets (see <TT>socket(3)</TT>,
  2876. <TT>bind(3)</TT>, <TT>listen(3)</TT>,
  2877. <A HREF=#netfd_open_socket>st_netfd_open_socket()</A>, and possibly
  2878. <A HREF=#netfd_serialize_accept>st_netfd_serialize_accept()</A>),
  2879. create shared memory segments, inter-process communication (IPC)
  2880. channels and synchronization primitives (if any).</LI>
  2881. <P>
  2882. <LI>Create several processes via <TT>fork(2)</TT>. The parent process should
  2883. either exit or become a "watchdog" (e.g., it starts a new process when
  2884. an existing one crashes, does a cleanup upon application termination,
  2885. etc.).</LI>
  2886. <P>
  2887. <LI>In each child process create a pool of threads (see
  2888. <A HREF=#thread_create>st_thread_create()</A>) to handle user
  2889. connections. Each thread in the pool may accept client connections
  2890. (<A HREF=#accept>st_accept()</A>), connect to other servers
  2891. (<A HREF=#connect>st_connect()</A>), perform various network I/O
  2892. (<A HREF=#read>st_read()</A>, <A HREF=#write>st_write()</A>, etc.).</LI>
  2893. </OL>
  2894. <P>
  2895. Note that only State Threads library <A HREF=#io>I/O functions</A> should
  2896. be used for a network I/O: any other I/O calls may block the calling process
  2897. indefinitely. For example, standard I/O functions (<TT>fgets(3)</TT>,
  2898. <TT>fread(3)</TT>, <TT>fwrite(3)</TT>, <TT>fprintf(3)</TT>, etc.) call
  2899. <TT>read(2)</TT> and <TT>write(2)</TT> directly and therefore should not be
  2900. used on sockets or pipes.
  2901. <P>
  2902. Also note that for short <A
  2903. HREF="notes.html#timeouts">timeouts</A> to work the program
  2904. should do context switches (for example by calling
  2905. <TT>st_usleep()</TT>) on a regular basis.
  2906. <P>
  2907. <HR>
  2908. <P>
  2909. <A NAME="block">
  2910. <H2>List of Blocking Functions</H2>
  2911. </A>
  2912. <P>
  2913. The thread context switch (process state change) can <I>only</I> happen
  2914. in a well-known set of blocking functions.
  2915. Only the following functions can block the calling thread:
  2916. <P>
  2917. <DL>
  2918. <DD><A HREF=#thread_join>st_thread_join()</A></DD>
  2919. <DD><A HREF=#sleep>st_sleep()</A></DD>
  2920. <DD><A HREF=#sleep>st_usleep()</A></DD>
  2921. <DD><A HREF=#cond_wait>st_cond_wait()</A></DD>
  2922. <DD><A HREF=#cond_timedwait>st_cond_timedwait()</A></DD>
  2923. <DD><A HREF=#mutex_lock>st_mutex_lock()</A></DD>
  2924. <DD><A HREF=#netfd_poll>st_netfd_poll()</A></DD>
  2925. <DD><A HREF=#accept>st_accept()</A></DD>
  2926. <DD><A HREF=#connect>st_connect()</A></DD>
  2927. <DD><A HREF=#read>st_read()</A></DD>
  2928. <DD><A HREF=#read_fully>st_read_fully()</A></DD>
  2929. <DD><A HREF=#read_resid>st_read_resid()</A></DD>
  2930. <DD><A HREF=#readv>st_readv()</A></DD>
  2931. <DD><A HREF=#readv_resid>st_readv_resid()</A></DD>
  2932. <DD><A HREF=#write>st_write()</A></DD>
  2933. <DD><A HREF=#write_resid>st_write_resid()</A></DD>
  2934. <DD><A HREF=#writev>st_writev()</A></DD>
  2935. <DD><A HREF=#writev_resid>st_writev_resid()</A></DD>
  2936. <DD><A HREF=#recvfrom>st_recvfrom()</A></DD>
  2937. <DD><A HREF=#sendto>st_sendto()</A></DD>
  2938. <DD><A HREF=#recvmsg>st_recvmsg()</A></DD>
  2939. <DD><A HREF=#sendmsg>st_sendmsg()</A></DD>
  2940. <DD><A HREF=#poll>st_poll()</A></DD>
  2941. </DL>
  2942. <P>
  2943. <HR>
  2944. <P>
  2945. </BODY>
  2946. </HTML>