This commit is contained in:
wyfcyx 2022-12-23 03:25:26 +00:00
parent fcec463e6d
commit 44559e9b41
4 changed files with 26 additions and 13 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -3,4 +3,4 @@
<span class="item-spacer"></span> fn <a href="#tymethod.writable" class="fnname">writable</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.bool.html">bool</a>;
<span class="item-spacer"></span> fn <a href="#tymethod.read" class="fnname">read</a>(&amp;self, buf: <a class="struct" href="../mm/page_table/struct.UserBuffer.html" title="struct os::mm::page_table::UserBuffer">UserBuffer</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.usize.html">usize</a>;
<span class="item-spacer"></span> fn <a href="#tymethod.write" class="fnname">write</a>(&amp;self, buf: <a class="struct" href="../mm/page_table/struct.UserBuffer.html" title="struct os::mm::page_table::UserBuffer">UserBuffer</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.usize.html">usize</a>;
}</code></pre></div><h2 id="required-methods" class="small-section-header">Required Methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><div id="tymethod.readable" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/os/fs/mod.rs.html#8">source</a></div><h4 class="code-header">fn <a href="#tymethod.readable" class="fnname">readable</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.bool.html">bool</a></h4></div><div id="tymethod.writable" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/os/fs/mod.rs.html#9">source</a></div><h4 class="code-header">fn <a href="#tymethod.writable" class="fnname">writable</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.bool.html">bool</a></h4></div><div id="tymethod.read" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/os/fs/mod.rs.html#10">source</a></div><h4 class="code-header">fn <a href="#tymethod.read" class="fnname">read</a>(&amp;self, buf: <a class="struct" href="../mm/page_table/struct.UserBuffer.html" title="struct os::mm::page_table::UserBuffer">UserBuffer</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.usize.html">usize</a></h4></div><div id="tymethod.write" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/os/fs/mod.rs.html#11">source</a></div><h4 class="code-header">fn <a href="#tymethod.write" class="fnname">write</a>(&amp;self, buf: <a class="struct" href="../mm/page_table/struct.UserBuffer.html" title="struct os::mm::page_table::UserBuffer">UserBuffer</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.usize.html">usize</a></h4></div></div><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div class="item-list" id="implementors-list"><section id="impl-File-for-OSInode" class="impl has-srclink"><span class="rightside"><a class="srclink" href="../../src/os/fs/inode.rs.html#108-139">source</a></span><a href="#impl-File-for-OSInode" class="anchor"></a><h3 class="code-header in-band">impl <a class="trait" href="trait.File.html" title="trait os::fs::File">File</a> for <a class="struct" href="inode/struct.OSInode.html" title="struct os::fs::inode::OSInode">OSInode</a></h3></section><section id="impl-File-for-Pipe" class="impl has-srclink"><span class="rightside"><a class="srclink" href="../../src/os/fs/pipe.rs.html#108-166">source</a></span><a href="#impl-File-for-Pipe" class="anchor"></a><h3 class="code-header in-band">impl <a class="trait" href="trait.File.html" title="trait os::fs::File">File</a> for <a class="struct" href="pipe/struct.Pipe.html" title="struct os::fs::pipe::Pipe">Pipe</a></h3></section><section id="impl-File-for-Stdin" class="impl has-srclink"><span class="rightside"><a class="srclink" href="../../src/os/fs/stdio.rs.html#9-28">source</a></span><a href="#impl-File-for-Stdin" class="anchor"></a><h3 class="code-header in-band">impl <a class="trait" href="trait.File.html" title="trait os::fs::File">File</a> for <a class="struct" href="stdio/struct.Stdin.html" title="struct os::fs::stdio::Stdin">Stdin</a></h3></section><section id="impl-File-for-Stdout" class="impl has-srclink"><span class="rightside"><a class="srclink" href="../../src/os/fs/stdio.rs.html#30-46">source</a></span><a href="#impl-File-for-Stdout" class="anchor"></a><h3 class="code-header in-band">impl <a class="trait" href="trait.File.html" title="trait os::fs::File">File</a> for <a class="struct" href="stdio/struct.Stdout.html" title="struct os::fs::stdio::Stdout">Stdout</a></h3></section></div><script type="text/javascript" src="../../implementors/os/fs/trait.File.js" data-ignore-extern-crates="" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="os" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0-nightly (f6f9d5e73 2022-08-04)" ></div></body></html>
}</code></pre></div><h2 id="required-methods" class="small-section-header">Required Methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><div id="tymethod.readable" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/os/fs/mod.rs.html#8">source</a></div><h4 class="code-header">fn <a href="#tymethod.readable" class="fnname">readable</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.bool.html">bool</a></h4></div><div id="tymethod.writable" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/os/fs/mod.rs.html#9">source</a></div><h4 class="code-header">fn <a href="#tymethod.writable" class="fnname">writable</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.bool.html">bool</a></h4></div><div id="tymethod.read" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/os/fs/mod.rs.html#10">source</a></div><h4 class="code-header">fn <a href="#tymethod.read" class="fnname">read</a>(&amp;self, buf: <a class="struct" href="../mm/page_table/struct.UserBuffer.html" title="struct os::mm::page_table::UserBuffer">UserBuffer</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.usize.html">usize</a></h4></div><div id="tymethod.write" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/os/fs/mod.rs.html#11">source</a></div><h4 class="code-header">fn <a href="#tymethod.write" class="fnname">write</a>(&amp;self, buf: <a class="struct" href="../mm/page_table/struct.UserBuffer.html" title="struct os::mm::page_table::UserBuffer">UserBuffer</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.usize.html">usize</a></h4></div></div><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div class="item-list" id="implementors-list"><section id="impl-File-for-OSInode" class="impl has-srclink"><span class="rightside"><a class="srclink" href="../../src/os/fs/inode.rs.html#108-139">source</a></span><a href="#impl-File-for-OSInode" class="anchor"></a><h3 class="code-header in-band">impl <a class="trait" href="trait.File.html" title="trait os::fs::File">File</a> for <a class="struct" href="inode/struct.OSInode.html" title="struct os::fs::inode::OSInode">OSInode</a></h3></section><section id="impl-File-for-Pipe" class="impl has-srclink"><span class="rightside"><a class="srclink" href="../../src/os/fs/pipe.rs.html#108-173">source</a></span><a href="#impl-File-for-Pipe" class="anchor"></a><h3 class="code-header in-band">impl <a class="trait" href="trait.File.html" title="trait os::fs::File">File</a> for <a class="struct" href="pipe/struct.Pipe.html" title="struct os::fs::pipe::Pipe">Pipe</a></h3></section><section id="impl-File-for-Stdin" class="impl has-srclink"><span class="rightside"><a class="srclink" href="../../src/os/fs/stdio.rs.html#9-28">source</a></span><a href="#impl-File-for-Stdin" class="anchor"></a><h3 class="code-header in-band">impl <a class="trait" href="trait.File.html" title="trait os::fs::File">File</a> for <a class="struct" href="stdio/struct.Stdin.html" title="struct os::fs::stdio::Stdin">Stdin</a></h3></section><section id="impl-File-for-Stdout" class="impl has-srclink"><span class="rightside"><a class="srclink" href="../../src/os/fs/stdio.rs.html#30-46">source</a></span><a href="#impl-File-for-Stdout" class="anchor"></a><h3 class="code-header in-band">impl <a class="trait" href="trait.File.html" title="trait os::fs::File">File</a> for <a class="struct" href="stdio/struct.Stdout.html" title="struct os::fs::stdio::Stdout">Stdout</a></h3></section></div><script type="text/javascript" src="../../implementors/os/fs/trait.File.js" data-ignore-extern-crates="" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="os" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0-nightly (f6f9d5e73 2022-08-04)" ></div></body></html>

