haiku-website/static/legacy-docs/bebook/BLocker.html

88 lines
13 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>The Be Book - Classes And Methods - The Support Kit</title><link rel="stylesheet" href="be_book.css" type="text/css" media="all" /><link rel="shortcut icon" type="image/vnd.microsoft.icon" href="./images/favicon.ico" /><!--[if IE]>
<link rel="stylesheet" type="text/css" href="be_book_ie.css" />
<![endif]--><meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /><meta name="keywords" content="Access, BeOS, BeBook, API" /><link rel="start" href="index.html" title="The Be Book" /><link rel="up" href="TheSupportKit.html" title="The Support Kit" /><link rel="prev" href="BList.html" title="BList" /><link rel="next" href="BMallocIO.html" title="BMallocIO" /></head><body><div id="header"><div id="headerT"><div id="headerTL"><a accesskey="p" href="BList.html" title="BList"><img src="./images/navigation/prev.png" alt="Prev" /></a> <a accesskey="u" href="TheSupportKit.html" title="The Support Kit"><img src="./images/navigation/up.png" alt="Up" /></a> <a accesskey="n" href="BMallocIO.html" title="BMallocIO"><img src="./images/navigation/next.png" alt="Next" /></a></div><div id="headerTR"><div id="navigpeople"><a href="http://www.haiku-os.org"><img src="./images/People_24.png" alt="haiku-os.org" title="Visit The Haiku Website" /></a></div><div class="navighome" title="Home"><a accesskey="h" href="index.html"><img src="./images/navigation/home.png" alt="Home" /></a></div><div class="navigboxed" id="navigindex"><a accesskey="i" href="ClassIndex.html" title="Index">I</a></div><div class="navigboxed" id="naviglang" title="English">en</div></div><div id="headerTC">The Be Book - Classes And Methods - The Support Kit</div></div><div id="headerB">Prev: <a href="BList.html">BList</a>  Up: <a href="TheSupportKit.html">The Support Kit</a>  Next: <a href="BMallocIO.html">BMallocIO</a></div><hr /></div><div class="section"><div xmlns="" xmlns:d="http://docbook.org/ns/docbook" class="titlepage"><div><div xmlns:d="http://docbook.org/ns/docbook"><h2 xmlns="http://www.w3.org/1999/xhtml" class="title"><a id="BLocker"></a>BLocker</h2></div></div></div><a id="id1271347" class="indexterm"></a><div class="classheader"><table border="0"><colgroup><col /><col /></colgroup><tbody><tr><td><table width="100%" border="0"><colgroup><col /><col /></colgroup><tbody><tr><td>Derived From:</td><td></td></tr><tr><td>Mix-in Classes:</td><td></td></tr><tr><td>Declared In:</td><td><code class="filename">support/Locker.h</code></td></tr><tr><td>Library:</td><td><code class="filename">libbe.so</code></td></tr><tr><td>Allocation:</td><td></td></tr></tbody></table></td><td>
<a class="link overview" href="BLocker_Overview.html" title="BLocker">Class Overview</a>
<div class="toc"><ul><li><span class="section"><a href="BLocker.html#BLocker_ConstructorDestructor">Constructor and Destructor</a></span></li><li><span class="section"><a href="BLocker.html#BLocker_MemberFunctions">Member Functions</a></span></li></ul></div>
</td></tr></tbody></table></div><div class="section"><div xmlns="" xmlns:d="http://docbook.org/ns/docbook" class="titlepage"><div><hr /><div xmlns:d="http://docbook.org/ns/docbook"><h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a id="BLocker_ConstructorDestructor"></a>Constructor and Destructor</h3></div></div></div><div class="section"><div xmlns="" xmlns:d="http://docbook.org/ns/docbook" class="titlepage"><div><div xmlns:d="http://docbook.org/ns/docbook"><h4 xmlns="http://www.w3.org/1999/xhtml" class="title"><a id="BLocker_Constructor"></a>BLocker()</h4></div></div></div><div class="synopsisgroup">
<code class="constructorsynopsis cpp"><span class="methodname">BLocker</span>();</code>
<code class="constructorsynopsis cpp"><span class="methodname">BLocker</span>(<span class="methodparam"><span class="modifier">const </span><span class="type">char* </span><span class="parameter">name</span></span>);</code>
<code class="constructorsynopsis cpp"><span class="methodname">BLocker</span>(<span class="methodparam"><span class="type">bool </span><span class="parameter">benaphore_style</span></span>);</code>
<code class="constructorsynopsis cpp"><span class="methodname">BLocker</span>(<span class="methodparam"><span class="modifier">const </span><span class="type">char* </span><span class="parameter">name</span></span>,<br />        <span class="methodparam"><span class="type">bool </span><span class="parameter">benaphore_style</span></span>);</code>
</div><p>
Sets up the object, creating a semaphore to implement the locking
mechanism. The optional <code class="parameter">name</code> is for diagnostics and debugging.
</p><p>
By default, a <code class="classname">BLocker</code> is implemented as a benaphore—a
hybrid integer mutex and semaphore. This allows for faster performance
when there is little contention for the lock. If you'd rather use a pure
semaphore for locking, then pass <code class="constant">false</code> as the
<code class="parameter">benaphore_style</code> argument.
</p></div><div class="section"><div xmlns="" xmlns:d="http://docbook.org/ns/docbook" class="titlepage"><div><div xmlns:d="http://docbook.org/ns/docbook"><h4 xmlns="http://www.w3.org/1999/xhtml" class="title"><a id="BLocker_Destructor"></a>~BLocker()</h4></div></div></div><code class="destructorsynopsis cpp"><span class="modifier">virtual </span><span class="methodname">~BLocker</span>();</code><p>
Destroys the lock, deleting the controlling semaphore. If there are any
threads blocked waiting to lock the object, they're immediately unblocked.
</p></div></div><div class="section"><div xmlns="" xmlns:d="http://docbook.org/ns/docbook" class="titlepage"><div><hr /><div xmlns:d="http://docbook.org/ns/docbook"><h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a id="BLocker_MemberFunctions"></a>Member Functions</h3></div></div></div><div class="section"><div xmlns="" xmlns:d="http://docbook.org/ns/docbook" class="titlepage"><div><div xmlns:d="http://docbook.org/ns/docbook"><h4 xmlns="http://www.w3.org/1999/xhtml" class="title"><a id="BLocker_Lock"></a><a id="BLocker_LockWithTimeout"></a>
<a id="BLocker_Unlock"></a>
Lock() , LockWithTimeout() , Unlock()</h4></div></div></div><code class="methodsynopsis cpp"><span class="type">bool </span><span class="methodname">Lock</span>();</code><code class="methodsynopsis cpp"><span class="type">status_t </span><span class="methodname">LockWithTimeout</span>(<span class="methodparam"><span class="type">bigtime_t </span><span class="parameter">timeout</span></span>);</code><code class="methodsynopsis cpp"><span class="void">void </span><span class="methodname">Unlock</span>();</code><p>
<code class="methodname">Lock()</code> tries to lock the <code class="classname">BLocker</code>.
The function returns <span class="bold"><strong>a)</strong></span> when the lock
is acquired (return = <code class="constant">true</code>) or <span class="bold"><strong>b)</strong></span>
immediately if the <code class="classname">BLocker</code>'s semaphore
is deleted, most commonly as a result of deleting the object (return =
<code class="constant">false</code>). A thread can nest <code class="methodname">Lock()</code> calls,
but each call must be balanced by
a concomitant <code class="methodname">Unlock()</code>.
</p><p>
<code class="methodname">LockWithTimeout()</code> lets you declare a time limit, specified in
microseconds. If <code class="methodname">LockWithTimeout()</code> can't acquire the lock before the time
limit expires, it returns <code class="constant">B_TIMED_OUT</code>. If the timeout is 0, this function
immediately returns <code class="constant">B_OK</code> (if it locked the
<code class="classname">BLocker</code>
) or <code class="constant">B_ERROR</code> (if it
failed to obtain the lock). If the timeout is <code class="constant">B_INFINITE_TIMEOUT</code>, it
blocks without limit, just as <code class="methodname">Lock()</code> does. Note that if <code class="methodname">Lock()</code> returns 0
(<code class="constant">false</code>), it has failed to lock the <code class="classname">BLocker</code>, but if <code class="methodname">LockWithTimeout()</code>
returns 0 (<code class="constant">B_OK</code>), it has succeeded.
</p><p>
<code class="methodname">Unlock()</code> releases one level of nested locks and returns immediately. If
there are threads blocked waiting for the lock when the lock is released,
the thread that's been waiting the longest acquires the lock.
</p><p>
Any thread can call <code class="methodname">Unlock()</code> on any <code class="classname">BLocker</code>—the thread needn't be
the lock's current holder. Call <code class="methodname">IsLocked()</code> before calling <code class="methodname">Unlock()</code> if you
want to make sure you own a lock before you unlock it.
</p></div><div class="section"><div xmlns="" xmlns:d="http://docbook.org/ns/docbook" class="titlepage"><div><div xmlns:d="http://docbook.org/ns/docbook"><h4 xmlns="http://www.w3.org/1999/xhtml" class="title"><a id="BLocker_LockingThread"></a><a id="BLocker_IsLocked"></a>
<a id="BLocker_CountLocks"></a>
<a id="BLocker_CountLockRequests"></a>
<a id="BLocker_Sem"></a>
LockingThread(), IsLocked(), CountLocks(), CountLockRequests(), Sem()</h4></div></div></div><code class="methodsynopsis cpp"><span class="type">thread_id </span><span class="methodname">LockingThread</span>() <span class="modifier">const</span>;</code><code class="methodsynopsis cpp"><span class="type">bool </span><span class="methodname">IsLocked</span>() <span class="modifier">const</span>;</code><code class="methodsynopsis cpp"><span class="type">int32 </span><span class="methodname">CountLocks</span>() <span class="modifier">const</span>;</code><code class="methodsynopsis cpp"><span class="type">int32 </span><span class="methodname">CountLockRequests</span>() <span class="modifier">const</span>;</code><code class="methodsynopsis cpp"><span class="type">sem_id </span><span class="methodname">Sem</span>() <span class="modifier">const</span>;</code><p>
These functions provide information that may be useful for debugging
purposes.
</p><p>
<code class="methodname">LockingThread()</code> returns the thread that currently
has the <code class="classname">BLocker</code> locked,
or <code class="constant">B_ERROR</code> if the <code class="classname">BLocker</code> isn't locked.
</p><p>
<code class="methodname">IsLocked()</code> returns <code class="constant">true</code> if the calling thread currently has the <code class="classname">BLocker</code>
locked (if it's the locking thread) and <code class="constant">false</code> if not (if some other
thread is the locking thread or the <code class="classname">BLocker</code> isn't locked).
</p><p>
<code class="methodname">CountLocks()</code> returns the number of times the locking thread has locked
the <code class="classname">BLocker</code>—the number of <code class="methodname">Lock()</code>
(or <code class="methodname">LockWithTimeout()</code>) calls that
have not yet been balanced by matching <code class="methodname">Unlock()</code> calls.
</p><p>
<code class="methodname">CountLockRequests()</code> returns the number of threads currently trying to
lock the <code class="classname">BLocker</code>. The count includes the thread that currently holds the
lock plus all threads currently waiting to acquire it.
</p><p>
<code class="methodname">Sem()</code> returns the sem_id for the semaphore that the <code class="classname">BLocker</code> uses to
implement the locking mechanism.
</p></div></div></div><div id="footer"><hr /><div id="footerT">Prev: <a href="BList.html">BList</a>  Up: <a href="TheSupportKit.html">The Support Kit</a>  Next: <a href="BMallocIO.html">BMallocIO</a> </div><div id="footerB"><div id="footerBL"><a href="BList.html" title="BList"><img src="./images/navigation/prev.png" alt="Prev" /></a> <a href="TheSupportKit.html" title="The Support Kit"><img src="./images/navigation/up.png" alt="Up" /></a> <a href="BMallocIO.html" title="BMallocIO"><img src="./images/navigation/next.png" alt="Next" /></a></div><div id="footerBR"><div><a href="http://www.haiku-os.org"><img src="./images/People_24.png" alt="haiku-os.org" title="Visit The Haiku Website" /></a></div><div class="navighome" title="Home"><a accesskey="h" href="index.html"><img src="./images/navigation/home.png" alt="Home" /></a></div></div><div id="footerBC"><a href="http://www.access-company.com/home.html" title="ACCESS Co."><img alt="Access Company" src="./images/access_logo.png" /></a></div></div></div><div id="licenseFooter"><div id="licenseFooterBL"><a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/3.0/" title="Creative Commons License"><img alt="Creative Commons License" style="border-width:0" src="https://licensebuttons.net/l/by-nc-nd/3.0/88x31.png" /></a></div><div id="licenseFooterBR"><a href="./LegalNotice.html">Legal Notice</a></div><div id="licenseFooterBC"><span id="licenseText">This work is licensed under a
<a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/3.0/">Creative
Commons Attribution-Non commercial-No Derivative Works 3.0 License</a>.</span></div></div></body></html>