<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>GNU Radio 3.2.2 C++ API: omnithread.h Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="navtree.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="navtree.js"></script> <script type="text/javascript" src="resize.js"></script> <script type="text/javascript"> $(document).ready(initResizable); </script> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.7.3 --> <div id="top"> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td style="padding-left: 0.5em;"> <div id="projectname">GNU Radio 3.2.2 C++ API</div> </td> </tr> </tbody> </table> </div> </div> <div id="side-nav" class="ui-resizable side-nav-resizable"> <div id="nav-tree"> <div id="nav-tree-contents"> </div> </div> <div id="splitbar" style="-moz-user-select:none;" class="ui-resizable-handle"> </div> </div> <script type="text/javascript"> initNavTree('omnithread_8h.html',''); </script> <div id="doc-content"> <div class="header"> <div class="headertitle"> <h1>omnithread.h</h1> </div> </div> <div class="contents"> <a href="omnithread_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// -*- Mode: C++; -*-</span> <a name="l00002"></a>00002 <span class="comment">// Package : omnithread</span> <a name="l00003"></a>00003 <span class="comment">// omnithread.h Created : 7/94 tjr</span> <a name="l00004"></a>00004 <span class="comment">//</span> <a name="l00005"></a>00005 <span class="comment">// Copyright (C) 2006 Free Software Foundation, Inc.</span> <a name="l00006"></a>00006 <span class="comment">// Copyright (C) 1994,1995,1996, 1997 Olivetti & Oracle Research Laboratory</span> <a name="l00007"></a>00007 <span class="comment">//</span> <a name="l00008"></a>00008 <span class="comment">// This file is part of the omnithread library</span> <a name="l00009"></a>00009 <span class="comment">//</span> <a name="l00010"></a>00010 <span class="comment">// The omnithread library is free software; you can redistribute it and/or</span> <a name="l00011"></a>00011 <span class="comment">// modify it under the terms of the GNU Library General Public</span> <a name="l00012"></a>00012 <span class="comment">// License as published by the Free Software Foundation; either</span> <a name="l00013"></a>00013 <span class="comment">// version 2 of the License, or (at your option) any later version.</span> <a name="l00014"></a>00014 <span class="comment">//</span> <a name="l00015"></a>00015 <span class="comment">// This library is distributed in the hope that it will be useful,</span> <a name="l00016"></a>00016 <span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <a name="l00017"></a>00017 <span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span> <a name="l00018"></a>00018 <span class="comment">// Library General Public License for more details.</span> <a name="l00019"></a>00019 <span class="comment">//</span> <a name="l00020"></a>00020 <span class="comment">// You should have received a copy of the GNU Library General Public</span> <a name="l00021"></a>00021 <span class="comment">// License along with this library; if not, write to the Free</span> <a name="l00022"></a>00022 <span class="comment">// Software Foundation, Inc., 51 Franklin Street, Boston, MA </span> <a name="l00023"></a>00023 <span class="comment">// 02110-1301, USA</span> <a name="l00024"></a>00024 <span class="comment">//</span> <a name="l00025"></a>00025 <a name="l00026"></a>00026 <span class="comment">//</span> <a name="l00027"></a>00027 <span class="comment">// Interface to OMNI thread abstraction.</span> <a name="l00028"></a>00028 <span class="comment">//</span> <a name="l00029"></a>00029 <span class="comment">// This file declares classes for threads and synchronisation objects</span> <a name="l00030"></a>00030 <span class="comment">// (mutexes, condition variables and counting semaphores).</span> <a name="l00031"></a>00031 <span class="comment">//</span> <a name="l00032"></a>00032 <span class="comment">// Wherever a seemingly arbitrary choice has had to be made as to the interface</span> <a name="l00033"></a>00033 <span class="comment">// provided, the intention here has been to be as POSIX-like as possible. This</span> <a name="l00034"></a>00034 <span class="comment">// is why there is no semaphore timed wait, for example.</span> <a name="l00035"></a>00035 <span class="comment">//</span> <a name="l00036"></a>00036 <a name="l00037"></a>00037 <span class="preprocessor">#ifndef __omnithread_h_</span> <a name="l00038"></a>00038 <span class="preprocessor"></span><span class="preprocessor">#define __omnithread_h_</span> <a name="l00039"></a>00039 <span class="preprocessor"></span> <a name="l00040"></a>00040 <span class="preprocessor">#ifndef NULL</span> <a name="l00041"></a><a class="code" href="omnithread_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4">00041</a> <span class="preprocessor"></span><span class="preprocessor">#define NULL 0</span> <a name="l00042"></a>00042 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00043"></a>00043 <span class="preprocessor"></span> <a name="l00044"></a>00044 <span class="keyword">class </span><a class="code" href="classomni__mutex.html">omni_mutex</a>; <a name="l00045"></a>00045 <span class="keyword">class </span><a class="code" href="classomni__condition.html">omni_condition</a>; <a name="l00046"></a>00046 <span class="keyword">class </span><a class="code" href="classomni__semaphore.html">omni_semaphore</a>; <a name="l00047"></a>00047 <span class="keyword">class </span><a class="code" href="classomni__thread.html">omni_thread</a>; <a name="l00048"></a>00048 <a name="l00049"></a>00049 <span class="comment">//</span> <a name="l00050"></a>00050 <span class="comment">// OMNI_THREAD_EXPOSE can be defined as public or protected to expose the</span> <a name="l00051"></a>00051 <span class="comment">// implementation class - this may be useful for debugging. Hopefully this</span> <a name="l00052"></a>00052 <span class="comment">// won't change the underlying structure which the compiler generates so that</span> <a name="l00053"></a>00053 <span class="comment">// this can work without recompiling the library.</span> <a name="l00054"></a>00054 <span class="comment">//</span> <a name="l00055"></a>00055 <a name="l00056"></a>00056 <span class="preprocessor">#ifndef OMNI_THREAD_EXPOSE</span> <a name="l00057"></a><a class="code" href="omnithread_8h.html#aa328f2be804b7cfb832792bb3ebc9f67">00057</a> <span class="preprocessor"></span><span class="preprocessor">#define OMNI_THREAD_EXPOSE private</span> <a name="l00058"></a>00058 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00059"></a>00059 <span class="preprocessor"></span> <a name="l00060"></a>00060 <span class="comment">//</span> <a name="l00061"></a>00061 <span class="comment">// Include implementation-specific header file.</span> <a name="l00062"></a>00062 <span class="comment">//</span> <a name="l00063"></a>00063 <span class="comment">// This must define 4 CPP macros of the form OMNI_x_IMPLEMENTATION for mutex,</span> <a name="l00064"></a>00064 <span class="comment">// condition variable, semaphore and thread. Each should define any</span> <a name="l00065"></a>00065 <span class="comment">// implementation-specific members of the corresponding classes.</span> <a name="l00066"></a>00066 <span class="comment">//</span> <a name="l00067"></a>00067 <a name="l00068"></a>00068 <a name="l00069"></a>00069 <span class="comment">//</span> <a name="l00070"></a>00070 <span class="comment">// For now, we assume they've always got a Posix Threads implementation.</span> <a name="l00071"></a>00071 <span class="comment">// If not, it'll take some configure hacking to sort it out, along with</span> <a name="l00072"></a>00072 <span class="comment">// the relevant libraries to link with, etc.</span> <a name="l00073"></a>00073 <span class="comment">//</span> <a name="l00074"></a>00074 <a name="l00075"></a>00075 <span class="preprocessor">#if !defined(OMNITHREAD_POSIX) && !defined(OMNITHREAD_NT) && defined HAVE_CONFIG_H</span> <a name="l00076"></a>00076 <span class="preprocessor"></span><span class="comment">// #include <config.h> // No, No, No! Never include <config.h> from a header</span> <a name="l00077"></a>00077 <span class="preprocessor">#endif</span> <a name="l00078"></a>00078 <span class="preprocessor"></span> <a name="l00079"></a>00079 <span class="preprocessor">#if defined(OMNITHREAD_POSIX)</span> <a name="l00080"></a>00080 <span class="preprocessor"></span><span class="preprocessor">#include <gnuradio/ot_posix.h></span> <a name="l00081"></a>00081 <a name="l00082"></a>00082 <span class="preprocessor">#elif defined(OMNITHREAD_NT)</span> <a name="l00083"></a>00083 <span class="preprocessor"></span><span class="preprocessor">#include <gnuradio/ot_nt.h></span> <a name="l00084"></a>00084 <a name="l00085"></a>00085 <span class="preprocessor">#ifdef _MSC_VER</span> <a name="l00086"></a>00086 <span class="preprocessor"></span> <a name="l00087"></a>00087 <span class="comment">// Using MSVC++ to compile. If compiling library as a DLL,</span> <a name="l00088"></a>00088 <span class="comment">// define _OMNITHREAD_DLL. If compiling as a statuc library, define</span> <a name="l00089"></a>00089 <span class="comment">// _WINSTATIC</span> <a name="l00090"></a>00090 <span class="comment">// If compiling an application that is to be statically linked to omnithread,</span> <a name="l00091"></a>00091 <span class="comment">// define _WINSTATIC (if the application is to be dynamically linked, </span> <a name="l00092"></a>00092 <span class="comment">// there is no need to define any of these macros).</span> <a name="l00093"></a>00093 <a name="l00094"></a>00094 <span class="preprocessor">#if defined (_OMNITHREAD_DLL) && defined(_WINSTATIC)</span> <a name="l00095"></a>00095 <span class="preprocessor"></span><span class="preprocessor">#error "Both _OMNITHREAD_DLL and _WINSTATIC are defined."</span> <a name="l00096"></a>00096 <span class="preprocessor"></span><span class="preprocessor">#elif defined(_OMNITHREAD_DLL)</span> <a name="l00097"></a>00097 <span class="preprocessor"></span><span class="preprocessor">#define _OMNITHREAD_NTDLL_ __declspec(dllexport)</span> <a name="l00098"></a>00098 <span class="preprocessor"></span><span class="preprocessor">#elif !defined(_WINSTATIC)</span> <a name="l00099"></a>00099 <span class="preprocessor"></span><span class="preprocessor">#define _OMNITHREAD_NTDLL_ __declspec(dllimport)</span> <a name="l00100"></a>00100 <span class="preprocessor"></span><span class="preprocessor">#elif defined(_WINSTATIC)</span> <a name="l00101"></a>00101 <span class="preprocessor"></span><span class="preprocessor">#define _OMNITHREAD_NTDLL_</span> <a name="l00102"></a>00102 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00103"></a>00103 <span class="preprocessor"></span> <span class="comment">// _OMNITHREAD_DLL && _WINSTATIC</span> <a name="l00104"></a>00104 <a name="l00105"></a>00105 <span class="preprocessor">#else</span> <a name="l00106"></a>00106 <span class="preprocessor"></span> <a name="l00107"></a>00107 <span class="comment">// Not using MSVC++ to compile</span> <a name="l00108"></a>00108 <span class="preprocessor">#define _OMNITHREAD_NTDLL_</span> <a name="l00109"></a>00109 <span class="preprocessor"></span> <a name="l00110"></a>00110 <span class="preprocessor">#endif</span> <a name="l00111"></a>00111 <span class="preprocessor"></span> <span class="comment">// _MSC_VER</span> <a name="l00112"></a>00112 <a name="l00113"></a>00113 <span class="preprocessor">#elif defined(__vxWorks__)</span> <a name="l00114"></a>00114 <span class="preprocessor"></span><span class="preprocessor">#include <gnuradio/ot_VxThread.h></span> <a name="l00115"></a>00115 <a name="l00116"></a>00116 <span class="preprocessor">#elif defined(__sunos__)</span> <a name="l00117"></a>00117 <span class="preprocessor"></span><span class="preprocessor">#if __OSVERSION__ != 5</span> <a name="l00118"></a>00118 <span class="preprocessor"></span><span class="comment">// XXX Workaround for SUN C++ compiler (seen on 4.2) Template.DB code</span> <a name="l00119"></a>00119 <span class="comment">// regeneration bug. See omniORB2/CORBA_sysdep.h for details.</span> <a name="l00120"></a>00120 <span class="preprocessor">#if !defined(__SUNPRO_CC) || __OSVERSION__ != '5'</span> <a name="l00121"></a>00121 <span class="preprocessor"></span><span class="preprocessor">#error "Only SunOS 5.x or later is supported."</span> <a name="l00122"></a>00122 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00123"></a>00123 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00124"></a>00124 <span class="preprocessor"></span><span class="preprocessor">#ifdef UseSolarisThreads</span> <a name="l00125"></a>00125 <span class="preprocessor"></span><span class="preprocessor">#include <gnuradio/ot_solaris.h></span> <a name="l00126"></a>00126 <span class="preprocessor">#else</span> <a name="l00127"></a>00127 <span class="preprocessor"></span><span class="preprocessor">#include <gnuradio/ot_posix.h></span> <a name="l00128"></a>00128 <span class="preprocessor">#endif</span> <a name="l00129"></a>00129 <span class="preprocessor"></span> <a name="l00130"></a>00130 <span class="preprocessor">#elif defined(__rtems__)</span> <a name="l00131"></a>00131 <span class="preprocessor"></span><span class="preprocessor">#include <gnuradio/ot_posix.h></span> <a name="l00132"></a>00132 <span class="preprocessor">#include <sched.h></span> <a name="l00133"></a>00133 <a name="l00134"></a>00134 <span class="preprocessor">#elif defined(__macos__)</span> <a name="l00135"></a>00135 <span class="preprocessor"></span><span class="preprocessor">#include <gnuradio/ot_posix.h></span> <a name="l00136"></a>00136 <span class="preprocessor">#include <sched.h></span> <a name="l00137"></a>00137 <a name="l00138"></a>00138 <span class="preprocessor">#else</span> <a name="l00139"></a>00139 <span class="preprocessor"></span><span class="preprocessor">#error "No implementation header file"</span> <a name="l00140"></a>00140 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00141"></a>00141 <span class="preprocessor"></span> <a name="l00142"></a>00142 <a name="l00143"></a>00143 <span class="preprocessor">#if !defined(__WIN32__)</span> <a name="l00144"></a><a class="code" href="omnithread_8h.html#a2e26482727d49712e0fd18a61b962e30">00144</a> <span class="preprocessor"></span><span class="preprocessor">#define _OMNITHREAD_NTDLL_</span> <a name="l00145"></a>00145 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00146"></a>00146 <span class="preprocessor"></span> <a name="l00147"></a>00147 <span class="preprocessor">#if (!defined(OMNI_MUTEX_IMPLEMENTATION) || \</span> <a name="l00148"></a>00148 <span class="preprocessor"> !defined(OMNI_MUTEX_LOCK_IMPLEMENTATION) || \</span> <a name="l00149"></a>00149 <span class="preprocessor"> !defined(OMNI_MUTEX_TRYLOCK_IMPLEMENTATION)|| \</span> <a name="l00150"></a>00150 <span class="preprocessor"> !defined(OMNI_MUTEX_UNLOCK_IMPLEMENTATION) || \</span> <a name="l00151"></a>00151 <span class="preprocessor"> !defined(OMNI_CONDITION_IMPLEMENTATION) || \</span> <a name="l00152"></a>00152 <span class="preprocessor"> !defined(OMNI_SEMAPHORE_IMPLEMENTATION) || \</span> <a name="l00153"></a>00153 <span class="preprocessor"> !defined(OMNI_THREAD_IMPLEMENTATION))</span> <a name="l00154"></a>00154 <span class="preprocessor"></span><span class="preprocessor">#error "Implementation header file incomplete"</span> <a name="l00155"></a>00155 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00156"></a>00156 <span class="preprocessor"></span> <a name="l00157"></a>00157 <a name="l00158"></a>00158 <span class="comment">//</span> <a name="l00159"></a>00159 <span class="comment">// This exception is thrown in the event of a fatal error.</span> <a name="l00160"></a>00160 <span class="comment">//</span> <a name="l00161"></a>00161 <a name="l00162"></a><a class="code" href="classomni__thread__fatal.html">00162</a> <span class="keyword">class </span><a class="code" href="omnithread_8h.html#a2e26482727d49712e0fd18a61b962e30">_OMNITHREAD_NTDLL_</a> <a class="code" href="classomni__thread__fatal.html">omni_thread_fatal</a> { <a name="l00163"></a>00163 <span class="keyword">public</span>: <a name="l00164"></a><a class="code" href="classomni__thread__fatal.html#ab431d1dfde8732f7b268afb0a12d929b">00164</a> <span class="keywordtype">int</span> <a class="code" href="classomni__thread__fatal.html#ab431d1dfde8732f7b268afb0a12d929b">error</a>; <a name="l00165"></a><a class="code" href="classomni__thread__fatal.html#a08dd9224f8deadbaafa37f5b2400de37">00165</a> <a class="code" href="classomni__thread__fatal.html#a08dd9224f8deadbaafa37f5b2400de37">omni_thread_fatal</a>(<span class="keywordtype">int</span> e = 0) : error(e) {} <a name="l00166"></a>00166 }; <a name="l00167"></a>00167 <a name="l00168"></a>00168 <a name="l00169"></a>00169 <span class="comment">//</span> <a name="l00170"></a>00170 <span class="comment">// This exception is thrown when an operation is invoked with invalid</span> <a name="l00171"></a>00171 <span class="comment">// arguments.</span> <a name="l00172"></a>00172 <span class="comment">//</span> <a name="l00173"></a>00173 <a name="l00174"></a><a class="code" href="classomni__thread__invalid.html">00174</a> <span class="keyword">class </span><a class="code" href="omnithread_8h.html#a2e26482727d49712e0fd18a61b962e30">_OMNITHREAD_NTDLL_</a> <a class="code" href="classomni__thread__invalid.html">omni_thread_invalid</a> {}; <a name="l00175"></a>00175 <a name="l00176"></a>00176 <span class="comment"></span> <a name="l00177"></a>00177 <span class="comment">///////////////////////////////////////////////////////////////////////////</span> <a name="l00178"></a>00178 <span class="comment"></span><span class="comment">//</span> <a name="l00179"></a>00179 <span class="comment">// Mutex</span> <a name="l00180"></a>00180 <span class="comment">//</span><span class="comment"></span> <a name="l00181"></a>00181 <span class="comment">///////////////////////////////////////////////////////////////////////////</span> <a name="l00182"></a>00182 <span class="comment"></span> <a name="l00183"></a><a class="code" href="classomni__mutex.html">00183</a> <span class="keyword">class </span><a class="code" href="omnithread_8h.html#a2e26482727d49712e0fd18a61b962e30">_OMNITHREAD_NTDLL_</a> <a class="code" href="classomni__mutex.html">omni_mutex</a> { <a name="l00184"></a>00184 <a name="l00185"></a>00185 <span class="keyword">public</span>: <a name="l00186"></a>00186 <a class="code" href="classomni__mutex.html">omni_mutex</a>(<span class="keywordtype">void</span>); <a name="l00187"></a>00187 ~<a class="code" href="classomni__mutex.html">omni_mutex</a>(<span class="keywordtype">void</span>); <a name="l00188"></a>00188 <a name="l00189"></a><a class="code" href="classomni__mutex.html#a9b07185f9e0b8ce92f88e8a90c948ac9">00189</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classomni__mutex.html#a9b07185f9e0b8ce92f88e8a90c948ac9">lock</a>(<span class="keywordtype">void</span>) { <a class="code" href="ot__nt_8h.html#a869be82c956dc858288b691ec920e94e">OMNI_MUTEX_LOCK_IMPLEMENTATION</a> } <a name="l00190"></a><a class="code" href="classomni__mutex.html#af96d1aa993e9585123aea5a7fe4c8b5c">00190</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classomni__mutex.html#af96d1aa993e9585123aea5a7fe4c8b5c">unlock</a>(<span class="keywordtype">void</span>) { <a class="code" href="ot__nt_8h.html#ae5eea767ddd262fa3a5fe58bf134c88b">OMNI_MUTEX_UNLOCK_IMPLEMENTATION</a> } <a name="l00191"></a><a class="code" href="classomni__mutex.html#a0b6c6ba36804b427d79d7fa4e20f05f6">00191</a> <span class="keyword">inline</span> <span class="keywordtype">int</span> <a class="code" href="classomni__mutex.html#a0b6c6ba36804b427d79d7fa4e20f05f6">trylock</a>(<span class="keywordtype">void</span>) { <span class="keywordflow">return</span> <a class="code" href="ot__nt_8h.html#a60188a002a01fc60933ed6236d04ae21">OMNI_MUTEX_TRYLOCK_IMPLEMENTATION</a> } <a name="l00192"></a>00192 <span class="comment">// if mutex is unlocked, lock it and return 1 (true).</span> <a name="l00193"></a>00193 <span class="comment">// If it's already locked then return 0 (false).</span> <a name="l00194"></a>00194 <a name="l00195"></a><a class="code" href="classomni__mutex.html#af1ba72e77ce3eda44e136d9bcbdcae3b">00195</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classomni__mutex.html#af1ba72e77ce3eda44e136d9bcbdcae3b">acquire</a>(<span class="keywordtype">void</span>) { lock(); } <a name="l00196"></a><a class="code" href="classomni__mutex.html#ac6c9e0165c54331e18bf10f0df868764">00196</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classomni__mutex.html#ac6c9e0165c54331e18bf10f0df868764">release</a>(<span class="keywordtype">void</span>) { unlock(); } <a name="l00197"></a>00197 <span class="comment">// the names lock and unlock are preferred over acquire and release</span> <a name="l00198"></a>00198 <span class="comment">// since we are attempting to be as POSIX-like as possible.</span> <a name="l00199"></a>00199 <a name="l00200"></a><a class="code" href="classomni__mutex.html#a340108a225a697f546857d6e8df60a68">00200</a> <span class="keyword">friend</span> <span class="keyword">class </span><a class="code" href="classomni__condition.html">omni_condition</a>; <a name="l00201"></a>00201 <a name="l00202"></a>00202 <span class="keyword">private</span>: <a name="l00203"></a>00203 <span class="comment">// dummy copy constructor and operator= to prevent copying</span> <a name="l00204"></a>00204 <a class="code" href="classomni__mutex.html">omni_mutex</a>(<span class="keyword">const</span> <a class="code" href="classomni__mutex.html">omni_mutex</a>&); <a name="l00205"></a>00205 <a class="code" href="classomni__mutex.html">omni_mutex</a>& operator=(<span class="keyword">const</span> <a class="code" href="classomni__mutex.html">omni_mutex</a>&); <a name="l00206"></a>00206 <a name="l00207"></a>00207 <a class="code" href="omnithread_8h.html#aa328f2be804b7cfb832792bb3ebc9f67">OMNI_THREAD_EXPOSE</a>: <a name="l00208"></a>00208 <a class="code" href="ot__mach_8h.html#a7ad99028e48ab278b80a11e32ec300e2">OMNI_MUTEX_IMPLEMENTATION</a> <a name="l00209"></a>00209 }; <a name="l00210"></a>00210 <a name="l00211"></a>00211 <span class="comment">//</span> <a name="l00212"></a>00212 <span class="comment">// As an alternative to:</span> <a name="l00213"></a>00213 <span class="comment">// {</span> <a name="l00214"></a>00214 <span class="comment">// mutex.lock();</span> <a name="l00215"></a>00215 <span class="comment">// .....</span> <a name="l00216"></a>00216 <span class="comment">// mutex.unlock();</span> <a name="l00217"></a>00217 <span class="comment">// }</span> <a name="l00218"></a>00218 <span class="comment">//</span> <a name="l00219"></a>00219 <span class="comment">// you can use a single instance of the omni_mutex_lock class:</span> <a name="l00220"></a>00220 <span class="comment">//</span> <a name="l00221"></a>00221 <span class="comment">// {</span> <a name="l00222"></a>00222 <span class="comment">// omni_mutex_lock l(mutex);</span> <a name="l00223"></a>00223 <span class="comment">// ....</span> <a name="l00224"></a>00224 <span class="comment">// }</span> <a name="l00225"></a>00225 <span class="comment">//</span> <a name="l00226"></a>00226 <span class="comment">// This has the advantage that mutex.unlock() will be called automatically</span> <a name="l00227"></a>00227 <span class="comment">// when an exception is thrown.</span> <a name="l00228"></a>00228 <span class="comment">//</span> <a name="l00229"></a>00229 <a name="l00230"></a><a class="code" href="classomni__mutex__lock.html">00230</a> <span class="keyword">class </span><a class="code" href="omnithread_8h.html#a2e26482727d49712e0fd18a61b962e30">_OMNITHREAD_NTDLL_</a> <a class="code" href="classomni__mutex__lock.html">omni_mutex_lock</a> { <a name="l00231"></a>00231 <a class="code" href="classomni__mutex.html">omni_mutex</a>& mutex; <a name="l00232"></a>00232 <span class="keyword">public</span>: <a name="l00233"></a><a class="code" href="classomni__mutex__lock.html#a0c960b8e312bb4d3eb45fdcafcd564f9">00233</a> <a class="code" href="classomni__mutex__lock.html#a0c960b8e312bb4d3eb45fdcafcd564f9">omni_mutex_lock</a>(<a class="code" href="classomni__mutex.html">omni_mutex</a>& m) : mutex(m) { mutex.lock(); } <a name="l00234"></a><a class="code" href="classomni__mutex__lock.html#ad67b5deac2dee518e8b17ad873f10dcf">00234</a> <a class="code" href="classomni__mutex__lock.html#ad67b5deac2dee518e8b17ad873f10dcf">~omni_mutex_lock</a>(<span class="keywordtype">void</span>) { mutex.unlock(); } <a name="l00235"></a>00235 <span class="keyword">private</span>: <a name="l00236"></a>00236 <span class="comment">// dummy copy constructor and operator= to prevent copying</span> <a name="l00237"></a>00237 <a class="code" href="classomni__mutex__lock.html">omni_mutex_lock</a>(<span class="keyword">const</span> <a class="code" href="classomni__mutex__lock.html">omni_mutex_lock</a>&); <a name="l00238"></a>00238 <a class="code" href="classomni__mutex__lock.html">omni_mutex_lock</a>& operator=(<span class="keyword">const</span> <a class="code" href="classomni__mutex__lock.html">omni_mutex_lock</a>&); <a name="l00239"></a>00239 }; <a name="l00240"></a>00240 <a name="l00241"></a>00241 <span class="comment"></span> <a name="l00242"></a>00242 <span class="comment">///////////////////////////////////////////////////////////////////////////</span> <a name="l00243"></a>00243 <span class="comment"></span><span class="comment">//</span> <a name="l00244"></a>00244 <span class="comment">// Condition variable</span> <a name="l00245"></a>00245 <span class="comment">//</span><span class="comment"></span> <a name="l00246"></a>00246 <span class="comment">///////////////////////////////////////////////////////////////////////////</span> <a name="l00247"></a>00247 <span class="comment"></span> <a name="l00248"></a><a class="code" href="classomni__condition.html">00248</a> <span class="keyword">class </span><a class="code" href="omnithread_8h.html#a2e26482727d49712e0fd18a61b962e30">_OMNITHREAD_NTDLL_</a> <a class="code" href="classomni__condition.html">omni_condition</a> { <a name="l00249"></a>00249 <a name="l00250"></a>00250 <a class="code" href="classomni__mutex.html">omni_mutex</a>* mutex; <a name="l00251"></a>00251 <a name="l00252"></a>00252 <span class="keyword">public</span>: <a name="l00253"></a>00253 <a class="code" href="classomni__condition.html">omni_condition</a>(<a class="code" href="classomni__mutex.html">omni_mutex</a>* m); <a name="l00254"></a>00254 <span class="comment">// constructor must be given a pointer to an existing mutex. The</span> <a name="l00255"></a>00255 <span class="comment">// condition variable is then linked to the mutex, so that there is an</span> <a name="l00256"></a>00256 <span class="comment">// implicit unlock and lock around wait() and timed_wait().</span> <a name="l00257"></a>00257 <a name="l00258"></a>00258 ~<a class="code" href="classomni__condition.html">omni_condition</a>(<span class="keywordtype">void</span>); <a name="l00259"></a>00259 <a name="l00260"></a>00260 <span class="keywordtype">void</span> wait(<span class="keywordtype">void</span>); <a name="l00261"></a>00261 <span class="comment">// wait for the condition variable to be signalled. The mutex is</span> <a name="l00262"></a>00262 <span class="comment">// implicitly released before waiting and locked again after waking up.</span> <a name="l00263"></a>00263 <span class="comment">// If wait() is called by multiple threads, a signal may wake up more</span> <a name="l00264"></a>00264 <span class="comment">// than one thread. See POSIX threads documentation for details.</span> <a name="l00265"></a>00265 <a name="l00266"></a>00266 <span class="keywordtype">int</span> timedwait(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> secs, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> nanosecs = 0); <a name="l00267"></a>00267 <span class="comment">// timedwait() is given an absolute time to wait until. To wait for a</span> <a name="l00268"></a>00268 <span class="comment">// relative time from now, use omni_thread::get_time. See POSIX threads</span> <a name="l00269"></a>00269 <span class="comment">// documentation for why absolute times are better than relative.</span> <a name="l00270"></a>00270 <span class="comment">// Returns 1 (true) if successfully signalled, 0 (false) if time</span> <a name="l00271"></a>00271 <span class="comment">// expired.</span> <a name="l00272"></a>00272 <a name="l00273"></a>00273 <span class="keywordtype">void</span> signal(<span class="keywordtype">void</span>); <a name="l00274"></a>00274 <span class="comment">// if one or more threads have called wait(), signal wakes up at least</span> <a name="l00275"></a>00275 <span class="comment">// one of them, possibly more. See POSIX threads documentation for</span> <a name="l00276"></a>00276 <span class="comment">// details.</span> <a name="l00277"></a>00277 <a name="l00278"></a>00278 <span class="keywordtype">void</span> broadcast(<span class="keywordtype">void</span>); <a name="l00279"></a>00279 <span class="comment">// broadcast is like signal but wakes all threads which have called</span> <a name="l00280"></a>00280 <span class="comment">// wait().</span> <a name="l00281"></a>00281 <a name="l00282"></a>00282 <span class="keyword">private</span>: <a name="l00283"></a>00283 <span class="comment">// dummy copy constructor and operator= to prevent copying</span> <a name="l00284"></a>00284 <a class="code" href="classomni__condition.html">omni_condition</a>(<span class="keyword">const</span> <a class="code" href="classomni__condition.html">omni_condition</a>&); <a name="l00285"></a>00285 <a class="code" href="classomni__condition.html">omni_condition</a>& operator=(<span class="keyword">const</span> <a class="code" href="classomni__condition.html">omni_condition</a>&); <a name="l00286"></a>00286 <a name="l00287"></a>00287 <a class="code" href="omnithread_8h.html#aa328f2be804b7cfb832792bb3ebc9f67">OMNI_THREAD_EXPOSE</a>: <a name="l00288"></a>00288 <a class="code" href="ot__mach_8h.html#a92b19edc455706d139150fe3d44d2943">OMNI_CONDITION_IMPLEMENTATION</a> <a name="l00289"></a>00289 }; <a name="l00290"></a>00290 <a name="l00291"></a>00291 <span class="comment"></span> <a name="l00292"></a>00292 <span class="comment">///////////////////////////////////////////////////////////////////////////</span> <a name="l00293"></a>00293 <span class="comment"></span><span class="comment">//</span> <a name="l00294"></a>00294 <span class="comment">// Counting (or binary) semaphore</span> <a name="l00295"></a>00295 <span class="comment">//</span><span class="comment"></span> <a name="l00296"></a>00296 <span class="comment">///////////////////////////////////////////////////////////////////////////</span> <a name="l00297"></a>00297 <span class="comment"></span> <a name="l00298"></a><a class="code" href="classomni__semaphore.html">00298</a> <span class="keyword">class </span><a class="code" href="omnithread_8h.html#a2e26482727d49712e0fd18a61b962e30">_OMNITHREAD_NTDLL_</a> <a class="code" href="classomni__semaphore.html">omni_semaphore</a> { <a name="l00299"></a>00299 <a name="l00300"></a>00300 <span class="keyword">public</span>: <a name="l00301"></a>00301 <span class="comment">// if max_count == 1, you've got a binary semaphore.</span> <a name="l00302"></a>00302 <a class="code" href="classomni__semaphore.html">omni_semaphore</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> initial = 1, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> max_count = 0x7fffffff); <a name="l00303"></a>00303 ~<a class="code" href="classomni__semaphore.html">omni_semaphore</a>(<span class="keywordtype">void</span>); <a name="l00304"></a>00304 <a name="l00305"></a>00305 <span class="keywordtype">void</span> wait(<span class="keywordtype">void</span>); <a name="l00306"></a>00306 <span class="comment">// if semaphore value is > 0 then decrement it and carry on. If it's</span> <a name="l00307"></a>00307 <span class="comment">// already 0 then block.</span> <a name="l00308"></a>00308 <a name="l00309"></a>00309 <span class="keywordtype">int</span> trywait(<span class="keywordtype">void</span>); <a name="l00310"></a>00310 <span class="comment">// if semaphore value is > 0 then decrement it and return 1 (true).</span> <a name="l00311"></a>00311 <span class="comment">// If it's already 0 then return 0 (false).</span> <a name="l00312"></a>00312 <a name="l00313"></a>00313 <span class="keywordtype">void</span> post(<span class="keywordtype">void</span>); <a name="l00314"></a>00314 <span class="comment">// if any threads are blocked in wait(), wake one of them up. Otherwise</span> <a name="l00315"></a>00315 <span class="comment">// increment the value of the semaphore.</span> <a name="l00316"></a>00316 <a name="l00317"></a>00317 <span class="keyword">private</span>: <a name="l00318"></a>00318 <span class="comment">// dummy copy constructor and operator= to prevent copying</span> <a name="l00319"></a>00319 <a class="code" href="classomni__semaphore.html">omni_semaphore</a>(<span class="keyword">const</span> <a class="code" href="classomni__semaphore.html">omni_semaphore</a>&); <a name="l00320"></a>00320 <a class="code" href="classomni__semaphore.html">omni_semaphore</a>& operator=(<span class="keyword">const</span> <a class="code" href="classomni__semaphore.html">omni_semaphore</a>&); <a name="l00321"></a>00321 <a name="l00322"></a>00322 <a class="code" href="omnithread_8h.html#aa328f2be804b7cfb832792bb3ebc9f67">OMNI_THREAD_EXPOSE</a>: <a name="l00323"></a>00323 <a class="code" href="ot__mach_8h.html#a1b0bdd06018f31de329a10966ff315d6">OMNI_SEMAPHORE_IMPLEMENTATION</a> <a name="l00324"></a>00324 }; <a name="l00325"></a>00325 <a name="l00326"></a>00326 <span class="comment">//</span> <a name="l00327"></a>00327 <span class="comment">// A helper class for semaphores, similar to omni_mutex_lock above.</span> <a name="l00328"></a>00328 <span class="comment">//</span> <a name="l00329"></a>00329 <a name="l00330"></a><a class="code" href="classomni__semaphore__lock.html">00330</a> <span class="keyword">class </span><a class="code" href="omnithread_8h.html#a2e26482727d49712e0fd18a61b962e30">_OMNITHREAD_NTDLL_</a> <a class="code" href="classomni__semaphore__lock.html">omni_semaphore_lock</a> { <a name="l00331"></a>00331 <a class="code" href="classomni__semaphore.html">omni_semaphore</a>& sem; <a name="l00332"></a>00332 <span class="keyword">public</span>: <a name="l00333"></a><a class="code" href="classomni__semaphore__lock.html#a49d4392cc65c24df0892899077232fe2">00333</a> <a class="code" href="classomni__semaphore__lock.html#a49d4392cc65c24df0892899077232fe2">omni_semaphore_lock</a>(<a class="code" href="classomni__semaphore.html">omni_semaphore</a>& s) : sem(s) { sem.wait(); } <a name="l00334"></a><a class="code" href="classomni__semaphore__lock.html#a684ccf2e23cc1821ee8d7eccf06ef673">00334</a> <a class="code" href="classomni__semaphore__lock.html#a684ccf2e23cc1821ee8d7eccf06ef673">~omni_semaphore_lock</a>(<span class="keywordtype">void</span>) { sem.post(); } <a name="l00335"></a>00335 <span class="keyword">private</span>: <a name="l00336"></a>00336 <span class="comment">// dummy copy constructor and operator= to prevent copying</span> <a name="l00337"></a>00337 <a class="code" href="classomni__semaphore__lock.html">omni_semaphore_lock</a>(<span class="keyword">const</span> <a class="code" href="classomni__semaphore__lock.html">omni_semaphore_lock</a>&); <a name="l00338"></a>00338 <a class="code" href="classomni__semaphore__lock.html">omni_semaphore_lock</a>& operator=(<span class="keyword">const</span> <a class="code" href="classomni__semaphore__lock.html">omni_semaphore_lock</a>&); <a name="l00339"></a>00339 }; <a name="l00340"></a>00340 <a name="l00341"></a>00341 <span class="comment"></span> <a name="l00342"></a>00342 <span class="comment">///////////////////////////////////////////////////////////////////////////</span> <a name="l00343"></a>00343 <span class="comment"></span><span class="comment">//</span> <a name="l00344"></a>00344 <span class="comment">// Thread</span> <a name="l00345"></a>00345 <span class="comment">//</span><span class="comment"></span> <a name="l00346"></a>00346 <span class="comment">///////////////////////////////////////////////////////////////////////////</span> <a name="l00347"></a>00347 <span class="comment"></span> <a name="l00348"></a><a class="code" href="classomni__thread.html">00348</a> <span class="keyword">class </span><a class="code" href="omnithread_8h.html#a2e26482727d49712e0fd18a61b962e30">_OMNITHREAD_NTDLL_</a> <a class="code" href="classomni__thread.html">omni_thread</a> { <a name="l00349"></a>00349 <a name="l00350"></a>00350 <span class="keyword">public</span>: <a name="l00351"></a>00351 <a name="l00352"></a><a class="code" href="classomni__thread.html#a2165b14568e7e8cc5c91f7a7bd2a07a3">00352</a> <span class="keyword">enum</span> <a class="code" href="classomni__thread.html#a2165b14568e7e8cc5c91f7a7bd2a07a3">priority_t</a> { <a name="l00353"></a><a class="code" href="classomni__thread.html#a2165b14568e7e8cc5c91f7a7bd2a07a3a31663aaf35eec6c9775761147925b1d2">00353</a> <a class="code" href="classomni__thread.html#a2165b14568e7e8cc5c91f7a7bd2a07a3a31663aaf35eec6c9775761147925b1d2">PRIORITY_LOW</a>, <a name="l00354"></a><a class="code" href="classomni__thread.html#a2165b14568e7e8cc5c91f7a7bd2a07a3a26dccf3e997f929779b2377972c23e7e">00354</a> <a class="code" href="classomni__thread.html#a2165b14568e7e8cc5c91f7a7bd2a07a3a26dccf3e997f929779b2377972c23e7e">PRIORITY_NORMAL</a>, <a name="l00355"></a><a class="code" href="classomni__thread.html#a2165b14568e7e8cc5c91f7a7bd2a07a3ae769531413a5f82697a261220adb2ab4">00355</a> PRIORITY_HIGH <a name="l00356"></a>00356 }; <a name="l00357"></a>00357 <a name="l00358"></a><a class="code" href="classomni__thread.html#a1e2a0832db9f46b12ed6cd676d2e5e29">00358</a> <span class="keyword">enum</span> <a class="code" href="classomni__thread.html#a1e2a0832db9f46b12ed6cd676d2e5e29">state_t</a> { <a name="l00359"></a><a class="code" href="classomni__thread.html#a1e2a0832db9f46b12ed6cd676d2e5e29a3507d36e8e924a25fc0514825926f533">00359</a> <a class="code" href="classomni__thread.html#a1e2a0832db9f46b12ed6cd676d2e5e29a3507d36e8e924a25fc0514825926f533">STATE_NEW</a>, <span class="comment">// thread object exists but thread hasn't</span> <a name="l00360"></a>00360 <span class="comment">// started yet.</span> <a name="l00361"></a><a class="code" href="classomni__thread.html#a1e2a0832db9f46b12ed6cd676d2e5e29ad5dff7c27e0305d26d84f9037eec0e7b">00361</a> <a class="code" href="classomni__thread.html#a1e2a0832db9f46b12ed6cd676d2e5e29ad5dff7c27e0305d26d84f9037eec0e7b">STATE_RUNNING</a>, <span class="comment">// thread is running.</span> <a name="l00362"></a><a class="code" href="classomni__thread.html#a1e2a0832db9f46b12ed6cd676d2e5e29a302285cf7ff9aedd24491c0b809e37ec">00362</a> STATE_TERMINATED <span class="comment">// thread has terminated but storage has not</span> <a name="l00363"></a>00363 <span class="comment">// been reclaimed (i.e. waiting to be joined).</span> <a name="l00364"></a>00364 }; <a name="l00365"></a>00365 <a name="l00366"></a>00366 <span class="comment">//</span> <a name="l00367"></a>00367 <span class="comment">// Constructors set up the thread object but the thread won't start until</span> <a name="l00368"></a>00368 <span class="comment">// start() is called. The create method can be used to construct and start</span> <a name="l00369"></a>00369 <span class="comment">// a thread in a single call.</span> <a name="l00370"></a>00370 <span class="comment">//</span> <a name="l00371"></a>00371 <a name="l00372"></a>00372 <a class="code" href="classomni__thread.html">omni_thread</a>(<span class="keywordtype">void</span> (*fn)(<span class="keywordtype">void</span>*), <span class="keywordtype">void</span>* arg = <a class="code" href="omnithread_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>, <a name="l00373"></a>00373 priority_t pri = PRIORITY_NORMAL); <a name="l00374"></a>00374 <a class="code" href="classomni__thread.html">omni_thread</a>(<span class="keywordtype">void</span>* (*fn)(<span class="keywordtype">void</span>*), <span class="keywordtype">void</span>* arg = <a class="code" href="omnithread_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>, <a name="l00375"></a>00375 priority_t pri = PRIORITY_NORMAL); <a name="l00376"></a>00376 <span class="comment">// these constructors create a thread which will run the given function</span> <a name="l00377"></a>00377 <span class="comment">// when start() is called. The thread will be detached if given a</span> <a name="l00378"></a>00378 <span class="comment">// function with void return type, undetached if given a function</span> <a name="l00379"></a>00379 <span class="comment">// returning void*. If a thread is detached, storage for the thread is</span> <a name="l00380"></a>00380 <span class="comment">// reclaimed automatically on termination. Only an undetached thread</span> <a name="l00381"></a>00381 <span class="comment">// can be joined.</span> <a name="l00382"></a>00382 <a name="l00383"></a>00383 <span class="keywordtype">void</span> start(<span class="keywordtype">void</span>); <a name="l00384"></a>00384 <span class="comment">// start() causes a thread created with one of the constructors to</span> <a name="l00385"></a>00385 <span class="comment">// start executing the appropriate function.</span> <a name="l00386"></a>00386 <a name="l00387"></a>00387 <span class="keyword">protected</span>: <a name="l00388"></a>00388 <a name="l00389"></a>00389 <a class="code" href="classomni__thread.html">omni_thread</a>(<span class="keywordtype">void</span>* arg = <a class="code" href="omnithread_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>, priority_t pri = PRIORITY_NORMAL); <a name="l00390"></a>00390 <span class="comment">// this constructor is used in a derived class. The thread will</span> <a name="l00391"></a>00391 <span class="comment">// execute the run() or run_undetached() member functions depending on</span> <a name="l00392"></a>00392 <span class="comment">// whether start() or start_undetached() is called respectively.</span> <a name="l00393"></a>00393 <a name="l00394"></a>00394 <span class="keyword">public</span>: <a name="l00395"></a>00395 <a name="l00396"></a>00396 <span class="keywordtype">void</span> start_undetached(<span class="keywordtype">void</span>); <a name="l00397"></a>00397 <span class="comment">// can be used with the above constructor in a derived class to cause</span> <a name="l00398"></a>00398 <span class="comment">// the thread to be undetached. In this case the thread executes the</span> <a name="l00399"></a>00399 <span class="comment">// run_undetached member function.</span> <a name="l00400"></a>00400 <a name="l00401"></a>00401 <span class="keyword">protected</span>: <a name="l00402"></a>00402 <a name="l00403"></a>00403 <span class="keyword">virtual</span> ~<a class="code" href="classomni__thread.html">omni_thread</a>(<span class="keywordtype">void</span>); <a name="l00404"></a>00404 <span class="comment">// destructor cannot be called by user (except via a derived class).</span> <a name="l00405"></a>00405 <span class="comment">// Use exit() or cancel() instead. This also means a thread object must</span> <a name="l00406"></a>00406 <span class="comment">// be allocated with new - it cannot be statically or automatically</span> <a name="l00407"></a>00407 <span class="comment">// allocated. The destructor of a class that inherits from omni_thread</span> <a name="l00408"></a>00408 <span class="comment">// shouldn't be public either (otherwise the thread object can be</span> <a name="l00409"></a>00409 <span class="comment">// destroyed while the underlying thread is still running).</span> <a name="l00410"></a>00410 <a name="l00411"></a>00411 <span class="keyword">public</span>: <a name="l00412"></a>00412 <a name="l00413"></a>00413 <span class="keywordtype">void</span> join(<span class="keywordtype">void</span>**); <a name="l00414"></a>00414 <span class="comment">// join causes the calling thread to wait for another's completion,</span> <a name="l00415"></a>00415 <span class="comment">// putting the return value in the variable of type void* whose address</span> <a name="l00416"></a>00416 <span class="comment">// is given (unless passed a null pointer). Only undetached threads</span> <a name="l00417"></a>00417 <span class="comment">// may be joined. Storage for the thread will be reclaimed.</span> <a name="l00418"></a>00418 <a name="l00419"></a>00419 <span class="keywordtype">void</span> set_priority(priority_t); <a name="l00420"></a>00420 <span class="comment">// set the priority of the thread.</span> <a name="l00421"></a>00421 <a name="l00422"></a>00422 <span class="keyword">static</span> <a class="code" href="classomni__thread.html">omni_thread</a>* create(<span class="keywordtype">void</span> (*fn)(<span class="keywordtype">void</span>*), <span class="keywordtype">void</span>* arg = <a class="code" href="omnithread_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>, <a name="l00423"></a>00423 priority_t pri = PRIORITY_NORMAL); <a name="l00424"></a>00424 <span class="keyword">static</span> <a class="code" href="classomni__thread.html">omni_thread</a>* create(<span class="keywordtype">void</span>* (*fn)(<span class="keywordtype">void</span>*), <span class="keywordtype">void</span>* arg = <a class="code" href="omnithread_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>, <a name="l00425"></a>00425 priority_t pri = PRIORITY_NORMAL); <a name="l00426"></a>00426 <span class="comment">// create spawns a new thread executing the given function with the</span> <a name="l00427"></a>00427 <span class="comment">// given argument at the given priority. Returns a pointer to the</span> <a name="l00428"></a>00428 <span class="comment">// thread object. It simply constructs a new thread object then calls</span> <a name="l00429"></a>00429 <span class="comment">// start.</span> <a name="l00430"></a>00430 <a name="l00431"></a>00431 <span class="keyword">static</span> <span class="keywordtype">void</span> exit(<span class="keywordtype">void</span>* return_value = <a class="code" href="omnithread_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>); <a name="l00432"></a>00432 <span class="comment">// causes the calling thread to terminate.</span> <a name="l00433"></a>00433 <a name="l00434"></a>00434 <span class="keyword">static</span> <a class="code" href="classomni__thread.html">omni_thread</a>* <span class="keyword">self</span>(void); <a name="l00435"></a>00435 <span class="comment">// returns the calling thread's omni_thread object. If the</span> <a name="l00436"></a>00436 <span class="comment">// calling thread is not the main thread and is not created</span> <a name="l00437"></a>00437 <span class="comment">// using this library, returns 0. (But see create_dummy()</span> <a name="l00438"></a>00438 <span class="comment">// below.)</span> <a name="l00439"></a>00439 <a name="l00440"></a>00440 <span class="keyword">static</span> <span class="keywordtype">void</span> yield(<span class="keywordtype">void</span>); <a name="l00441"></a>00441 <span class="comment">// allows another thread to run.</span> <a name="l00442"></a>00442 <a name="l00443"></a>00443 <span class="keyword">static</span> <span class="keywordtype">void</span> sleep(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> secs, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> nanosecs = 0); <a name="l00444"></a>00444 <span class="comment">// sleeps for the given time.</span> <a name="l00445"></a>00445 <a name="l00446"></a>00446 <span class="keyword">static</span> <span class="keywordtype">void</span> get_time(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>* abs_sec, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>* abs_nsec, <a name="l00447"></a>00447 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> rel_sec = 0, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> rel_nsec=0); <a name="l00448"></a>00448 <span class="comment">// calculates an absolute time in seconds and nanoseconds, suitable for</span> <a name="l00449"></a>00449 <span class="comment">// use in timed_waits on condition variables, which is the current time</span> <a name="l00450"></a>00450 <span class="comment">// plus the given relative offset.</span> <a name="l00451"></a>00451 <a name="l00452"></a>00452 <a name="l00453"></a>00453 <span class="keyword">static</span> <span class="keywordtype">void</span> stacksize(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> sz); <a name="l00454"></a>00454 <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> stacksize(); <a name="l00455"></a>00455 <span class="comment">// Use this value as the stack size when spawning a new thread.</span> <a name="l00456"></a>00456 <span class="comment">// The default value (0) means that the thread library default is</span> <a name="l00457"></a>00457 <span class="comment">// to be used.</span> <a name="l00458"></a>00458 <a name="l00459"></a>00459 <a name="l00460"></a>00460 <span class="comment">// Per-thread data</span> <a name="l00461"></a>00461 <span class="comment">//</span> <a name="l00462"></a>00462 <span class="comment">// These functions allow you to attach additional data to an</span> <a name="l00463"></a>00463 <span class="comment">// omni_thread. First allocate a key for yourself with</span> <a name="l00464"></a>00464 <span class="comment">// allocate_key(). Then you can store any object whose class is</span> <a name="l00465"></a>00465 <span class="comment">// derived from value_t. Any values still stored in the</span> <a name="l00466"></a>00466 <span class="comment">// omni_thread when the thread exits are deleted.</span> <a name="l00467"></a>00467 <span class="comment">//</span> <a name="l00468"></a>00468 <span class="comment">// These functions are NOT thread safe, so you should be very</span> <a name="l00469"></a>00469 <span class="comment">// careful about setting/getting data in a different thread to the</span> <a name="l00470"></a>00470 <span class="comment">// current thread.</span> <a name="l00471"></a>00471 <a name="l00472"></a><a class="code" href="classomni__thread.html#a0fa5931e90eb0a5f245152fdb246df88">00472</a> <span class="keyword">typedef</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="classomni__thread.html#a0fa5931e90eb0a5f245152fdb246df88">key_t</a>; <a name="l00473"></a>00473 <span class="keyword">static</span> <a class="code" href="classomni__thread.html#a0fa5931e90eb0a5f245152fdb246df88">key_t</a> allocate_key(); <a name="l00474"></a>00474 <a name="l00475"></a><a class="code" href="classomni__thread_1_1value__t.html">00475</a> <span class="keyword">class </span><a class="code" href="classomni__thread_1_1value__t.html">value_t</a> { <a name="l00476"></a>00476 <span class="keyword">public</span>: <a name="l00477"></a><a class="code" href="classomni__thread_1_1value__t.html#a40abd7accf262c6784ff66bd05a9cb62">00477</a> <span class="keyword">virtual</span> <a class="code" href="classomni__thread_1_1value__t.html#a40abd7accf262c6784ff66bd05a9cb62">~value_t</a>() {} <a name="l00478"></a>00478 }; <a name="l00479"></a>00479 <a name="l00480"></a>00480 value_t* set_value(key_t k, value_t* v); <a name="l00481"></a>00481 <span class="comment">// Sets a value associated with the given key. The key must</span> <a name="l00482"></a>00482 <span class="comment">// have been allocated with allocate_key(). If a value has</span> <a name="l00483"></a>00483 <span class="comment">// already been set with the specified key, the old value_t</span> <a name="l00484"></a>00484 <span class="comment">// object is deleted and replaced. Returns the value which was</span> <a name="l00485"></a>00485 <span class="comment">// set, or zero if the key is invalid.</span> <a name="l00486"></a>00486 <a name="l00487"></a>00487 value_t* get_value(key_t k); <a name="l00488"></a>00488 <span class="comment">// Returns the value associated with the key. If the key is</span> <a name="l00489"></a>00489 <span class="comment">// invalid, or there is no value for the key, returns zero.</span> <a name="l00490"></a>00490 <a name="l00491"></a>00491 value_t* remove_value(key_t k); <a name="l00492"></a>00492 <span class="comment">// Removes the value associated with the key and returns it.</span> <a name="l00493"></a>00493 <span class="comment">// If the key is invalid, or there is no value for the key,</span> <a name="l00494"></a>00494 <span class="comment">// returns zero.</span> <a name="l00495"></a>00495 <a name="l00496"></a>00496 <a name="l00497"></a>00497 <span class="comment">// Dummy omni_thread</span> <a name="l00498"></a>00498 <span class="comment">//</span> <a name="l00499"></a>00499 <span class="comment">// Sometimes, an application finds itself with threads created</span> <a name="l00500"></a>00500 <span class="comment">// outside of omnithread which must interact with omnithread</span> <a name="l00501"></a>00501 <span class="comment">// features such as the per-thread data. In this situation,</span> <a name="l00502"></a>00502 <span class="comment">// omni_thread::self() would normally return 0. These functions</span> <a name="l00503"></a>00503 <span class="comment">// allow the application to create a suitable dummy omni_thread</span> <a name="l00504"></a>00504 <span class="comment">// object.</span> <a name="l00505"></a>00505 <a name="l00506"></a>00506 <span class="keyword">static</span> <a class="code" href="classomni__thread.html">omni_thread</a>* create_dummy(<span class="keywordtype">void</span>); <a name="l00507"></a>00507 <span class="comment">// creates a dummy omni_thread for the calling thread. Future</span> <a name="l00508"></a>00508 <span class="comment">// calls to self() will return the dummy omni_thread. Throws</span> <a name="l00509"></a>00509 <span class="comment">// omni_thread_invalid if this thread already has an</span> <a name="l00510"></a>00510 <span class="comment">// associated omni_thread (real or dummy).</span> <a name="l00511"></a>00511 <a name="l00512"></a>00512 <span class="keyword">static</span> <span class="keywordtype">void</span> release_dummy(); <a name="l00513"></a>00513 <span class="comment">// release the dummy omni_thread for this thread. This</span> <a name="l00514"></a>00514 <span class="comment">// function MUST be called before the thread exits. Throws</span> <a name="l00515"></a>00515 <span class="comment">// omni_thread_invalid if the calling thread does not have a</span> <a name="l00516"></a>00516 <span class="comment">// dummy omni_thread.</span> <a name="l00517"></a>00517 <a name="l00518"></a>00518 <span class="comment">// class ensure_self should be created on the stack. If created in</span> <a name="l00519"></a>00519 <span class="comment">// a thread without an associated omni_thread, it creates a dummy</span> <a name="l00520"></a>00520 <span class="comment">// thread which is released when the ensure_self object is deleted.</span> <a name="l00521"></a>00521 <a name="l00522"></a><a class="code" href="classomni__thread_1_1ensure__self.html">00522</a> <span class="keyword">class </span><a class="code" href="classomni__thread_1_1ensure__self.html">ensure_self</a> { <a name="l00523"></a>00523 <span class="keyword">public</span>: <a name="l00524"></a><a class="code" href="classomni__thread_1_1ensure__self.html#a030b2eca318bdf7ea600c41c08a69bfb">00524</a> <span class="keyword">inline</span> <a class="code" href="classomni__thread_1_1ensure__self.html">ensure_self</a>() : _dummy(0) <a name="l00525"></a>00525 { <a name="l00526"></a>00526 _self = <a class="code" href="classomni__thread.html#a0c0cf663c8d81b989e8fab34df9e47f9">omni_thread::self</a>(); <a name="l00527"></a>00527 <span class="keywordflow">if</span> (!_self) { <a name="l00528"></a>00528 _dummy = 1; <a name="l00529"></a>00529 _self = <a class="code" href="classomni__thread.html#ac038d66148f9d7d132cae66635fa5583">omni_thread::create_dummy</a>(); <a name="l00530"></a>00530 } <a name="l00531"></a>00531 } <a name="l00532"></a><a class="code" href="classomni__thread_1_1ensure__self.html#ace8846114ba9d7c58cd98bd561be6eea">00532</a> <span class="keyword">inline</span> ~<a class="code" href="classomni__thread_1_1ensure__self.html">ensure_self</a>() <a name="l00533"></a>00533 { <a name="l00534"></a>00534 <span class="keywordflow">if</span> (_dummy) <a name="l00535"></a>00535 <a class="code" href="classomni__thread.html#a791a59c85d5ed29d5d73e2f09b524c69">omni_thread::release_dummy</a>(); <a name="l00536"></a>00536 } <a name="l00537"></a><a class="code" href="classomni__thread_1_1ensure__self.html#a56dfabd8abd408199b66779f9aeb7b98">00537</a> <span class="keyword">inline</span> <a class="code" href="classomni__thread.html">omni_thread</a>* <span class="keyword">self</span>() { <span class="keywordflow">return</span> _self; } <a name="l00538"></a>00538 <span class="keyword">private</span>: <a name="l00539"></a>00539 <a class="code" href="classomni__thread.html">omni_thread</a>* _self; <a name="l00540"></a>00540 <span class="keywordtype">int</span> _dummy; <a name="l00541"></a>00541 }; <a name="l00542"></a>00542 <a name="l00543"></a>00543 <a name="l00544"></a>00544 <span class="keyword">private</span>: <a name="l00545"></a>00545 <a name="l00546"></a>00546 <span class="keyword">virtual</span> <span class="keywordtype">void</span> run(<span class="keywordtype">void</span>* <span class="comment">/*arg*/</span>) {} <a name="l00547"></a>00547 <span class="keyword">virtual</span> <span class="keywordtype">void</span>* run_undetached(<span class="keywordtype">void</span>* <span class="comment">/*arg*/</span>) { <span class="keywordflow">return</span> <a class="code" href="omnithread_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>; } <a name="l00548"></a>00548 <span class="comment">// can be overridden in a derived class. When constructed using the</span> <a name="l00549"></a>00549 <span class="comment">// the constructor omni_thread(void*, priority_t), these functions are</span> <a name="l00550"></a>00550 <span class="comment">// called by start() and start_undetached() respectively.</span> <a name="l00551"></a>00551 <a name="l00552"></a>00552 <span class="keywordtype">void</span> common_constructor(<span class="keywordtype">void</span>* arg, priority_t pri, <span class="keywordtype">int</span> det); <a name="l00553"></a>00553 <span class="comment">// implements the common parts of the constructors.</span> <a name="l00554"></a>00554 <a name="l00555"></a>00555 <a class="code" href="classomni__mutex.html">omni_mutex</a> mutex; <a name="l00556"></a>00556 <span class="comment">// used to protect any members which can change after construction,</span> <a name="l00557"></a>00557 <span class="comment">// i.e. the following 2 members.</span> <a name="l00558"></a>00558 <a name="l00559"></a>00559 state_t _state; <a name="l00560"></a>00560 priority_t _priority; <a name="l00561"></a>00561 <a name="l00562"></a>00562 <span class="keyword">static</span> <a class="code" href="classomni__mutex.html">omni_mutex</a>* next_id_mutex; <a name="l00563"></a>00563 <span class="keyword">static</span> <span class="keywordtype">int</span> next_id; <a name="l00564"></a>00564 <span class="keywordtype">int</span> _id; <a name="l00565"></a>00565 <a name="l00566"></a>00566 void (*fn_void)(<span class="keywordtype">void</span>*); <a name="l00567"></a>00567 <span class="keywordtype">void</span>* (*fn_ret)(<span class="keywordtype">void</span>*); <a name="l00568"></a>00568 <span class="keywordtype">void</span>* thread_arg; <a name="l00569"></a>00569 <span class="keywordtype">int</span> detached; <a name="l00570"></a>00570 <span class="keywordtype">int</span> _dummy; <a name="l00571"></a>00571 value_t** _values; <a name="l00572"></a>00572 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> _value_alloc; <a name="l00573"></a>00573 <a name="l00574"></a>00574 <a class="code" href="classomni__thread.html">omni_thread</a>(<span class="keyword">const</span> <a class="code" href="classomni__thread.html">omni_thread</a>&); <a name="l00575"></a>00575 <a class="code" href="classomni__thread.html">omni_thread</a>& operator=(<span class="keyword">const</span> <a class="code" href="classomni__thread.html">omni_thread</a>&); <a name="l00576"></a>00576 <span class="comment">// Not implemented</span> <a name="l00577"></a>00577 <a name="l00578"></a>00578 <span class="keyword">public</span>: <a name="l00579"></a>00579 <a name="l00580"></a><a class="code" href="classomni__thread.html#a78942a0748869558ee516ab01591d74d">00580</a> <a class="code" href="classomni__thread.html#a2165b14568e7e8cc5c91f7a7bd2a07a3">priority_t</a> <a class="code" href="classomni__thread.html#a78942a0748869558ee516ab01591d74d">priority</a>(<span class="keywordtype">void</span>) { <a name="l00581"></a>00581 <a name="l00582"></a>00582 <span class="comment">// return this thread's priority.</span> <a name="l00583"></a>00583 <a name="l00584"></a>00584 <a class="code" href="classomni__mutex__lock.html">omni_mutex_lock</a> l(mutex); <a name="l00585"></a>00585 <span class="keywordflow">return</span> _priority; <a name="l00586"></a>00586 } <a name="l00587"></a>00587 <a name="l00588"></a><a class="code" href="classomni__thread.html#a4ec386ef03329c1e66e9ac51cc116d82">00588</a> <a class="code" href="classomni__thread.html#a1e2a0832db9f46b12ed6cd676d2e5e29">state_t</a> <a class="code" href="classomni__thread.html#a4ec386ef03329c1e66e9ac51cc116d82">state</a>(<span class="keywordtype">void</span>) { <a name="l00589"></a>00589 <a name="l00590"></a>00590 <span class="comment">// return thread state (invalid, new, running or terminated).</span> <a name="l00591"></a>00591 <a name="l00592"></a>00592 <a class="code" href="classomni__mutex__lock.html">omni_mutex_lock</a> l(mutex); <a name="l00593"></a>00593 <span class="keywordflow">return</span> _state; <a name="l00594"></a>00594 } <a name="l00595"></a>00595 <a name="l00596"></a><a class="code" href="classomni__thread.html#a1b0b87a8af2794ba9e5aac28e3d9073e">00596</a> <span class="keywordtype">int</span> <a class="code" href="classomni__thread.html#a1b0b87a8af2794ba9e5aac28e3d9073e">id</a>(<span class="keywordtype">void</span>) { <span class="keywordflow">return</span> _id; } <a name="l00597"></a>00597 <span class="comment">// return unique thread id within the current process.</span> <a name="l00598"></a>00598 <a name="l00599"></a>00599 <a name="l00600"></a>00600 <span class="comment">// This class plus the instance of it declared below allows us to execute</span> <a name="l00601"></a>00601 <span class="comment">// some initialisation code before main() is called.</span> <a name="l00602"></a>00602 <a name="l00603"></a><a class="code" href="classomni__thread_1_1init__t.html">00603</a> <span class="keyword">class </span><a class="code" href="omnithread_8h.html#a2e26482727d49712e0fd18a61b962e30">_OMNITHREAD_NTDLL_</a> <a class="code" href="classomni__thread_1_1init__t.html">init_t</a> { <a name="l00604"></a>00604 <span class="keyword">public</span>: <a name="l00605"></a>00605 <a class="code" href="classomni__thread_1_1init__t.html">init_t</a>(<span class="keywordtype">void</span>); <a name="l00606"></a>00606 ~<a class="code" href="classomni__thread_1_1init__t.html">init_t</a>(<span class="keywordtype">void</span>); <a name="l00607"></a>00607 }; <a name="l00608"></a>00608 <a name="l00609"></a><a class="code" href="classomni__thread.html#a33ba4052d5f5130d0b3f649a6d3af613">00609</a> <span class="keyword">friend</span> <span class="keyword">class </span><a class="code" href="classomni__thread_1_1init__t.html">init_t</a>; <a name="l00610"></a><a class="code" href="classomni__thread.html#a817539e9a93be72c8de8fd3fb00eb6ab">00610</a> <span class="keyword">friend</span> <span class="keyword">class </span>omni_thread_dummy; <a name="l00611"></a>00611 <a name="l00612"></a>00612 <a class="code" href="omnithread_8h.html#aa328f2be804b7cfb832792bb3ebc9f67">OMNI_THREAD_EXPOSE</a>: <a name="l00613"></a>00613 <a class="code" href="ot__mach_8h.html#acc85df821c0e286ac63c0198c691ddff">OMNI_THREAD_IMPLEMENTATION</a> <a name="l00614"></a>00614 }; <a name="l00615"></a>00615 <a name="l00616"></a>00616 <span class="preprocessor">#ifndef __rtems__</span> <a name="l00617"></a><a class="code" href="omnithread_8h.html#a74b439f3631000da8435dc94900ea982">00617</a> <span class="preprocessor"></span><span class="keyword">static</span> <a class="code" href="classomni__thread_1_1init__t.html">omni_thread::init_t</a> <a class="code" href="omnithread_8h.html#a74b439f3631000da8435dc94900ea982">omni_thread_init</a>; <a name="l00618"></a>00618 <span class="preprocessor">#else</span> <a name="l00619"></a>00619 <span class="preprocessor"></span><span class="comment">// RTEMS calls global Ctor/Dtor in a context that is not</span> <a name="l00620"></a>00620 <span class="comment">// a posix thread. Calls to functions to pthread_self() in</span> <a name="l00621"></a>00621 <span class="comment">// that context returns NULL. </span> <a name="l00622"></a>00622 <span class="comment">// So, for RTEMS we will make the thread initialization at the</span> <a name="l00623"></a>00623 <span class="comment">// beginning of the Init task that has a posix context.</span> <a name="l00624"></a>00624 <span class="preprocessor">#endif</span> <a name="l00625"></a>00625 <span class="preprocessor"></span> <a name="l00626"></a>00626 <span class="preprocessor">#endif</span> </pre></div></div> </div> <div id="nav-path" class="navpath"> <ul> <li class="navelem"><a class="el" href="omnithread_8h.html">omnithread.h</a> </li> <li class="footer">Generated on Thu Feb 17 2011 for GNU Radio 3.2.2 C++ API by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.3 </li> </ul> </div> </body> </html>