View File

@ -164,6 +164,13 @@
<span id="164">164</span>
<span id="165">165</span>
<span id="166">166</span>
<span id="167">167</span>
<span id="168">168</span>
<span id="169">169</span>
<span id="170">170</span>
<span id="171">171</span>
<span id="172">172</span>
<span id="173">173</span>
</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident"><span class="kw">super</span>::File</span>;
<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::mm::UserBuffer</span>;
<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::sync::UPIntrFreeCell</span>;
@ -280,36 +287,40 @@
}
<span class="kw">fn</span> <span class="ident">read</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">buf</span>: <span class="ident">UserBuffer</span>) -&gt; <span class="ident">usize</span> {
<span class="macro">assert!</span>(<span class="self">self</span>.<span class="ident">readable</span>());
<span class="kw">let</span> <span class="ident">want_to_read</span> <span class="op">=</span> <span class="ident">buf</span>.<span class="ident">len</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buf_iter</span> <span class="op">=</span> <span class="ident">buf</span>.<span class="ident">into_iter</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">read_size</span> <span class="op">=</span> <span class="number">0usize</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">already_read</span> <span class="op">=</span> <span class="number">0usize</span>;
<span class="kw">loop</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">ring_buffer</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">buffer</span>.<span class="ident">exclusive_access</span>();
<span class="kw">let</span> <span class="ident">loop_read</span> <span class="op">=</span> <span class="ident">ring_buffer</span>.<span class="ident">available_read</span>();
<span class="kw">if</span> <span class="ident">loop_read</span> <span class="op">==</span> <span class="number">0</span> {
<span class="kw">if</span> <span class="ident">ring_buffer</span>.<span class="ident">all_write_ends_closed</span>() {
<span class="kw">return</span> <span class="ident">read_size</span>;
<span class="kw">return</span> <span class="ident">already_read</span>;
}
<span class="ident">drop</span>(<span class="ident">ring_buffer</span>);
<span class="ident">suspend_current_and_run_next</span>();
<span class="kw">continue</span>;
}
<span class="comment">// read at most loop_read bytes</span>
<span class="kw">for</span> <span class="kw">_</span> <span class="kw">in</span> <span class="number">0</span>..<span class="ident">loop_read</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">byte_ref</span>) <span class="op">=</span> <span class="ident">buf_iter</span>.<span class="ident">next</span>() {
<span class="kw">unsafe</span> {
<span class="kw-2">*</span><span class="ident">byte_ref</span> <span class="op">=</span> <span class="ident">ring_buffer</span>.<span class="ident">read_byte</span>();
}
<span class="ident">read_size</span> <span class="op">+</span><span class="op">=</span> <span class="number">1</span>;
<span class="ident">already_read</span> <span class="op">+</span><span class="op">=</span> <span class="number">1</span>;
<span class="kw">if</span> <span class="ident">already_read</span> <span class="op">==</span> <span class="ident">want_to_read</span> {
<span class="kw">return</span> <span class="ident">want_to_read</span>;
}
} <span class="kw">else</span> {
<span class="kw">return</span> <span class="ident">read_size</span>;
<span class="kw">return</span> <span class="ident">already_read</span>;
}
}
}
}
<span class="kw">fn</span> <span class="ident">write</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">buf</span>: <span class="ident">UserBuffer</span>) -&gt; <span class="ident">usize</span> {
<span class="macro">assert!</span>(<span class="self">self</span>.<span class="ident">writable</span>());
<span class="kw">let</span> <span class="ident">want_to_write</span> <span class="op">=</span> <span class="ident">buf</span>.<span class="ident">len</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buf_iter</span> <span class="op">=</span> <span class="ident">buf</span>.<span class="ident">into_iter</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">write_size</span> <span class="op">=</span> <span class="number">0usize</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">already_write</span> <span class="op">=</span> <span class="number">0usize</span>;
<span class="kw">loop</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">ring_buffer</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">buffer</span>.<span class="ident">exclusive_access</span>();
<span class="kw">let</span> <span class="ident">loop_write</span> <span class="op">=</span> <span class="ident">ring_buffer</span>.<span class="ident">available_write</span>();
@ -322,13 +333,15 @@
<span class="kw">for</span> <span class="kw">_</span> <span class="kw">in</span> <span class="number">0</span>..<span class="ident">loop_write</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">byte_ref</span>) <span class="op">=</span> <span class="ident">buf_iter</span>.<span class="ident">next</span>() {
<span class="ident">ring_buffer</span>.<span class="ident">write_byte</span>(<span class="kw">unsafe</span> { <span class="kw-2">*</span><span class="ident">byte_ref</span> });
<span class="ident">write_size</span> <span class="op">+</span><span class="op">=</span> <span class="number">1</span>;
<span class="ident">already_write</span> <span class="op">+</span><span class="op">=</span> <span class="number">1</span>;
<span class="kw">if</span> <span class="ident">already_write</span> <span class="op">==</span> <span class="ident">want_to_write</span> {
<span class="kw">return</span> <span class="ident">want_to_write</span>;
}
} <span class="kw">else</span> {
<span class="kw">return</span> <span class="ident">write_size</span>;
<span class="kw">return</span> <span class="ident">already_write</span>;
}
}
}
}
}
</code></pre></div>
}</code></pre></div>
</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="os" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0-nightly (f6f9d5e73 2022-08-04)" ></div></body></html>