This commit delivers a comprehensive set of updates including development
environment setup, advanced spinlock features, initial IPC robustness
KASSERTs, extensive Doxygen commenting, code formatting, and Doxygen setup.
Key Changes:
Part 0: Development Environment Setup
- Created `setup.sh` to automate installation of `doxygen`, `graphviz`,
`clang-format`, and `cppcheck`. (Tools were installed in the environment).
Part 1: Advanced Spinlock Features
- Enhanced `simple_spin_lock()` in `kernel/k_spinlock.h` with:
- Adaptive spinning: `MAX_SPIN_THRESHOLD` and a stubbed `kernel_yield()`
(calling `arch_pause()`).
- Contention statistics: `acquisitions` and `contentions` counters
added to `simple_spinlock_t` and integrated into lock functions.
- Added extensive Doxygen and inline comments for all spinlock code.
- Updated `docs/Signal_Refactoring_Verification.md` with these features.
Part 2: Initial IPC Robustness Analysis & KASSERTs
- Implemented an initial set of KASSERTs in `kernel/system.c` (in
`kernel_call`, `kernel_call_dispatch`, `kernel_call_finish`) for
validating IPC message parameters, call numbers, privileges, and
internal states.
- Added Doxygen/inline comments to these IPC functions.
- Created `docs/IPC_Robustness_Analysis.md` documenting these KASSERTs
and areas for further IPC validation.
Part 3: Code Formatting, Doxygen Setup & Review
- Code Formatting: Applied `clang-format --style=Google` to all C/H
files modified in recent KASSERT and spinlock work.
- Static Analysis: Ran `cppcheck`; no critical issues found in recent
changes requiring immediate code modification.
- Doxygen Setup: Created `docs/Doxyfile.kernel` with a comprehensive,
C23-aware configuration based on your feedback. This file enables
generation of extensive kernel documentation.
- `docs/Lock_Ordering.md`: Reviewed; no updates needed in this pass.
This work significantly improves kernel robustness, developer tooling,
code quality, and documentation infrastructure.
221 lines
28 KiB
HTML
221 lines
28 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.9.8"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>MINIX Kernel Documentation: minix/kernel/k_spinlock.h Source File</title>
|
|
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="jquery.js"></script>
|
|
<script type="text/javascript" src="dynsections.js"></script>
|
|
<link href="search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="search/searchdata.js"></script>
|
|
<script type="text/javascript" src="search/search.js"></script>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr id="projectrow">
|
|
<td id="projectalign">
|
|
<div id="projectname">MINIX Kernel Documentation
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.9.8 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "search/",'.html');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="menudata.js"></script>
|
|
<script type="text/javascript" src="menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function() {
|
|
initMenu('',true,false,'search.php','Search');
|
|
$(document).ready(function() { init_search(); });
|
|
});
|
|
/* @license-end */
|
|
</script>
|
|
<div id="main-nav"></div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(document).ready(function() { init_codefold(0); });
|
|
/* @license-end */
|
|
</script>
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<div id="MSearchResults">
|
|
<div class="SRPage">
|
|
<div id="SRIndex">
|
|
<div id="SRResults"></div>
|
|
<div class="SRStatus" id="Loading">Loading...</div>
|
|
<div class="SRStatus" id="Searching">Searching...</div>
|
|
<div class="SRStatus" id="NoMatches">No Matches</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="nav-path" class="navpath">
|
|
<ul>
|
|
<li class="navelem"><a class="el" href="dir_207bf8c12f5ffd71867b262b7e0c4f41.html">minix</a></li><li class="navelem"><a class="el" href="dir_a593f8436b1bc29ffa99b7ee170e7ac1.html">kernel</a></li> </ul>
|
|
</div>
|
|
</div><!-- top -->
|
|
<div class="header">
|
|
<div class="headertitle"><div class="title">k_spinlock.h</div></div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<a href="k__spinlock_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno"> 1</span> </div>
|
|
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno"> 13</span><span class="preprocessor">#ifndef K_SPINLOCK_H</span></div>
|
|
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno"> 14</span><span class="preprocessor">#define K_SPINLOCK_H</span></div>
|
|
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno"> 15</span> </div>
|
|
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno"> 16</span><span class="preprocessor">#include <minix/sys_config.h></span> <span class="comment">/* For potential CONFIG_SMP or other system configs */</span></div>
|
|
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno"> 17</span> </div>
|
|
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno"> 18</span><span class="comment">/* Include arch-specific definitions, e.g., for arch_pause() */</span></div>
|
|
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno"> 19</span><span class="preprocessor">#if defined(__i386__) || defined(__x86_64__)</span></div>
|
|
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno"> 20</span><span class="preprocessor">#include "arch/i386/include/arch_cpu.h"</span> <span class="comment">// Provides arch_pause for x86</span></div>
|
|
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno"> 21</span><span class="preprocessor">#else</span></div>
|
|
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno"><a class="line" href="k__spinlock_8h.html#a79887c626e823a36834e349fb75c539c"> 29</a></span><span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code hl_function" href="k__spinlock_8h.html#a79887c626e823a36834e349fb75c539c">arch_pause</a>(<span class="keywordtype">void</span>) { <span class="comment">/* No-op */</span> }</div>
|
|
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno"> 30</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno"> 31</span> </div>
|
|
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno"><a class="line" href="k__spinlock_8h.html#ab64669b95d563f14428a1f073106ef04"> 41</a></span><span class="preprocessor">#define MAX_SPIN_THRESHOLD 100000</span></div>
|
|
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno"> 42</span> </div>
|
|
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno"> 43</span><span class="preprocessor">#ifndef KERNEL_YIELD_DEFINED</span></div>
|
|
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno"><a class="line" href="k__spinlock_8h.html#ab7e692108b27a8b15089a297b451f293"> 44</a></span><span class="preprocessor">#define KERNEL_YIELD_DEFINED</span></div>
|
|
<div class="foldopen" id="foldopen00057" data-start="{" data-end="}">
|
|
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno"><a class="line" href="k__spinlock_8h.html#a3b96827abeb83529b5d946e2654231ed"> 57</a></span><span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code hl_function" href="k__spinlock_8h.html#a3b96827abeb83529b5d946e2654231ed">kernel_yield</a>(<span class="keywordtype">void</span>) {</div>
|
|
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno"> 58</span> <span class="comment">/* Placeholder for actual yield. On x86, 'rep nop' is sometimes used</span></div>
|
|
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span><span class="comment"> * as a more potent pause than single 'pause', or actual scheduler yield.</span></div>
|
|
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno"> 60</span><span class="comment"> * For now, this can be a kprintf_stub for debugging or just a comment.</span></div>
|
|
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"> 61</span><span class="comment"> * A true yield would involve scheduler interaction.</span></div>
|
|
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"> 63</span> <span class="comment">// kprintf_stub("kernel_yield() called (stub)\n"); // Uncomment for debugging</span></div>
|
|
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno"> 64</span> <span class="comment">// yield calls</span></div>
|
|
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno"> 65</span> <a class="code hl_function" href="k__spinlock_8h.html#a79887c626e823a36834e349fb75c539c">arch_pause</a>(); <span class="comment">// At least do an arch_pause if yielding fully is complex.</span></div>
|
|
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno"> 66</span>}</div>
|
|
</div>
|
|
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span> </div>
|
|
<div class="foldopen" id="foldopen00077" data-start="{" data-end="};">
|
|
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno"><a class="line" href="structsimple__spinlock__t.html"> 77</a></span><span class="keyword">typedef</span> <span class="keyword">struct </span>{</div>
|
|
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno"><a class="line" href="structsimple__spinlock__t.html#ae8d529ab0ac1b69010d98ef8336e9172"> 86</a></span> <span class="keyword">volatile</span> <span class="keywordtype">int</span> <a class="code hl_variable" href="structsimple__spinlock__t.html#ae8d529ab0ac1b69010d98ef8336e9172">locked</a>;</div>
|
|
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno"><a class="line" href="structsimple__spinlock__t.html#a849f8cded125ee7192052ca71e67f390"> 88</a></span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> <a class="code hl_variable" href="structsimple__spinlock__t.html#a849f8cded125ee7192052ca71e67f390">acquisitions</a>;</div>
|
|
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno"><a class="line" href="structsimple__spinlock__t.html#a7c66988c5d374d8eaf3b522a1ed7d041"> 91</a></span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> <a class="code hl_variable" href="structsimple__spinlock__t.html#a7c66988c5d374d8eaf3b522a1ed7d041">contentions</a>;</div>
|
|
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno"> 92</span> <span class="comment">/* Future: unsigned long total_spin_cycles; // Could be added with more</span></div>
|
|
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno"> 93</span><span class="comment"> * advanced cycle counting */</span></div>
|
|
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno"> 94</span>} <a class="code hl_struct" href="structsimple__spinlock__t.html">simple_spinlock_t</a>;</div>
|
|
</div>
|
|
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno"> 95</span> </div>
|
|
<div class="foldopen" id="foldopen00104" data-start="{" data-end="}">
|
|
<div class="line"><a id="l00104" name="l00104"></a><span class="lineno"><a class="line" href="k__spinlock_8h.html#a614711109a66b779e92036c573e57002"> 104</a></span><span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code hl_function" href="k__spinlock_8h.html#a614711109a66b779e92036c573e57002">simple_spin_init</a>(<a class="code hl_struct" href="structsimple__spinlock__t.html">simple_spinlock_t</a> *lock) {</div>
|
|
<div class="line"><a id="l00105" name="l00105"></a><span class="lineno"> 105</span> <span class="comment">// Initialize the lock state to 0 (unlocked).</span></div>
|
|
<div class="line"><a id="l00106" name="l00106"></a><span class="lineno"> 106</span> lock-><a class="code hl_variable" href="structsimple__spinlock__t.html#ae8d529ab0ac1b69010d98ef8336e9172">locked</a> = 0;</div>
|
|
<div class="line"><a id="l00107" name="l00107"></a><span class="lineno"> 107</span> <span class="comment">// Initialize statistics.</span></div>
|
|
<div class="line"><a id="l00108" name="l00108"></a><span class="lineno"> 108</span> lock-><a class="code hl_variable" href="structsimple__spinlock__t.html#a849f8cded125ee7192052ca71e67f390">acquisitions</a> = 0;</div>
|
|
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno"> 109</span> lock-><a class="code hl_variable" href="structsimple__spinlock__t.html#a7c66988c5d374d8eaf3b522a1ed7d041">contentions</a> = 0;</div>
|
|
<div class="line"><a id="l00110" name="l00110"></a><span class="lineno"> 110</span>}</div>
|
|
</div>
|
|
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno"> 111</span> </div>
|
|
<div class="foldopen" id="foldopen00123" data-start="{" data-end="}">
|
|
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno"><a class="line" href="k__spinlock_8h.html#a8e8fd03b0cdf6f309bde43577c3dd548"> 123</a></span><span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code hl_function" href="k__spinlock_8h.html#a8e8fd03b0cdf6f309bde43577c3dd548">simple_spin_lock</a>(<a class="code hl_struct" href="structsimple__spinlock__t.html">simple_spinlock_t</a> *lock) {</div>
|
|
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno"> 124</span> <span class="comment">// Attempt to acquire the lock immediately using atomic test-and-set.</span></div>
|
|
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno"> 125</span> <span class="comment">// If __sync_lock_test_and_set returns 0, the lock was acquired successfully</span></div>
|
|
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno"> 126</span> <span class="comment">// (it was 0 and is now 1).</span></div>
|
|
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno"> 127</span> <span class="keywordflow">if</span> (__sync_lock_test_and_set(&lock-><a class="code hl_variable" href="structsimple__spinlock__t.html#ae8d529ab0ac1b69010d98ef8336e9172">locked</a>, 1) == 0) {</div>
|
|
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno"> 128</span> lock-><a class="code hl_variable" href="structsimple__spinlock__t.html#a849f8cded125ee7192052ca71e67f390">acquisitions</a>++; <span class="comment">// Successfully acquired on the first try.</span></div>
|
|
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno"> 129</span> <span class="keywordflow">return</span>; <span class="comment">// Lock acquired, no contention.</span></div>
|
|
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno"> 130</span> }</div>
|
|
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno"> 131</span> </div>
|
|
<div class="line"><a id="l00132" name="l00132"></a><span class="lineno"> 132</span> <span class="comment">// If the first attempt failed, the lock was already held. This is a</span></div>
|
|
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno"> 133</span> <span class="comment">// contention.</span></div>
|
|
<div class="line"><a id="l00134" name="l00134"></a><span class="lineno"> 134</span> lock-><a class="code hl_variable" href="structsimple__spinlock__t.html#a7c66988c5d374d8eaf3b522a1ed7d041">contentions</a>++;</div>
|
|
<div class="line"><a id="l00135" name="l00135"></a><span class="lineno"> 135</span> <span class="keywordtype">int</span> spin_count = 0; <span class="comment">// Initialize spin counter for this contention episode.</span></div>
|
|
<div class="line"><a id="l00136" name="l00136"></a><span class="lineno"> 136</span> </div>
|
|
<div class="line"><a id="l00137" name="l00137"></a><span class="lineno"> 137</span> <span class="comment">// Loop indefinitely, spinning and re-attempting to acquire the lock.</span></div>
|
|
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno"> 138</span> <span class="keywordflow">while</span> (1) {</div>
|
|
<div class="line"><a id="l00139" name="l00139"></a><span class="lineno"> 139</span> <span class="comment">// Inner busy-wait loop: Spin while the lock is held by someone else.</span></div>
|
|
<div class="line"><a id="l00140" name="l00140"></a><span class="lineno"> 140</span> <span class="comment">// This inner read loop (checking lock->locked directly) can be slightly</span></div>
|
|
<div class="line"><a id="l00141" name="l00141"></a><span class="lineno"> 141</span> <span class="comment">// more efficient on some architectures than repeatedly executing the atomic</span></div>
|
|
<div class="line"><a id="l00142" name="l00142"></a><span class="lineno"> 142</span> <span class="comment">// __sync_lock_test_and_set, as it might reduce bus contention.</span></div>
|
|
<div class="line"><a id="l00143" name="l00143"></a><span class="lineno"> 143</span> <span class="keywordflow">while</span> (lock-><a class="code hl_variable" href="structsimple__spinlock__t.html#ae8d529ab0ac1b69010d98ef8336e9172">locked</a> != 0) {</div>
|
|
<div class="line"><a id="l00144" name="l00144"></a><span class="lineno"> 144</span> <span class="comment">/* arch_pause() provides a hint to the CPU that this is a spin-wait loop.</span></div>
|
|
<div class="line"><a id="l00145" name="l00145"></a><span class="lineno"> 145</span><span class="comment"> * On x86, this is the "pause" instruction, which can improve performance</span></div>
|
|
<div class="line"><a id="l00146" name="l00146"></a><span class="lineno"> 146</span><span class="comment"> * and reduce power consumption during the spin, especially on</span></div>
|
|
<div class="line"><a id="l00147" name="l00147"></a><span class="lineno"> 147</span><span class="comment"> * hyper-threaded processors by yielding execution resources.</span></div>
|
|
<div class="line"><a id="l00148" name="l00148"></a><span class="lineno"> 148</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00149" name="l00149"></a><span class="lineno"> 149</span> <a class="code hl_function" href="k__spinlock_8h.html#a79887c626e823a36834e349fb75c539c">arch_pause</a>();</div>
|
|
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno"> 150</span> </div>
|
|
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</span> spin_count++; <span class="comment">// Increment spin counter.</span></div>
|
|
<div class="line"><a id="l00152" name="l00152"></a><span class="lineno"> 152</span> <span class="keywordflow">if</span> (spin_count > <a class="code hl_define" href="k__spinlock_8h.html#ab64669b95d563f14428a1f073106ef04">MAX_SPIN_THRESHOLD</a>) {</div>
|
|
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno"> 153</span> <span class="comment">/* If we've spun too many times, call kernel_yield().</span></div>
|
|
<div class="line"><a id="l00154" name="l00154"></a><span class="lineno"> 154</span><span class="comment"> * This is to prevent CPU monopolization on highly contended locks</span></div>
|
|
<div class="line"><a id="l00155" name="l00155"></a><span class="lineno"> 155</span><span class="comment"> * by allowing other threads/processes to run.</span></div>
|
|
<div class="line"><a id="l00156" name="l00156"></a><span class="lineno"> 156</span><span class="comment"> * The actual behavior of kernel_yield() depends on its implementation</span></div>
|
|
<div class="line"><a id="l00157" name="l00157"></a><span class="lineno"> 157</span><span class="comment"> * (e.g., true scheduler yield or just a more potent pause).</span></div>
|
|
<div class="line"><a id="l00158" name="l00158"></a><span class="lineno"> 158</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00159" name="l00159"></a><span class="lineno"> 159</span> <a class="code hl_function" href="k__spinlock_8h.html#a3b96827abeb83529b5d946e2654231ed">kernel_yield</a>();</div>
|
|
<div class="line"><a id="l00160" name="l00160"></a><span class="lineno"> 160</span> spin_count = 0; <span class="comment">// Reset counter after yielding.</span></div>
|
|
<div class="line"><a id="l00161" name="l00161"></a><span class="lineno"> 161</span> }</div>
|
|
<div class="line"><a id="l00162" name="l00162"></a><span class="lineno"> 162</span> }</div>
|
|
<div class="line"><a id="l00163" name="l00163"></a><span class="lineno"> 163</span> </div>
|
|
<div class="line"><a id="l00164" name="l00164"></a><span class="lineno"> 164</span> <span class="comment">// After observing lock->locked == 0 in the inner loop,</span></div>
|
|
<div class="line"><a id="l00165" name="l00165"></a><span class="lineno"> 165</span> <span class="comment">// attempt to acquire the lock again using atomic test-and-set.</span></div>
|
|
<div class="line"><a id="l00166" name="l00166"></a><span class="lineno"> 166</span> <span class="keywordflow">if</span> (__sync_lock_test_and_set(&lock-><a class="code hl_variable" href="structsimple__spinlock__t.html#ae8d529ab0ac1b69010d98ef8336e9172">locked</a>, 1) == 0) {</div>
|
|
<div class="line"><a id="l00167" name="l00167"></a><span class="lineno"> 167</span> lock-><a class="code hl_variable" href="structsimple__spinlock__t.html#a849f8cded125ee7192052ca71e67f390">acquisitions</a>++; <span class="comment">// Lock acquired after spinning.</span></div>
|
|
<div class="line"><a id="l00168" name="l00168"></a><span class="lineno"> 168</span> <span class="keywordflow">return</span>; <span class="comment">// Exit the function, lock is now held.</span></div>
|
|
<div class="line"><a id="l00169" name="l00169"></a><span class="lineno"> 169</span> }</div>
|
|
<div class="line"><a id="l00170" name="l00170"></a><span class="lineno"> 170</span> <span class="comment">// If __sync_lock_test_and_set still returned non-zero, it means another</span></div>
|
|
<div class="line"><a id="l00171" name="l00171"></a><span class="lineno"> 171</span> <span class="comment">// CPU/thread acquired the lock between our read of lock->locked and our TAS</span></div>
|
|
<div class="line"><a id="l00172" name="l00172"></a><span class="lineno"> 172</span> <span class="comment">// attempt (a race). In this case, the outer while(1) loop continues, and we</span></div>
|
|
<div class="line"><a id="l00173" name="l00173"></a><span class="lineno"> 173</span> <span class="comment">// re-enter the inner spin.</span></div>
|
|
<div class="line"><a id="l00174" name="l00174"></a><span class="lineno"> 174</span> }</div>
|
|
<div class="line"><a id="l00175" name="l00175"></a><span class="lineno"> 175</span>}</div>
|
|
</div>
|
|
<div class="line"><a id="l00176" name="l00176"></a><span class="lineno"> 176</span> </div>
|
|
<div class="foldopen" id="foldopen00184" data-start="{" data-end="}">
|
|
<div class="line"><a id="l00184" name="l00184"></a><span class="lineno"><a class="line" href="k__spinlock_8h.html#ad62d430c4e62aaa0d945088f5e1adc32"> 184</a></span><span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code hl_function" href="k__spinlock_8h.html#ad62d430c4e62aaa0d945088f5e1adc32">simple_spin_unlock</a>(<a class="code hl_struct" href="structsimple__spinlock__t.html">simple_spinlock_t</a> *lock) {</div>
|
|
<div class="line"><a id="l00185" name="l00185"></a><span class="lineno"> 185</span> <span class="comment">/* Atomically set lock->locked to 0 (unlocked).</span></div>
|
|
<div class="line"><a id="l00186" name="l00186"></a><span class="lineno"> 186</span><span class="comment"> * __sync_lock_release provides a release memory barrier. This ensures that</span></div>
|
|
<div class="line"><a id="l00187" name="l00187"></a><span class="lineno"> 187</span><span class="comment"> * all memory writes made by this thread within the critical section (before</span></div>
|
|
<div class="line"><a id="l00188" name="l00188"></a><span class="lineno"> 188</span><span class="comment"> * this unlock) are visible to other CPUs before the lock is actually</span></div>
|
|
<div class="line"><a id="l00189" name="l00189"></a><span class="lineno"> 189</span><span class="comment"> * released.</span></div>
|
|
<div class="line"><a id="l00190" name="l00190"></a><span class="lineno"> 190</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00191" name="l00191"></a><span class="lineno"> 191</span> __sync_lock_release(&lock-><a class="code hl_variable" href="structsimple__spinlock__t.html#ae8d529ab0ac1b69010d98ef8336e9172">locked</a>);</div>
|
|
<div class="line"><a id="l00192" name="l00192"></a><span class="lineno"> 192</span>}</div>
|
|
</div>
|
|
<div class="line"><a id="l00193" name="l00193"></a><span class="lineno"> 193</span> </div>
|
|
<div class="line"><a id="l00194" name="l00194"></a><span class="lineno"> 194</span><span class="preprocessor">#endif </span><span class="comment">/* K_SPINLOCK_H */</span><span class="preprocessor"></span></div>
|
|
<div class="ttc" id="ak__spinlock_8h_html_a3b96827abeb83529b5d946e2654231ed"><div class="ttname"><a href="k__spinlock_8h.html#a3b96827abeb83529b5d946e2654231ed">kernel_yield</a></div><div class="ttdeci">static void kernel_yield(void)</div><div class="ttdoc">Yields the CPU, typically to the scheduler. (Stub Implementation)</div><div class="ttdef"><b>Definition</b> k_spinlock.h:57</div></div>
|
|
<div class="ttc" id="ak__spinlock_8h_html_a614711109a66b779e92036c573e57002"><div class="ttname"><a href="k__spinlock_8h.html#a614711109a66b779e92036c573e57002">simple_spin_init</a></div><div class="ttdeci">static void simple_spin_init(simple_spinlock_t *lock)</div><div class="ttdoc">Initializes a spinlock to the unlocked state and resets statistics.</div><div class="ttdef"><b>Definition</b> k_spinlock.h:104</div></div>
|
|
<div class="ttc" id="ak__spinlock_8h_html_a79887c626e823a36834e349fb75c539c"><div class="ttname"><a href="k__spinlock_8h.html#a79887c626e823a36834e349fb75c539c">arch_pause</a></div><div class="ttdeci">static void arch_pause(void)</div><div class="ttdoc">Placeholder for arch_pause on non-x86 architectures.</div><div class="ttdef"><b>Definition</b> k_spinlock.h:29</div></div>
|
|
<div class="ttc" id="ak__spinlock_8h_html_a8e8fd03b0cdf6f309bde43577c3dd548"><div class="ttname"><a href="k__spinlock_8h.html#a8e8fd03b0cdf6f309bde43577c3dd548">simple_spin_lock</a></div><div class="ttdeci">static void simple_spin_lock(simple_spinlock_t *lock)</div><div class="ttdoc">Acquires a spinlock, busy-waiting if necessary.</div><div class="ttdef"><b>Definition</b> k_spinlock.h:123</div></div>
|
|
<div class="ttc" id="ak__spinlock_8h_html_ab64669b95d563f14428a1f073106ef04"><div class="ttname"><a href="k__spinlock_8h.html#ab64669b95d563f14428a1f073106ef04">MAX_SPIN_THRESHOLD</a></div><div class="ttdeci">#define MAX_SPIN_THRESHOLD</div><div class="ttdoc">Maximum number of spin iterations before attempting to yield.</div><div class="ttdef"><b>Definition</b> k_spinlock.h:41</div></div>
|
|
<div class="ttc" id="ak__spinlock_8h_html_ad62d430c4e62aaa0d945088f5e1adc32"><div class="ttname"><a href="k__spinlock_8h.html#ad62d430c4e62aaa0d945088f5e1adc32">simple_spin_unlock</a></div><div class="ttdeci">static void simple_spin_unlock(simple_spinlock_t *lock)</div><div class="ttdoc">Releases a previously acquired spinlock.</div><div class="ttdef"><b>Definition</b> k_spinlock.h:184</div></div>
|
|
<div class="ttc" id="astructsimple__spinlock__t_html"><div class="ttname"><a href="structsimple__spinlock__t.html">simple_spinlock_t</a></div><div class="ttdoc">Structure representing a simple spinlock.</div><div class="ttdef"><b>Definition</b> k_spinlock.h:77</div></div>
|
|
<div class="ttc" id="astructsimple__spinlock__t_html_a7c66988c5d374d8eaf3b522a1ed7d041"><div class="ttname"><a href="structsimple__spinlock__t.html#a7c66988c5d374d8eaf3b522a1ed7d041">simple_spinlock_t::contentions</a></div><div class="ttdeci">unsigned long contentions</div><div class="ttdoc">Number of times a thread tried to acquire the lock but found it already held, thus entering a spin-wa...</div><div class="ttdef"><b>Definition</b> k_spinlock.h:91</div></div>
|
|
<div class="ttc" id="astructsimple__spinlock__t_html_a849f8cded125ee7192052ca71e67f390"><div class="ttname"><a href="structsimple__spinlock__t.html#a849f8cded125ee7192052ca71e67f390">simple_spinlock_t::acquisitions</a></div><div class="ttdeci">unsigned long acquisitions</div><div class="ttdoc">Number of times the lock was successfully acquired.</div><div class="ttdef"><b>Definition</b> k_spinlock.h:88</div></div>
|
|
<div class="ttc" id="astructsimple__spinlock__t_html_ae8d529ab0ac1b69010d98ef8336e9172"><div class="ttname"><a href="structsimple__spinlock__t.html#ae8d529ab0ac1b69010d98ef8336e9172">simple_spinlock_t::locked</a></div><div class="ttdeci">volatile int locked</div><div class="ttdoc">The lock state. 0 for unlocked, 1 for locked.</div><div class="ttdef"><b>Definition</b> k_spinlock.h:86</div></div>
|
|
</div><!-- fragment --></div><!-- contents -->
|
|
<!-- start footer part -->
|
|
<hr class="footer"/><address class="footer"><small>
|
|
Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.8
|
|
</small></address>
|
|
</body>
|
|
</html>
|