⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mutex_8h-source.html

📁 用来介绍ZIG Library游戏网络引擎的文档
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>mutex.h Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.2.18 --><center><a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; </center><hr><h1>mutex.h</h1><div class="fragment"><pre>00001 <span class="comment">/*</span>00002 <span class="comment">    ZIG - An extendable, portable game engine focused on networking &amp; scripting</span>00003 <span class="comment">    Project Home: http://zige.sourceforge.net</span>00004 <span class="comment">    Copyright (C) 2002  F醔io Reis Cecin &lt;fcecin AT inf DOT ufrgs DOT br&gt;</span>00005 <span class="comment"></span>00006 <span class="comment">    This library is free software; you can redistribute it and/or</span>00007 <span class="comment">    modify it under the terms of the GNU Lesser General Public</span>00008 <span class="comment">    License as published by the Free Software Foundation; either</span>00009 <span class="comment">    version 2.1 of the License, or (at your option) any later version.</span>00010 <span class="comment"></span>00011 <span class="comment">    This library is distributed in the hope that it will be useful,</span>00012 <span class="comment">    but WITHOUT ANY WARRANTY; without even the implied warranty of</span>00013 <span class="comment">    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>00014 <span class="comment">    Lesser General Public License for more details.</span>00015 <span class="comment"></span>00016 <span class="comment">    You should have received a copy of the GNU Lesser General Public</span>00017 <span class="comment">    License along with this library; if not, write to the Free Software</span>00018 <span class="comment">    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   </span>00019 <span class="comment">*/</span>00020 <span class="comment">/*</span>00021 <span class="comment"></span>00022 <span class="comment">        mutex for threading</span>00023 <span class="comment"></span>00024 <span class="comment">        as of ZIG v1.4.0, this is not needed anymore (moved back to thread.h)</span>00025 <span class="comment"></span>00026 <span class="comment">*/</span>00027 <span class="comment">/*</span>00028 <span class="comment">#ifndef _ZIG_HEADER_MUTEX_H_</span>00029 <span class="comment">#define _ZIG_HEADER_MUTEX_H_</span>00030 <span class="comment"></span>00031 <span class="comment">#ifndef NO_DOXYGEN              // internal class -- do not document</span>00032 <span class="comment"></span>00033 <span class="comment">// ================================================</span>00034 <span class="comment">// our does-the-job-without-getting-in-your-way</span>00035 <span class="comment">// set of classes that wrap up the pthreads API</span>00036 <span class="comment">// ================================================</span>00037 <span class="comment"></span>00038 <span class="comment">// ZIG 1.4.0: this should stay #define'd -- switches the mutex code to use hawkthreads.h</span>00039 <span class="comment">// instead of pthread.h</span>00040 <span class="comment">//#define USE_HAWKTHREADS</span>00041 <span class="comment"></span>00042 <span class="comment">#ifdef USE_HAWKTHREADS</span>00043 <span class="comment">#include &lt;hawkthreads.h&gt;</span>00044 <span class="comment">#else</span>00045 <span class="comment">#include &lt;pthread.h&gt;</span>00046 <span class="comment">#endif</span>00047 <span class="comment"></span>00048 <span class="comment">#include "utils.h"</span>00049 <span class="comment"></span>00050 <span class="comment">class console_c;</span>00051 <span class="comment"></span>00056 <span class="comment">#ifdef USE_HAWKTHREADS</span>00057 <span class="comment">#define THREAD_INVALID (HThreadID)HT_INVALID</span>00058 <span class="comment">#else</span>00059 <span class="comment">#define THREAD_INVALID (pthread_t)-1</span>00060 <span class="comment">#endif</span>00061 <span class="comment"></span>00062 <span class="comment"></span>00067 <span class="comment">class mutex_c {</span>00068 <span class="comment">public:</span>00069 <span class="comment"></span>00071 <span class="comment">        mutex_c() {</span>00072 <span class="comment">                //con = 0;</span>00073 <span class="comment">                numlock = 0;</span>00074 <span class="comment">                owner = THREAD_INVALID;</span>00075 <span class="comment">                int r;</span>00076 <span class="comment">#ifdef USE_HAWKTHREADS</span>00077 <span class="comment">                r = htMutexInit(&amp;m);</span>00078 <span class="comment">                assert(r == 0);</span>00079 <span class="comment">                r = htMutexInit(&amp;c);</span>00080 <span class="comment">                assert(r == 0);</span>00081 <span class="comment">#else</span>00082 <span class="comment">                r = pthread_mutex_init(&amp;m, 0);</span>00083 <span class="comment">                assert(r == 0);</span>00084 <span class="comment">                r = pthread_mutex_init(&amp;c, 0);</span>00085 <span class="comment">                assert(r == 0);</span>00086 <span class="comment">#endif</span>00087 <span class="comment">        }</span>00088 <span class="comment"></span>00090 <span class="comment">        //void set_console(console_c *conio) { con = conio; }</span>00091 <span class="comment"></span>00095 <span class="comment">        bool lock() {</span>00096 <span class="comment">                pthread_t id = pthread_self();</span>00097 <span class="comment">                 count_lock();</span>00098 <span class="comment">                if (owner != id) { // different thread. go ahead and try to lock it</span>00099 <span class="comment">                         count_unlock();</span>00100 <span class="comment">                        //if (con) con-&gt;dprintf("MUTEX %i locking... owner %i\n", m, owner);</span>00101 <span class="comment">                        int r = pthread_mutex_lock(&amp;m);</span>00102 <span class="comment">                        if (r != 0) { </span>00103 <span class="comment">//                              if (con) </span>00104 <span class="comment">//                                      con-&gt;xprintf(5, "ERROR MUTEX %i LOCK == %i\n", m, r);</span>00105 <span class="comment">                                FAILSTOP();  // trap "can't lock" errors</span>00106 <span class="comment">                        }</span>00107 <span class="comment">                        //if (con) con-&gt;dprintf("MUTEX %i locked (%i)\n", m, r);</span>00108 <span class="comment">                         count_lock(); // inside critical section - so set owner and numlock to 1</span>00109 <span class="comment">                        owner = id;</span>00110 <span class="comment">                        numlock = 1;</span>00111 <span class="comment">                         count_unlock();</span>00112 <span class="comment">                        return (r == 0);</span>00113 <span class="comment">                }</span>00114 <span class="comment">                else {</span>00115 <span class="comment">                        numlock++;        // thread that already owns the lock. just increment the counter</span>00116 <span class="comment">                        if (numlock &gt; 16)  </span>00117 <span class="comment">                                FAILSTOP();  // FAIL - too many recursive locks, may be in a loop...</span>00118 <span class="comment">                         count_unlock();</span>00119 <span class="comment">                        return true;</span>00120 <span class="comment">                }</span>00121 <span class="comment">        }</span>00122 <span class="comment"></span>00124 <span class="comment">        bool unlock() {</span>00125 <span class="comment">                pthread_t id = pthread_self();</span>00126 <span class="comment">                 count_lock();</span>00127 <span class="comment">                if (owner != id) {</span>00128 <span class="comment">                         count_unlock();</span>00129 <span class="comment">                        FAILSTOP();    // FAIL - let's trap this</span>00130 <span class="comment">                        return false;                           //it's not the thread that owns the lock! error!</span>00131 <span class="comment">                }</span>00132 <span class="comment">                else {</span>00133 <span class="comment">                        if (numlock &lt;= 0) {</span>00134 <span class="comment">                                FAILSTOP(); // FAIL - unlocking more than locking!</span>00135 <span class="comment">                        }</span>00136 <span class="comment">                        numlock--;      // decrement lock count</span>00137 <span class="comment">                        if (numlock == 0) {  // no more lock counts - remove entry, do unlock</span>00138 <span class="comment">                                owner = THREAD_INVALID; // set owner to "nobody"</span>00139 <span class="comment">                                 count_unlock();</span>00140 <span class="comment">                                //if (con) con-&gt;dprintf("MUTEX %i unlocking... owner %i\n", m, owner);</span>00141 <span class="comment">                                int r = pthread_mutex_unlock(&amp;m);</span>00142 <span class="comment">                                if (r != 0) {</span>00143 <span class="comment">                                        FAILSTOP();  // trap "can't unlock" errors</span>00144 <span class="comment">//                                      if (con) </span>00145 <span class="comment">//                                              con-&gt;xprintf(5, "ERROR: MUTEX %i UNLOCK == %i\n", m, r);</span>00146 <span class="comment">                                }</span>00147 <span class="comment">                                //if (con) con-&gt;dprintf("MUTEX %i unlocked (%i)\n", m, r);</span>00148 <span class="comment">                                return (r == 0);</span>00149 <span class="comment">                        }</span>00150 <span class="comment">                        else {</span>00151 <span class="comment">                                count_unlock(); // do nothing, still more unlocks to go.</span>00152 <span class="comment">                                return true;</span>00153 <span class="comment">                        }</span>00154 <span class="comment">                }</span>00155 <span class="comment">        }</span>00156 <span class="comment"></span>00158 <span class="comment">        virtual ~mutex_c() {</span>00159 <span class="comment">                int r;</span>00160 <span class="comment">#ifdef USE_HAWKTHREADS</span>00161 <span class="comment">                r = htMutexDestroy(&amp;m);</span>00162 <span class="comment">                r = htMutexDestroy(&amp;c);</span>00163 <span class="comment">#else</span>00164 <span class="comment">                r = pthread_mutex_destroy(&amp;m);</span>00165 <span class="comment">                assert(r == 0);</span>00166 <span class="comment">                r = pthread_mutex_destroy(&amp;c);</span>00167 <span class="comment">                assert(r == 0);</span>00168 <span class="comment">#endif</span>00169 <span class="comment">        }</span>00170 <span class="comment"></span>00171 <span class="comment">protected:</span>00172 <span class="comment"></span>00173 <span class="comment">        void count_lock() { int r = pthread_mutex_lock(&amp;c); if (r != 0) FAILSTOP();     }</span>00174 <span class="comment">        void count_unlock() { int r = pthread_mutex_unlock(&amp;c); if (r != 0) FAILSTOP(); }</span>00175 <span class="comment">        pthread_t owner;        // thread that owns the mutex and is inside the critical section</span>00176 <span class="comment">        int numlock; // number of times the owner thread has locked</span>00177 <span class="comment">        console_c *con; //debugging...</span>00178 <span class="comment"></span>00179 <span class="comment">private:</span>00180 <span class="comment"></span>00181 <span class="comment">#ifdef USE_HAWKTHREADS</span>00182 <span class="comment">        HTmutex m;</span>00183 <span class="comment">        HTmutex c;</span>00184 <span class="comment">#else</span>00185 <span class="comment">        pthread_mutex_t m; //the mutex</span>00186 <span class="comment">        pthread_mutex_t c; //mutex for accessing the lock counter map</span>00187 <span class="comment">#endif</span>00188 <span class="comment"></span>00189 <span class="comment">        //non-copyable</span>00190 <span class="comment">        mutex_c(const mutex_c&amp;){}</span>00191 <span class="comment">        mutex_c &amp;operator=(const mutex_c&amp;);</span>00192 <span class="comment">};</span>00193 <span class="comment"></span>00194 <span class="comment"></span>00195 <span class="comment">#endif // NO_DOXYGEN</span>00196 <span class="comment"></span>00197 <span class="comment">#endif // _MUTEX_H_</span>00198 <span class="comment"></span>00199 <span class="comment"></span>00200 <span class="comment">*/</span></pre></div><hr><address style="align: right;"><small>Generated on Mon Jan 24 21:14:23 2005 for ZIG by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.18 </small></address></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -