blob: 49e79a1e5e562e82eed13b18c4b2d93190d8fbae [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_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_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_mutex_H</span>
<a name="l00022"></a>00022 <span class="preprocessor"></span><span class="preprocessor">#define __TBB_spin_mutex_H</span>
<a name="l00023"></a>00023 <span class="preprocessor"></span>
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;cstddef&gt;</span>
<a name="l00025"></a>00025 <span class="preprocessor">#include &lt;new&gt;</span>
<a name="l00026"></a>00026 <span class="preprocessor">#include "aligned_space.h"</span>
<a name="l00027"></a>00027 <span class="preprocessor">#include "tbb_stddef.h"</span>
<a name="l00028"></a>00028 <span class="preprocessor">#include "tbb_machine.h"</span>
<a name="l00029"></a>00029 <span class="preprocessor">#include "tbb_profiling.h"</span>
<a name="l00030"></a>00030
<a name="l00031"></a>00031 <span class="keyword">namespace </span>tbb {
<a name="l00032"></a>00032
<a name="l00034"></a>00034
<a name="l00039"></a><a class="code" href="a00194.html">00039</a> <span class="keyword">class </span><a class="code" href="a00194.html">spin_mutex</a> {
<a name="l00041"></a>00041 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> flag;
<a name="l00042"></a>00042
<a name="l00043"></a>00043 <span class="keyword">public</span>:
<a name="l00045"></a>00045
<a name="l00046"></a><a class="code" href="a00194.html#3d8fb44644fd8d41ada1fbeba7409be3">00046</a> <a class="code" href="a00194.html#3d8fb44644fd8d41ada1fbeba7409be3">spin_mutex</a>() : flag(0) {
<a name="l00047"></a>00047 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS</span>
<a name="l00048"></a>00048 <span class="preprocessor"></span> internal_construct();
<a name="l00049"></a>00049 <span class="preprocessor">#endif</span>
<a name="l00050"></a>00050 <span class="preprocessor"></span> }
<a name="l00051"></a>00051
<a name="l00053"></a><a class="code" href="a00195.html">00053</a> <span class="keyword">class </span><a class="code" href="a00195.html">scoped_lock</a> : internal::no_copy {
<a name="l00054"></a>00054 <span class="keyword">private</span>:
<a name="l00056"></a>00056 <a class="code" href="a00194.html">spin_mutex</a>* my_mutex;
<a name="l00057"></a>00057
<a name="l00059"></a>00059 uintptr_t my_unlock_value;
<a name="l00060"></a>00060
<a name="l00062"></a>00062 <span class="keywordtype">void</span> __TBB_EXPORTED_METHOD internal_acquire( <a class="code" href="a00194.html">spin_mutex</a>&amp; m );
<a name="l00063"></a>00063
<a name="l00065"></a>00065 <span class="keywordtype">bool</span> __TBB_EXPORTED_METHOD internal_try_acquire( <a class="code" href="a00194.html">spin_mutex</a>&amp; m );
<a name="l00066"></a>00066
<a name="l00068"></a>00068 <span class="keywordtype">void</span> __TBB_EXPORTED_METHOD internal_release();
<a name="l00069"></a>00069
<a name="l00070"></a>00070 <span class="keyword">friend</span> <span class="keyword">class </span><a class="code" href="a00194.html">spin_mutex</a>;
<a name="l00071"></a>00071
<a name="l00072"></a>00072 <span class="keyword">public</span>:
<a name="l00074"></a><a class="code" href="a00195.html#29ae680ae7f5e685c2e15535b9c855b3">00074</a> <a class="code" href="a00195.html#29ae680ae7f5e685c2e15535b9c855b3">scoped_lock</a>() : my_mutex(NULL), my_unlock_value(0) {}
<a name="l00075"></a>00075
<a name="l00077"></a><a class="code" href="a00195.html#5ce6807050a9e8f87bcb4a65dccb12ef">00077</a> <a class="code" href="a00195.html#29ae680ae7f5e685c2e15535b9c855b3">scoped_lock</a>( <a class="code" href="a00194.html">spin_mutex</a>&amp; m ) {
<a name="l00078"></a>00078 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT</span>
<a name="l00079"></a>00079 <span class="preprocessor"></span> my_mutex=NULL;
<a name="l00080"></a>00080 internal_acquire(m);
<a name="l00081"></a>00081 <span class="preprocessor">#else</span>
<a name="l00082"></a>00082 <span class="preprocessor"></span> my_unlock_value = __TBB_LockByte(m.<a class="code" href="a00194.html#81b0b05fb71280ce0c79708e82569d86">flag</a>);
<a name="l00083"></a>00083 my_mutex=&amp;m;
<a name="l00084"></a>00084 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/</span>
<a name="l00085"></a>00085 }
<a name="l00086"></a>00086
<a name="l00088"></a><a class="code" href="a00195.html#3ee3c338732b1f64b0b32a757807a30d">00088</a> <span class="keywordtype">void</span> <a class="code" href="a00195.html#3ee3c338732b1f64b0b32a757807a30d">acquire</a>( <a class="code" href="a00194.html">spin_mutex</a>&amp; m ) {
<a name="l00089"></a>00089 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT</span>
<a name="l00090"></a>00090 <span class="preprocessor"></span> internal_acquire(m);
<a name="l00091"></a>00091 <span class="preprocessor">#else</span>
<a name="l00092"></a>00092 <span class="preprocessor"></span> my_unlock_value = __TBB_LockByte(m.<a class="code" href="a00194.html#81b0b05fb71280ce0c79708e82569d86">flag</a>);
<a name="l00093"></a>00093 my_mutex = &amp;m;
<a name="l00094"></a>00094 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/</span>
<a name="l00095"></a>00095 }
<a name="l00096"></a>00096
<a name="l00098"></a>00098
<a name="l00099"></a><a class="code" href="a00195.html#9297ec188534b45dc0ca48f2f39a0501">00099</a> <span class="keywordtype">bool</span> <a class="code" href="a00195.html#9297ec188534b45dc0ca48f2f39a0501">try_acquire</a>( <a class="code" href="a00194.html">spin_mutex</a>&amp; m ) {
<a name="l00100"></a>00100 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT</span>
<a name="l00101"></a>00101 <span class="preprocessor"></span> <span class="keywordflow">return</span> internal_try_acquire(m);
<a name="l00102"></a>00102 <span class="preprocessor">#else</span>
<a name="l00103"></a>00103 <span class="preprocessor"></span> <span class="keywordtype">bool</span> result = __TBB_TryLockByte(m.<a class="code" href="a00194.html#81b0b05fb71280ce0c79708e82569d86">flag</a>);
<a name="l00104"></a>00104 <span class="keywordflow">if</span>( result ) {
<a name="l00105"></a>00105 my_unlock_value = 0;
<a name="l00106"></a>00106 my_mutex = &amp;m;
<a name="l00107"></a>00107 }
<a name="l00108"></a>00108 <span class="keywordflow">return</span> result;
<a name="l00109"></a>00109 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/</span>
<a name="l00110"></a>00110 }
<a name="l00111"></a>00111
<a name="l00113"></a><a class="code" href="a00195.html#eeb615e68e963e6bf8d9c11402d0ce8e">00113</a> <span class="keywordtype">void</span> <a class="code" href="a00195.html#eeb615e68e963e6bf8d9c11402d0ce8e">release</a>() {
<a name="l00114"></a>00114 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT</span>
<a name="l00115"></a>00115 <span class="preprocessor"></span> internal_release();
<a name="l00116"></a>00116 <span class="preprocessor">#else</span>
<a name="l00117"></a>00117 <span class="preprocessor"></span> __TBB_store_with_release(my_mutex-&gt;<a class="code" href="a00194.html#81b0b05fb71280ce0c79708e82569d86">flag</a>, static_cast&lt;unsigned char&gt;(my_unlock_value));
<a name="l00118"></a>00118 my_mutex = NULL;
<a name="l00119"></a>00119 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */</span>
<a name="l00120"></a>00120 }
<a name="l00121"></a>00121
<a name="l00123"></a><a class="code" href="a00195.html#ac6fa425d1f06c56d8b70abc51aac844">00123</a> <a class="code" href="a00195.html#ac6fa425d1f06c56d8b70abc51aac844">~scoped_lock</a>() {
<a name="l00124"></a>00124 <span class="keywordflow">if</span>( my_mutex ) {
<a name="l00125"></a>00125 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT</span>
<a name="l00126"></a>00126 <span class="preprocessor"></span> internal_release();
<a name="l00127"></a>00127 <span class="preprocessor">#else</span>
<a name="l00128"></a>00128 <span class="preprocessor"></span> __TBB_store_with_release(my_mutex-&gt;<a class="code" href="a00194.html#81b0b05fb71280ce0c79708e82569d86">flag</a>, static_cast&lt;unsigned char&gt;(my_unlock_value));
<a name="l00129"></a>00129 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */</span>
<a name="l00130"></a>00130 }
<a name="l00131"></a>00131 }
<a name="l00132"></a>00132 };
<a name="l00133"></a>00133
<a name="l00134"></a>00134 <span class="keywordtype">void</span> __TBB_EXPORTED_METHOD internal_construct();
<a name="l00135"></a>00135
<a name="l00136"></a>00136 <span class="comment">// Mutex traits</span>
<a name="l00137"></a>00137 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">bool</span> is_rw_mutex = <span class="keyword">false</span>;
<a name="l00138"></a>00138 <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="l00139"></a>00139 <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="l00140"></a>00140
<a name="l00141"></a>00141 <span class="comment">// ISO C++0x compatibility methods</span>
<a name="l00142"></a>00142
<a name="l00144"></a><a class="code" href="a00194.html#4f748989e19b6045e3a2d2ee73626a28">00144</a> <span class="keywordtype">void</span> <a class="code" href="a00194.html#4f748989e19b6045e3a2d2ee73626a28">lock</a>() {
<a name="l00145"></a>00145 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS</span>
<a name="l00146"></a>00146 <span class="preprocessor"></span> <a class="code" href="a00142.html">aligned_space&lt;scoped_lock,1&gt;</a> tmp;
<a name="l00147"></a>00147 <span class="keyword">new</span>(tmp.<a class="code" href="a00142.html#0d702fc6b9e9d061ace3501b3c861cdf">begin</a>()) <a class="code" href="a00195.html">scoped_lock</a>(*<span class="keyword">this</span>);
<a name="l00148"></a>00148 <span class="preprocessor">#else</span>
<a name="l00149"></a>00149 <span class="preprocessor"></span> __TBB_LockByte(flag);
<a name="l00150"></a>00150 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_THREADING_TOOLS*/</span>
<a name="l00151"></a>00151 }
<a name="l00152"></a>00152
<a name="l00154"></a>00154
<a name="l00155"></a><a class="code" href="a00194.html#8f9a58fb56a2b4c5efe1a7f7c1ae2074">00155</a> <span class="keywordtype">bool</span> <a class="code" href="a00194.html#8f9a58fb56a2b4c5efe1a7f7c1ae2074">try_lock</a>() {
<a name="l00156"></a>00156 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS</span>
<a name="l00157"></a>00157 <span class="preprocessor"></span> <a class="code" href="a00142.html">aligned_space&lt;scoped_lock,1&gt;</a> tmp;
<a name="l00158"></a>00158 <span class="keywordflow">return</span> (<span class="keyword">new</span>(tmp.<a class="code" href="a00142.html#0d702fc6b9e9d061ace3501b3c861cdf">begin</a>()) <a class="code" href="a00195.html">scoped_lock</a>)-&gt;internal_try_acquire(*<span class="keyword">this</span>);
<a name="l00159"></a>00159 <span class="preprocessor">#else</span>
<a name="l00160"></a>00160 <span class="preprocessor"></span> <span class="keywordflow">return</span> __TBB_TryLockByte(flag);
<a name="l00161"></a>00161 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_THREADING_TOOLS*/</span>
<a name="l00162"></a>00162 }
<a name="l00163"></a>00163
<a name="l00165"></a><a class="code" href="a00194.html#0e843ee6265f57f27d228ba91e7308ef">00165</a> <span class="keywordtype">void</span> <a class="code" href="a00194.html#0e843ee6265f57f27d228ba91e7308ef">unlock</a>() {
<a name="l00166"></a>00166 <span class="preprocessor">#if TBB_USE_THREADING_TOOLS</span>
<a name="l00167"></a>00167 <span class="preprocessor"></span> <a class="code" href="a00142.html">aligned_space&lt;scoped_lock,1&gt;</a> tmp;
<a name="l00168"></a>00168 <a class="code" href="a00195.html">scoped_lock</a>&amp; s = *tmp.<a class="code" href="a00142.html#0d702fc6b9e9d061ace3501b3c861cdf">begin</a>();
<a name="l00169"></a>00169 s.<a class="code" href="a00195.html#d968921bed018503214f36e09007ee7b">my_mutex</a> = <span class="keyword">this</span>;
<a name="l00170"></a>00170 s.<a class="code" href="a00195.html#43a6f8977cd1ed2752f417f3ad9fc4af">my_unlock_value</a> = 0;
<a name="l00171"></a>00171 s.<a class="code" href="a00195.html#8f4d19aa2d6d112034d281eed0dab5fa">internal_release</a>();
<a name="l00172"></a>00172 <span class="preprocessor">#else</span>
<a name="l00173"></a>00173 <span class="preprocessor"></span> __TBB_store_with_release(flag, 0);
<a name="l00174"></a>00174 <span class="preprocessor">#endif </span><span class="comment">/* TBB_USE_THREADING_TOOLS */</span>
<a name="l00175"></a>00175 }
<a name="l00176"></a>00176
<a name="l00177"></a>00177 <span class="keyword">friend</span> <span class="keyword">class </span>scoped_lock;
<a name="l00178"></a>00178 };
<a name="l00179"></a>00179
<a name="l00180"></a>00180 __TBB_DEFINE_PROFILING_SET_NAME(spin_mutex)
<a name="l00181"></a>00181
<a name="l00182"></a>00182 } <span class="comment">// namespace tbb</span>
<a name="l00183"></a>00183
<a name="l00184"></a>00184 <span class="preprocessor">#endif </span><span class="comment">/* __TBB_spin_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.