blob: a3161d8a3a4fce1375b1cf2e2f251bfdf9819a5f [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>spin_rw_mutex.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.7 -->
<div class="tabs">
<ul>
<li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li id="current"><a href="files.html"><span>Files</span></a></li>
<li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
</ul></div>
<div class="tabs">
<ul>
<li><a href="files.html"><span>File&nbsp;List</span></a></li>
<li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
</ul></div>
<h1>spin_rw_mutex.h</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> Copyright 2005-2010 Intel Corporation. All Rights Reserved.</span>
<a name="l00003"></a>00003 <span class="comment"></span>
<a name="l00004"></a>00004 <span class="comment"> The source code contained or described herein and all documents related</span>
<a name="l00005"></a>00005 <span class="comment"> to the source code ("Material") are owned by Intel Corporation or its</span>
<a name="l00006"></a>00006 <span class="comment"> suppliers or licensors. Title to the Material remains with Intel</span>
<a name="l00007"></a>00007 <span class="comment"> Corporation or its suppliers and licensors. The Material is protected</span>
<a name="l00008"></a>00008 <span class="comment"> by worldwide copyright laws and treaty provisions. No part of the</span>
<a name="l00009"></a>00009 <span class="comment"> Material may be used, copied, reproduced, modified, published, uploaded,</span>
<a name="l00010"></a>00010 <span class="comment"> posted, transmitted, distributed, or disclosed in any way without</span>
<a name="l00011"></a>00011 <span class="comment"> Intel's prior express written permission.</span>
<a name="l00012"></a>00012 <span class="comment"></span>
<a name="l00013"></a>00013 <span class="comment"> No license under any patent, copyright, trade secret or other</span>
<a name="l00014"></a>00014 <span class="comment"> intellectual property right is granted to or conferred upon you by</span>
<a name="l00015"></a>00015 <span class="comment"> disclosure or delivery of the Materials, either expressly, by</span>
<a name="l00016"></a>00016 <span class="comment"> implication, inducement, estoppel or otherwise. Any license under such</span>
<a name="l00017"></a>00017 <span class="comment"> intellectual property rights must be express and approved by Intel in</span>
<a name="l00018"></a>00018 <span class="comment"> writing.</span>
<a name="l00019"></a>00019 <span class="comment">*/</span>
<a name="l00020"></a>00020
<a name="l00021"></a>00021 <span class="preprocessor">#ifndef __TBB_spin_rw_mutex_H</span>
<a name="l00022"></a>00022 <span class="preprocessor"></span><span class="preprocessor">#define __TBB_spin_rw_mutex_H</span>
<a name="l00023"></a>00023 <span class="preprocessor"></span>
<a name="l00024"></a>00024 <span class="preprocessor">#include "tbb_stddef.h"</span>
<a name="l00025"></a>00025 <span class="preprocessor">#include "tbb_machine.h"</span>
<a name="l00026"></a>00026 <span class="preprocessor">#include "tbb_profiling.h"</span>
<a name="l00027"></a>00027
<a name="l00028"></a>00028 <span class="keyword">namespace </span>tbb {
<a name="l00029"></a>00029
<a name="l00030"></a>00030 <span class="keyword">class </span>spin_rw_mutex_v3;
<a name="l00031"></a>00031 <span class="keyword">typedef</span> spin_rw_mutex_v3 spin_rw_mutex;
<a name="l00032"></a>00032
<a name="l00034"></a>00034
<a name="l00035"></a><a class="code" href="a00196.html">00035</a> <span class="keyword">class </span><a class="code" href="a00196.html">spin_rw_mutex_v3</a> {
<a name="l00037"></a>00037
<a name="l00039"></a>00039 <span class="keywordtype">bool</span> __TBB_EXPORTED_METHOD internal_acquire_writer();
<a name="l00040"></a>00040
<a name="l00042"></a>00042
<a name="l00043"></a>00043 <span class="keywordtype">void</span> __TBB_EXPORTED_METHOD internal_release_writer();
<a name="l00044"></a>00044
<a name="l00046"></a>00046 <span class="keywordtype">void</span> __TBB_EXPORTED_METHOD internal_acquire_reader();
<a name="l00047"></a>00047
<a name="l00049"></a>00049 <span class="keywordtype">bool</span> __TBB_EXPORTED_METHOD internal_upgrade();
<a name="l00050"></a>00050
<a name="l00052"></a>00052
<a name="l00053"></a>00053 <span class="keywordtype">void</span> __TBB_EXPORTED_METHOD internal_downgrade();
<a name="l00054"></a>00054
<a name="l00056"></a>00056 <span class="keywordtype">void</span> __TBB_EXPORTED_METHOD internal_release_reader();
<a name="l00057"></a>00057
<a name="l00059"></a>00059 <span class="keywordtype">bool</span> __TBB_EXPORTED_METHOD internal_try_acquire_writer();
<a name="l00060"></a>00060
<a name="l00062"></a>00062 <span class="keywordtype">bool</span> __TBB_EXPORTED_METHOD internal_try_acquire_reader();
<a name="l00063"></a>00063
<a name="l00065"></a>00065 <span class="keyword">public</span>:
<a name="l00067"></a><a class="code" href="a00196.html#61332b2756de89f3f5f69310cbb6e70c">00067</a> <a class="code" href="a00196.html#61332b2756de89f3f5f69310cbb6e70c">spin_rw_mutex_v3</a>() : state(0) {
<a name="l00068"></a>00068 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS</span>
<a name="l00069"></a>00069 <span class="preprocessor"></span> internal_construct();
<a name="l00070"></a>00070 <span class="preprocessor">#endif</span>
<a name="l00071"></a>00071 <span class="preprocessor"></span> }
<a name="l00072"></a>00072
<a name="l00073"></a>00073 <span class="preprocessor">#if TBB_USE_ASSERT</span>
<a name="l00075"></a><a class="code" href="a00196.html#9a815fb2759e55072ed413f1b6970cf3">00075</a> <span class="preprocessor"> ~spin_rw_mutex_v3() {</span>
<a name="l00076"></a>00076 <span class="preprocessor"></span> __TBB_ASSERT( !state, <span class="stringliteral">"destruction of an acquired mutex"</span>);
<a name="l00077"></a>00077 };
<a name="l00078"></a>00078 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_ASSERT */</span>
<a name="l00079"></a>00079
<a name="l00081"></a>00081
<a name="l00083"></a><a class="code" href="a00197.html">00083</a> <span class="keyword">class </span><a class="code" href="a00197.html">scoped_lock</a> : internal::no_copy {
<a name="l00084"></a>00084 <span class="keyword">public</span>:
<a name="l00086"></a>00086
<a name="l00087"></a><a class="code" href="a00197.html#d6ea60dee5563f702379bf5e51aa8806">00087</a> <a class="code" href="a00197.html">scoped_lock</a>() : <a class="code" href="a00172.html">mutex</a>(NULL), is_writer(false) {}
<a name="l00088"></a>00088
<a name="l00090"></a><a class="code" href="a00197.html#42a92d4f8fdde425b111cfa8a9228071">00090</a> <a class="code" href="a00197.html">scoped_lock</a>( <a class="code" href="a00196.html">spin_rw_mutex</a>&amp; m, <span class="keywordtype">bool</span> write = <span class="keyword">true</span> ) : <a class="code" href="a00172.html">mutex</a>(NULL) {
<a name="l00091"></a>00091 <a class="code" href="a00267.html#a8686246bb5d3664bd07563749970fef5f1fafe8d229d348ff91d937f64e79c7">acquire</a>(m, write);
<a name="l00092"></a>00092 }
<a name="l00093"></a>00093
<a name="l00095"></a><a class="code" href="a00197.html#d7eaaa3f2e2c5dc11e7005811b1bdd04">00095</a> <a class="code" href="a00187.html#70246e0260493625ff956fa5926fc71f">~scoped_lock</a>() {
<a name="l00096"></a>00096 <span class="keywordflow">if</span>( <a class="code" href="a00172.html">mutex</a> ) <a class="code" href="a00267.html#a8686246bb5d3664bd07563749970fefaa1fa107db0245c41fb109d976ae8d70">release</a>();
<a name="l00097"></a>00097 }
<a name="l00098"></a>00098
<a name="l00100"></a><a class="code" href="a00197.html#b0b646ec5be02a127d159bbb7ca65353">00100</a> <span class="keywordtype">void</span> <a class="code" href="a00267.html#a8686246bb5d3664bd07563749970fef5f1fafe8d229d348ff91d937f64e79c7">acquire</a>( <a class="code" href="a00196.html">spin_rw_mutex</a>&amp; m, <span class="keywordtype">bool</span> write = <span class="keyword">true</span> ) {
<a name="l00101"></a>00101 __TBB_ASSERT( !<a class="code" href="a00172.html">mutex</a>, <span class="stringliteral">"holding mutex already"</span> );
<a name="l00102"></a>00102 is_writer = write;
<a name="l00103"></a>00103 <a class="code" href="a00172.html">mutex</a> = &amp;m;
<a name="l00104"></a>00104 <span class="keywordflow">if</span>( write ) <a class="code" href="a00172.html">mutex</a>-&gt;internal_acquire_writer();
<a name="l00105"></a>00105 <span class="keywordflow">else</span> <a class="code" href="a00172.html">mutex</a>-&gt;internal_acquire_reader();
<a name="l00106"></a>00106 }
<a name="l00107"></a>00107
<a name="l00109"></a>00109
<a name="l00110"></a><a class="code" href="a00197.html#3f0b1e3f2efab63336400348bd070226">00110</a> <span class="keywordtype">bool</span> upgrade_to_writer() {
<a name="l00111"></a>00111 __TBB_ASSERT( <a class="code" href="a00172.html">mutex</a>, <span class="stringliteral">"lock is not acquired"</span> );
<a name="l00112"></a>00112 __TBB_ASSERT( !is_writer, <span class="stringliteral">"not a reader"</span> );
<a name="l00113"></a>00113 is_writer = <span class="keyword">true</span>;
<a name="l00114"></a>00114 <span class="keywordflow">return</span> <a class="code" href="a00172.html">mutex</a>-&gt;internal_upgrade();
<a name="l00115"></a>00115 }
<a name="l00116"></a>00116
<a name="l00118"></a><a class="code" href="a00197.html#61b14d00a78185c9b2d206ebfc379124">00118</a> <span class="keywordtype">void</span> <a class="code" href="a00267.html#a8686246bb5d3664bd07563749970fefaa1fa107db0245c41fb109d976ae8d70">release</a>() {
<a name="l00119"></a>00119 __TBB_ASSERT( <a class="code" href="a00172.html">mutex</a>, <span class="stringliteral">"lock is not acquired"</span> );
<a name="l00120"></a>00120 <a class="code" href="a00196.html">spin_rw_mutex</a> *m = <a class="code" href="a00172.html">mutex</a>;
<a name="l00121"></a>00121 <a class="code" href="a00172.html">mutex</a> = NULL;
<a name="l00122"></a>00122 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT</span>
<a name="l00123"></a>00123 <span class="preprocessor"></span> <span class="keywordflow">if</span>( is_writer ) m-&gt;internal_release_writer();
<a name="l00124"></a>00124 <span class="keywordflow">else</span> m-&gt;internal_release_reader();
<a name="l00125"></a>00125 <span class="preprocessor">#else</span>
<a name="l00126"></a>00126 <span class="preprocessor"></span> <span class="keywordflow">if</span>( is_writer ) __TBB_AtomicAND( &amp;m-&gt;<a class="code" href="a00196.html#621b900d5fef23d2e401aabcbb777c76">state</a>, READERS );
<a name="l00127"></a>00127 <span class="keywordflow">else</span> __TBB_FetchAndAddWrelease( &amp;m-&gt;<a class="code" href="a00196.html#621b900d5fef23d2e401aabcbb777c76">state</a>, -(intptr_t)ONE_READER);
<a name="l00128"></a>00128 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */</span>
<a name="l00129"></a>00129 }
<a name="l00130"></a>00130
<a name="l00132"></a><a class="code" href="a00197.html#c2c2c38a08cb9080e87099fac3e5bc94">00132</a> <span class="keywordtype">bool</span> downgrade_to_reader() {
<a name="l00133"></a>00133 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT</span>
<a name="l00134"></a>00134 <span class="preprocessor"></span> __TBB_ASSERT( <a class="code" href="a00172.html">mutex</a>, <span class="stringliteral">"lock is not acquired"</span> );
<a name="l00135"></a>00135 __TBB_ASSERT( is_writer, <span class="stringliteral">"not a writer"</span> );
<a name="l00136"></a>00136 <a class="code" href="a00172.html">mutex</a>-&gt;internal_downgrade();
<a name="l00137"></a>00137 <span class="preprocessor">#else</span>
<a name="l00138"></a>00138 <span class="preprocessor"></span> __TBB_FetchAndAddW( &amp;<a class="code" href="a00172.html">mutex</a>-&gt;state, ((intptr_t)ONE_READER-WRITER));
<a name="l00139"></a>00139 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */</span>
<a name="l00140"></a>00140 is_writer = <span class="keyword">false</span>;
<a name="l00141"></a>00141
<a name="l00142"></a>00142 <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00143"></a>00143 }
<a name="l00144"></a>00144
<a name="l00146"></a><a class="code" href="a00197.html#9879626968d9b9a04cd2ec0fb2e84ae1">00146</a> <span class="keywordtype">bool</span> try_acquire( <a class="code" href="a00196.html">spin_rw_mutex</a>&amp; m, <span class="keywordtype">bool</span> write = <span class="keyword">true</span> ) {
<a name="l00147"></a>00147 __TBB_ASSERT( !<a class="code" href="a00172.html">mutex</a>, <span class="stringliteral">"holding mutex already"</span> );
<a name="l00148"></a>00148 <span class="keywordtype">bool</span> result;
<a name="l00149"></a>00149 is_writer = write;
<a name="l00150"></a>00150 result = write? m.internal_try_acquire_writer()
<a name="l00151"></a>00151 : m.internal_try_acquire_reader();
<a name="l00152"></a>00152 <span class="keywordflow">if</span>( result )
<a name="l00153"></a>00153 <a class="code" href="a00172.html">mutex</a> = &amp;m;
<a name="l00154"></a>00154 <span class="keywordflow">return</span> result;
<a name="l00155"></a>00155 }
<a name="l00156"></a>00156
<a name="l00157"></a>00157 <span class="keyword">private</span>:
<a name="l00159"></a>00159 <a class="code" href="a00196.html">spin_rw_mutex</a>* <a class="code" href="a00172.html">mutex</a>;
<a name="l00160"></a>00160
<a name="l00162"></a>00162
<a name="l00163"></a>00163 <span class="keywordtype">bool</span> is_writer;
<a name="l00164"></a>00164 };
<a name="l00165"></a>00165
<a name="l00166"></a>00166 <span class="comment">// Mutex traits</span>
<a name="l00167"></a>00167 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">bool</span> is_rw_mutex = <span class="keyword">true</span>;
<a name="l00168"></a>00168 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">bool</span> is_recursive_mutex = <span class="keyword">false</span>;
<a name="l00169"></a>00169 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">bool</span> is_fair_mutex = <span class="keyword">false</span>;
<a name="l00170"></a>00170
<a name="l00171"></a>00171 <span class="comment">// ISO C++0x compatibility methods</span>
<a name="l00172"></a>00172
<a name="l00174"></a><a class="code" href="a00196.html#4007d6e1523dbc3c2bb7f889ab789a8a">00174</a> <span class="keywordtype">void</span> lock() {internal_acquire_writer();}
<a name="l00175"></a>00175
<a name="l00177"></a>00177
<a name="l00178"></a><a class="code" href="a00196.html#088bb256be794cc47d3b83791632fdfc">00178</a> <span class="keywordtype">bool</span> try_lock() {<span class="keywordflow">return</span> internal_try_acquire_writer();}
<a name="l00179"></a>00179
<a name="l00181"></a><a class="code" href="a00196.html#f9f52ead2098eb5fb12da59d5ae53b55">00181</a> <span class="keywordtype">void</span> unlock() {
<a name="l00182"></a>00182 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT</span>
<a name="l00183"></a>00183 <span class="preprocessor"></span> <span class="keywordflow">if</span>( state&amp;WRITER ) internal_release_writer();
<a name="l00184"></a>00184 <span class="keywordflow">else</span> internal_release_reader();
<a name="l00185"></a>00185 <span class="preprocessor">#else</span>
<a name="l00186"></a>00186 <span class="preprocessor"></span> <span class="keywordflow">if</span>( state&amp;WRITER ) __TBB_AtomicAND( &amp;state, READERS );
<a name="l00187"></a>00187 <span class="keywordflow">else</span> __TBB_FetchAndAddWrelease( &amp;state, -(intptr_t)ONE_READER);
<a name="l00188"></a>00188 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */</span>
<a name="l00189"></a>00189 }
<a name="l00190"></a>00190
<a name="l00191"></a>00191 <span class="comment">// Methods for reader locks that resemble ISO C++0x compatibility methods.</span>
<a name="l00192"></a>00192
<a name="l00194"></a><a class="code" href="a00196.html#13f799708ac4ca437a16be202e263e18">00194</a> <span class="keywordtype">void</span> lock_read() {internal_acquire_reader();}
<a name="l00195"></a>00195
<a name="l00197"></a>00197
<a name="l00198"></a><a class="code" href="a00196.html#b8667415869013f840d976aa406d385a">00198</a> <span class="keywordtype">bool</span> try_lock_read() {<span class="keywordflow">return</span> internal_try_acquire_reader();}
<a name="l00199"></a>00199
<a name="l00200"></a>00200 <span class="keyword">private</span>:
<a name="l00201"></a>00201 <span class="keyword">typedef</span> intptr_t state_t;
<a name="l00202"></a>00202 <span class="keyword">static</span> <span class="keyword">const</span> state_t WRITER = 1;
<a name="l00203"></a>00203 <span class="keyword">static</span> <span class="keyword">const</span> state_t WRITER_PENDING = 2;
<a name="l00204"></a>00204 <span class="keyword">static</span> <span class="keyword">const</span> state_t READERS = ~(WRITER | WRITER_PENDING);
<a name="l00205"></a>00205 <span class="keyword">static</span> <span class="keyword">const</span> state_t ONE_READER = 4;
<a name="l00206"></a>00206 <span class="keyword">static</span> <span class="keyword">const</span> state_t BUSY = WRITER | READERS;
<a name="l00208"></a>00208
<a name="l00211"></a>00211 state_t state;
<a name="l00212"></a>00212
<a name="l00213"></a>00213 <span class="keywordtype">void</span> __TBB_EXPORTED_METHOD internal_construct();
<a name="l00214"></a>00214 };
<a name="l00215"></a>00215
<a name="l00216"></a>00216 __TBB_DEFINE_PROFILING_SET_NAME(spin_rw_mutex)
<a name="l00217"></a>00217
<a name="l00218"></a>00218 } <span class="comment">// namespace tbb</span>
<a name="l00219"></a>00219
<a name="l00220"></a>00220 <span class="preprocessor">#endif </span><span class="comment">/* __TBB_spin_rw_mutex_H */</span>
</pre></div><hr>
<p></p>
Copyright &copy; 2005-2010 Intel Corporation. All Rights Reserved.
<p></p>
Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are
registered trademarks or trademarks of Intel Corporation or its
subsidiaries in the United States and other countries.
<p></p>
* Other names and brands may be claimed as the property of others.