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

📄 pasync.h.html

📁 PTypes是一个扩充了多线程和网络功能的STL库
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!--Syntax highlighting generated by Web C Plus Plus software v0.8.2Webcpp Copyright (C)2001, (C)2002, (C)2003 Jeffrey Bakker under the GNU GPLGet webcpp at http://webcpp.sf.net--><html><head><title>pasync.h</title><style type="text/css">/*Webcpp v0.8.1 compatible StyleSheethttp://webcpp.sf.netTheme: ide-msvcpp*/body{background-color: #ffffff}.webcpp a:link    {color:#000000}.webcpp a:visited {color:#008000}.webcpp a:active  {color:#0000ff}.webcpp a:hover   {color:#0000ff}.webcpp pre{color: #000000}.webcpp font{font-size:100%}.webcpp .symbols{color: #000000}.webcpp .preproc{color: #0000ff}.webcpp .integer{color: #000000}.webcpp .floatpt{color: #000000}.webcpp .dblquot{color: #000000}.webcpp .sinquot{color: #000000}.webcpp .keyword{color: #0000ff;}.webcpp .keytype{color: #0000ff;}.webcpp .comment{color: #008000;}</style></head><body bgcolor="#FFFFFF" leftmargin="40" marginwidth="40"> <p><a href="../htsrc.html">Index</a><hr noshade></p><div class="webcpp"><pre><font CLASS=preproc>#ifndef</font> __PASYNC_H__ <font CLASS=preproc>#define</font> __PASYNC_H__ <font CLASS=preproc>#ifdef</font> WIN32 <font CLASS=preproc>#</font>  define _WINSOCKAPI_   <font CLASS=comment>// prevent inclusion of winsock.h, since we need winsock2.h </font><font CLASS=preproc>#</font>  include &lt;windows.h&gt; <font CLASS=preproc>#else</font> <font CLASS=preproc>#</font>  include &lt;pthread.h&gt; <font CLASS=preproc>#</font>  ifndef __bsdi__ <font CLASS=preproc>#</font>    include &lt;semaphore.h&gt; <font CLASS=preproc>#</font>  endif <font CLASS=preproc>#endif</font> <font CLASS=preproc>#ifndef</font> __PPORT_H__ <font CLASS=preproc>#include</font> <font CLASS=dblquot>"pport.h"</font> <font CLASS=preproc>#endif</font> <font CLASS=preproc>#ifndef</font> __PTYPES_H__ <font CLASS=preproc>#include</font> <font CLASS=dblquot>"ptypes.h"</font> <font CLASS=preproc>#endif</font> PTYPES_BEGIN<font CLASS=comment>//</font><font CLASS=comment>//  Summary of implementation:</font><font CLASS=comment>//</font><font CLASS=comment>//  atomic increment/decrement/exchange</font><font CLASS=comment>//    MSVC/BCC/i386: internal, asm</font><font CLASS=comment>//    GCC/i386: internal, asm</font><font CLASS=comment>//    GCC/PowerPC: internal, asm</font><font CLASS=comment>//    Other: internal, mutex hash table</font><font CLASS=comment>//</font><font CLASS=comment>//  mutex</font><font CLASS=comment>//    Win32: Critical section</font><font CLASS=comment>//    Other: POSIX mutex</font><font CLASS=comment>//</font><font CLASS=comment>//  trigger</font><font CLASS=comment>//    Win32: Event</font><font CLASS=comment>//    Other: internal, POSIX condvar/mutex</font><font CLASS=comment>//</font><font CLASS=comment>//  rwlock:</font><font CLASS=comment>//    Win32: internal, Event/mutex</font><font CLASS=comment>//    MacOS: internal, POSIX condvar/mutex</font><font CLASS=comment>//    Other: POSIX rwlock</font><font CLASS=comment>//</font><font CLASS=comment>//  semaphore:</font><font CLASS=comment>//    Win32: = timedsem</font><font CLASS=comment>//    MacOS: = timedsem</font><font CLASS=comment>//    Other: POSIX semaphore</font><font CLASS=comment>//</font><font CLASS=comment>//  timedsem (with timed waiting):</font><font CLASS=comment>//    Win32: Semaphore</font><font CLASS=comment>//    Other: internal, POSIX mutex/condvar</font><font CLASS=comment>//</font><font CLASS=preproc>#ifdef</font> _MSC_VER <font CLASS=preproc>#pragma</font> pack(<font CLASS=keyword>push</font>, <font CLASS=integer>4</font>) <font CLASS=preproc>#endif</font> <font CLASS=preproc>#ifdef</font> WIN32    typedef <font CLASS=keyword>int</font> pthread_id_t<font CLASS=comment>;</font>   typedef HANDLE pthread_t<font CLASS=comment>;</font><font CLASS=preproc>#else</font>    typedef pthread_t pthread_id_t<font CLASS=comment>;</font><font CLASS=preproc>#endif</font> void psleep(uint milliseconds)<font CLASS=comment>;</font>bool pthrequal(pthread_id_t id)<font CLASS=comment>;  // note: this is NOT the thread handle, use thread::get_id()</font>pthread_id_t pthrself()<font CLASS=comment>;          // ... same</font><font CLASS=comment>// -------------------------------------------------------------------- //</font><font CLASS=comment>// --- mutex ---------------------------------------------------------- //</font><font CLASS=comment>// -------------------------------------------------------------------- //</font><font CLASS=preproc>#ifdef</font> WIN32 struct mutex: public noncopyable{<font CLASS=preproc>protected:</font>    CRITICAL_SECTION critsec<font CLASS=comment>;</font><font CLASS=preproc>public:</font>    mutex()         { InitializeCriticalSection(&amp<font CLASS=comment>;critsec); }</font>    ~mutex()        { DeleteCriticalSection(&amp;critsec); }    <font CLASS=keytype>void</font> enter()    { EnterCriticalSection(&amp;critsec); }    <font CLASS=keytype>void</font> leave()    { LeaveCriticalSection(&amp;critsec); }    <font CLASS=keytype>void</font> lock()     { enter(); }    <font CLASS=keytype>void</font> unlock()   { leave(); }};<font CLASS=preproc>#else</font> <font CLASS=keyword>struct</font> mutex: public noncopyable{<font CLASS=preproc><font CLASS=keyword>protected</font>:</font>    pthread_mutex_t mtx;<font CLASS=preproc><font CLASS=keyword>public</font>:</font>    mutex()         { pthread_mutex_init(&amp;mtx, <font CLASS=integer>0</font>); }    ~mutex()        { pthread_mutex_destroy(&amp;mtx); }    <font CLASS=keytype>void</font> enter()    { pthread_mutex_lock(&amp;mtx); }    <font CLASS=keytype>void</font> leave()    { pthread_mutex_unlock(&amp;mtx); }    <font CLASS=keytype>void</font> lock()     { enter(); }    <font CLASS=keytype>void</font> unlock()   { leave(); }};<font CLASS=preproc>#endif</font> <font CLASS=comment>//</font><font CLASS=comment>// scopelock</font><font CLASS=comment>//</font><font CLASS=keyword>class</font> scopelock: <font CLASS=keyword>public</font> noncopyable{<font CLASS=preproc><font CLASS=keyword>protected</font>:</font>    mutex* mtx;<font CLASS=preproc><font CLASS=keyword>public</font>:</font>    scopelock(mutex&amp; imtx): mtx(&amp;imtx)  { mtx<font CLASS=symbols>-</font>&gt;lock(); }    ~scopelock()  { mtx<font CLASS=symbols>-</font>&gt;unlock(); }};<font CLASS=comment>//</font><font CLASS=comment>// mutex table for hashed memory locking (undocumented)</font><font CLASS=comment>//</font><font CLASS=preproc>#define</font> _MUTEX_HASH_SIZE     <font CLASS=integer>29</font>      <font CLASS=comment>// a prime number for hashing </font><font CLASS=preproc>#ifdef</font> WIN32 <font CLASS=preproc>#</font>  define pmemlock        mutex <font CLASS=preproc>#</font>  define pmementer(m)    (m)<font CLASS=symbols>-</font>&gt;lock() <font CLASS=preproc>#</font>  define pmemleave(m)    (m)<font CLASS=symbols>-</font>&gt;unlock() <font CLASS=preproc>#else</font> <font CLASS=preproc>#</font>  define _MTX_INIT       PTHREAD_MUTEX_INITIALIZER <font CLASS=preproc>#</font>  define pmemlock        pthread_mutex_t <font CLASS=preproc>#</font>  define pmementer       pthread_mutex_lock <font CLASS=preproc>#</font>  define pmemleave       pthread_mutex_unlock <font CLASS=preproc>#endif</font> <font CLASS=keyword>extern</font> pmemlock _mtxtable[_MUTEX_HASH_SIZE];<font CLASS=preproc>#define</font> pgetmemlock(addr) (_mtxtable <font CLASS=symbols>+</font> pintptr(addr) % _MUTEX_HASH_SIZE) <font CLASS=comment>// -------------------------------------------------------------------- //</font><font CLASS=comment>// --- trigger -------------------------------------------------------- //</font><font CLASS=comment>// -------------------------------------------------------------------- //</font><font CLASS=preproc>#ifdef</font> WIN32 <font CLASS=keyword>class</font> trigger: public noncopyable{<font CLASS=preproc><font CLASS=keyword>protected</font>:</font>    HANDLE handle;      <font CLASS=comment>// Event object</font><font CLASS=preproc><font CLASS=keyword>public</font>:</font>    trigger(<font CLASS=keytype>bool</font> autoreset, <font CLASS=keytype>bool</font> state);    ~trigger()          { CloseHandle(handle); }    <font CLASS=keytype>void</font> wait()         { WaitForSingleObject(handle, INFINITE); }    <font CLASS=keytype>void</font> post()         { SetEvent(handle); }    <font CLASS=keytype>void</font> signal()       { post(); }    <font CLASS=keytype>void</font> reset()        { ResetEvent(handle); }};<font CLASS=preproc>#else</font> <font CLASS=keyword>class</font> trigger: public noncopyable{<font CLASS=preproc><font CLASS=keyword>protected</font>:</font>    pthread_mutex_t mtx;    pthread_cond_t cond;    <font CLASS=keytype>int</font> state;    <font CLASS=keytype>bool</font> autoreset;<font CLASS=preproc><font CLASS=keyword>public</font>:</font>    trigger(<font CLASS=keytype>bool</font> autoreset, <font CLASS=keytype>bool</font> state);    ~trigger();    <font CLASS=keytype>void</font> wait();    <font CLASS=keytype>void</font> post();    <font CLASS=keytype>void</font> signal()  { post(); }    <font CLASS=keytype>void</font> reset();};<font CLASS=preproc>#endif</font> <font CLASS=comment>// -------------------------------------------------------------------- //</font><font CLASS=comment>// --- rwlock --------------------------------------------------------- //</font><font CLASS=comment>// -------------------------------------------------------------------- //</font><font CLASS=preproc>#if</font> defined(WIN32) <font CLASS=symbols>||</font> defined(__DARWIN__) <font CLASS=symbols>||</font> defined(__bsdi__) <font CLASS=preproc>#</font>  define __PTYPES_RWLOCK__ <font CLASS=preproc>#elif</font> defined(linux)    <font CLASS=comment>// on Linux rwlocks are included only with -D_GNU_SOURCE.</font>   <font CLASS=comment>// programs that don't use rwlocks, do not need to define</font>   <font CLASS=comment>// _GNU_SOURCE either.</font><font CLASS=preproc>#</font>  <font CLASS=keyword>if</font> defined(_GNU_SOURCE) <font CLASS=symbols>||</font> defined(__USE_UNIX98) <font CLASS=preproc>#</font>    define __POSIX_RWLOCK__ <font CLASS=preproc>#</font>  endif <font CLASS=preproc>#else</font> <font CLASS=preproc>#</font>  define __POSIX_RWLOCK__ <font CLASS=preproc>#endif</font> <font CLASS=preproc>#ifdef</font> __PTYPES_RWLOCK__ <font CLASS=keyword>struct</font> rwlock: <font CLASS=keyword>protected</font> mutex{<font CLASS=preproc><font CLASS=keyword>protected</font>:</font><font CLASS=preproc>#ifdef</font> WIN32     HANDLE  reading;    <font CLASS=comment>// Event object</font>    HANDLE  finished;   <font CLASS=comment>// Event object</font>    <font CLASS=keytype>int</font>     readcnt;

⌨️ 快捷键说明

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