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

📄 thread_8h-source.html

📁 用来介绍ZIG Library游戏网络引擎的文档
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!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>thread.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>thread.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">        thread -- threading stuff</span>00023 <span class="comment"></span>00024 <span class="comment">*/</span>00025 00026 <span class="preprocessor">#ifndef _ZIG_HEADER_THREAD_H_</span>00027 <span class="preprocessor"></span><span class="preprocessor">#define _ZIG_HEADER_THREAD_H_</span>00028 <span class="preprocessor"></span>00029 <span class="preprocessor">#ifndef NO_DOXYGEN              // internal class -- do not document</span>00030 <span class="preprocessor"></span>00031 <span class="comment">// important: for ZIG_NO_PTHREADS</span>00032 <span class="preprocessor">#include "zigdefs.h"</span>00033 00034 <span class="comment">// ZIG 1.4.0: force use of hawkthreads APIs. this should stay UNDEFINED unless</span>00035 <span class="comment">// debugging, etc.</span>00036 <span class="comment">//#define USE_HAWKTHREADS</span>00037 00038 <span class="comment">// ZIG 1.4.0: use hawkthreads alternatives if no pthreads support</span>00039 <span class="preprocessor">#ifdef ZIG_NO_PTHREADS</span>00040 <span class="preprocessor"></span><span class="preprocessor">#define USE_HAWKTHREADS</span>00041 <span class="preprocessor"></span><span class="preprocessor">#endif</span>00042 <span class="preprocessor"></span>00043 <span class="comment">// ================================================</span>00044 <span class="comment">// our does-the-job-without-getting-in-your-way</span>00045 <span class="comment">// set of classes that wrap up the pthreads API</span>00046 <span class="comment">// ================================================</span>00047 00048 <span class="comment">//#include &lt;hawkthreads.h&gt;</span>00049 <span class="comment">//#include &lt;pthread.h&gt;</span>00050 00051 <span class="preprocessor">#ifdef USE_HAWKTHREADS</span>00052 <span class="preprocessor"></span><span class="preprocessor">#include &lt;hawkthreads.h&gt;</span>00053 <span class="preprocessor">#endif</span>00054 <span class="preprocessor"></span>00055 <span class="preprocessor">#ifndef ZIG_NO_PTHREADS</span>00056 <span class="preprocessor"></span><span class="preprocessor">#include &lt;pthread.h&gt;</span>00057 <span class="preprocessor">#include &lt;sched.h&gt;</span>00058 <span class="preprocessor">#endif</span>00059 <span class="preprocessor"></span>00060 <span class="preprocessor">#include "<a class="code" href="utils_8h.html">utils.h</a>"</span>00061 <span class="comment">//#include "console.h"</span>00062 00063 <span class="comment">// =========== STUFF NEEDED BEFORE MUTEX STUFF =======================================</span>00064 00065 <span class="keyword">class </span><a class="code" href="classconsole__c.html">console_c</a>;00066 00067 <span class="preprocessor">#ifdef USE_HAWKTHREADS</span>00068 <span class="preprocessor"></span><span class="preprocessor">#define THREAD_INVALID (HThreadID)HT_INVALID</span>00069 <span class="preprocessor"></span><span class="keyword">typedef</span> HThreadID thread_id_t;00070 <span class="preprocessor">#else</span>00071 <span class="preprocessor"></span><span class="comment">// an invalid thread (used -1 since there cannot be a pointer to negative memory address.</span>00072 <span class="comment">// also, -1 is 0xFFFFFFFFFFF...(whatever) so even if the pointer is taken to be</span>00073 <span class="comment">// unsigned address, there cannot be anything useful in the last addressable byte</span>00074 <span class="comment">// of memory :-)</span>00075 <span class="preprocessor">#define THREAD_INVALID (pthread_t)-1</span>00076 <span class="preprocessor"></span><span class="comment">// a thread id. pthread_t is actually a pointer to a struct full of details (so</span>00077 <span class="comment">// pthread_t is like FILE*) so it can be copied freely.</span>00078 <span class="keyword">typedef</span> pthread_t thread_id_t;00079 <span class="preprocessor">#endif</span>00080 <span class="preprocessor"></span>00081 <span class="comment">// get handle to current thread</span>00082 thread_id_t current_thread();00083 00084 <span class="comment">// compare two thread handles</span>00085 <span class="comment">// returns a non-zero value if t1 and t2 are equal; otherwise, zero is returned.</span>00086 <span class="comment">// (from pthread_equal())</span>00087 <span class="keywordtype">int</span> thread_equal(thread_id_t t1, thread_id_t t2);00088 00089 <span class="comment">//==================== MUTEX BEGIN ===================================================</span>00090 00095 <span class="keyword">class </span>mutex_c {00096 <span class="keyword">public</span>:00097 00099         mutex_c() {00100                 <span class="comment">//con = 0;</span>00101                 numlock = 0;00102                 owner = THREAD_INVALID;00103                 <span class="keywordtype">int</span> r;00104 <span class="preprocessor">#ifdef USE_HAWKTHREADS</span>00105 <span class="preprocessor"></span>                r = htMutexInit(&amp;m);00106                 assert(r == 0);00107                 r = htMutexInit(&amp;c);00108                 assert(r == 0);00109 <span class="preprocessor">#else</span>00110 <span class="preprocessor"></span>                r = pthread_mutex_init(&amp;m, 0);00111                 assert(r == 0);00112                 r = pthread_mutex_init(&amp;c, 0);00113                 assert(r == 0);00114 <span class="preprocessor">#endif</span>00115 <span class="preprocessor"></span>        }00116 00118         <span class="comment">//void set_console(console_c *conio) { con = conio; }</span>00119 00123         <span class="keywordtype">bool</span> lock() {00124                 <span class="comment">//pthread_t id = pthread_self();</span>00125                 thread_id_t id = current_thread();00126                  count_lock();00127                 <span class="comment">//if (owner != id) { // different thread. go ahead and try to lock it</span>00128                 <span class="keywordflow">if</span> (thread_equal(id, owner) == 0) {00129                          count_unlock();00130                         <span class="comment">//if (con) con-&gt;dprintf("MUTEX %i locking... owner %i\n", m, owner);</span>00131                         <span class="keywordtype">int</span> r;00132 <span class="preprocessor">#ifdef USE_HAWKTHREADS</span>00133 <span class="preprocessor"></span>                        r = htMutexLock(&amp;m);00134 <span class="preprocessor">#else</span>00135 <span class="preprocessor"></span>                        r = pthread_mutex_lock(&amp;m);00136 <span class="preprocessor">#endif</span>00137 <span class="preprocessor"></span>                        <span class="keywordflow">if</span> (r != 0) { 00138 <span class="comment">//                              if (con) </span>00139 <span class="comment">//                                      con-&gt;xprintf(5, "ERROR MUTEX %i LOCK == %i\n", m, r);</span>00140                                 <a class="code" href="utils_8h.html#a1">FAILSTOP</a>();  <span class="comment">// trap "can't lock" errors</span>00141                         }00142                         <span class="comment">//if (con) con-&gt;dprintf("MUTEX %i locked (%i)\n", m, r);</span>00143                          count_lock(); <span class="comment">// inside critical section - so set owner and numlock to 1</span>00144                         owner = id;00145                         numlock = 1;00146                          count_unlock();00147                         <span class="keywordflow">return</span> (r == 0);00148                 }00149                 <span class="keywordflow">else</span> {00150                         numlock++;        <span class="comment">// thread that already owns the lock. just increment the counter</span>00151                         <span class="keywordflow">if</span> (numlock &gt; 16)  00152                                 <a class="code" href="utils_8h.html#a1">FAILSTOP</a>();  <span class="comment">// FAIL - too many recursive locks, may be in a loop...</span>00153                          count_unlock();00154                         <span class="keywordflow">return</span> <span class="keyword">true</span>;00155                 }00156         }00157 00159         <span class="keywordtype">bool</span> unlock() {00160                 <span class="comment">//pthread_t id = pthread_self();</span>00161                 thread_id_t id = current_thread();00162                  count_lock();00163                 <span class="comment">//if (owner != id) {</span>00164                 <span class="keywordflow">if</span> (thread_equal(id, owner) == 0) {00165                          count_unlock();00166                         <a class="code" href="utils_8h.html#a1">FAILSTOP</a>();    <span class="comment">// FAIL - let's trap this</span>00167                         <span class="keywordflow">return</span> <span class="keyword">false</span>;                           <span class="comment">//it's not the thread that owns the lock! error!</span>00168                 }00169                 <span class="keywordflow">else</span> {00170                         <span class="keywordflow">if</span> (numlock &lt;= 0) {00171                                 <a class="code" href="utils_8h.html#a1">FAILSTOP</a>(); <span class="comment">// FAIL - unlocking more than locking!</span>00172                         }00173                         numlock--;      <span class="comment">// decrement lock count</span>00174                         <span class="keywordflow">if</span> (numlock == 0) {  <span class="comment">// no more lock counts - remove entry, do unlock</span>00175                                 owner = THREAD_INVALID; <span class="comment">// set owner to "nobody"</span>00176                                  count_unlock();00177                                 <span class="comment">//if (con) con-&gt;dprintf("MUTEX %i unlocking... owner %i\n", m, owner);</span>00178                                  <span class="keywordtype">int</span> r;00179 <span class="preprocessor">#ifdef USE_HAWKTHREADS</span>00180 <span class="preprocessor"></span>                        r = htMutexUnlock(&amp;m);00181 <span class="preprocessor">#else</span>00182 <span class="preprocessor"></span>                        r = pthread_mutex_unlock(&amp;m);

⌨️ 快捷键说明

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