<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/book.swoole.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'class.swoole-coroutine-lock.php',
    1 => 'Swoole\\Coroutine\\Lock',
    2 => 'The Swoole\\Coroutine\\Lock class',
  ),
  'up' => 
  array (
    0 => 'book.swoole.php',
    1 => 'Swoole',
  ),
  'prev' => 
  array (
    0 => 'swoole-coroutine.suspend.php',
    1 => 'Swoole\\Coroutine::suspend',
  ),
  'next' => 
  array (
    0 => 'swoole-coroutine-lock.construct.php',
    1 => 'Swoole\\Coroutine\\Lock::__construct',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/swoole/swoole.coroutine.lock.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/class.swoole-coroutine-lock.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="class.swoole-coroutine-lock" class="reference">

 <h1 class="title">The Swoole\Coroutine\Lock class</h1>
 

 <div class="partintro"><p class="verinfo">(No version information available, might only be in Git)</p>


  <div class="section" id="swoole-coroutine-lock.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    Swoole 6.0.1 introduced a coroutine lock that supports inter-process and inter-thread sharing.
    This lock is designed with non-blocking behavior and enables efficient coroutine synchronization
    in multi-process and multi-thread environments.
   </p>
   <p class="para">
    When compiled with the <code class="literal">--enable-iouring</code> option and the Linux kernel supports
    the <code class="literal">io_uring futex</code> feature, Swoole&#039;s coroutine lock implements synchronization
    using <code class="literal">io_uring futex</code>. In this case, coroutines wait for lock wakeups using
    an efficient queuing mechanism, significantly improving performance.
   </p>
   <p class="para">
    Without <code class="literal">io_uring futex</code>, the coroutine lock falls back to an exponential backoff
    sleep mechanism, where the wait time increases by 2^n milliseconds (n being the number of failures)
    after each failed attempt to acquire the lock. While this approach avoids busy waiting, it introduces
    additional CPU scheduling overhead and latency.
   </p>
   <p class="para">
    The coroutine lock is reentrant, allowing the currently holding coroutine to safely perform
    multiple lock operations.
   </p>
   <div class="warning"><strong class="warning">警告</strong>
    <p class="para">
     Do not create locks in callback functions like <code class="literal">onReceive</code>, as this will cause
     continuous memory growth and lead to memory leaks.
    </p>
   </div>
   <div class="warning"><strong class="warning">警告</strong>
    <p class="para">
     Locking and unlocking must be performed in the same coroutine, otherwise it will break
     static conditions.
    </p>
   </div>
  </div>


  <div class="section" id="swoole-coroutine-lock.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <span class="ooclass"><strong class="classname"></strong></span>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">Swoole\Coroutine\Lock</strong>
     </span>
     {</div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="swoole-coroutine-lock.construct.php" class="methodname">__construct</a></span>(): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="swoole-coroutine-lock.lock.php" class="methodname">lock</a></span>(): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="swoole-coroutine-lock.trylock.php" class="methodname">trylock</a></span>(): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="swoole-coroutine-lock.unlock.php" class="methodname">unlock</a></span>(): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

   }</div>


  </div>

  <div class="section" id="swoole-coroutine-lock.examples">
   <h2 class="title">示例</h2>
   <div class="example" id="swoole-coroutine-lock.example.basic">
    <p><strong>示例 #1 Basic usage</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">use </span><span style="color: #0000BB">Swoole\Coroutine\Lock</span><span style="color: #007700">;<br />use </span><span style="color: #0000BB">Swoole\Coroutine\WaitGroup</span><span style="color: #007700">;<br />use function </span><span style="color: #0000BB">Swoole\Coroutine\go</span><span style="color: #007700">;<br />use function </span><span style="color: #0000BB">Swoole\Coroutine\run</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$lock </span><span style="color: #007700">= new </span><span style="color: #0000BB">Lock</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$waitGroup </span><span style="color: #007700">= new </span><span style="color: #0000BB">WaitGroup</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">run</span><span style="color: #007700">(function() use (</span><span style="color: #0000BB">$lock</span><span style="color: #007700">, </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">go</span><span style="color: #007700">(function() use (</span><span style="color: #0000BB">$lock</span><span style="color: #007700">, </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">$lock</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">lock</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />        </span><span style="color: #0000BB">$lock</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">unlock</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">done</span><span style="color: #007700">();<br />    });<br /><br />    </span><span style="color: #0000BB">go</span><span style="color: #007700">(function() use (</span><span style="color: #0000BB">$lock</span><span style="color: #007700">, </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">$lock</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">lock</span><span style="color: #007700">(); </span><span style="color: #FF8000">// Wait for the holding coroutine to unlock<br />        </span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />        </span><span style="color: #0000BB">$lock</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">unlock</span><span style="color: #007700">();<br />        </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">done</span><span style="color: #007700">();<br />    });<br /><br />    echo </span><span style="color: #DD0000">'Lock does not block the process'</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">$waitGroup</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">wait</span><span style="color: #007700">();<br />});</span></span></code></div>
    </div>

   </div>
  </div>

 </div>

 



























<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="swoole-coroutine-lock.construct.php">Swoole\Coroutine\Lock::__construct</a> — Construct a new coroutine lock</li><li><a href="swoole-coroutine-lock.lock.php">Swoole\Coroutine\Lock::lock</a> — Acquire the lock, blocking if necessary</li><li><a href="swoole-coroutine-lock.trylock.php">Swoole\Coroutine\Lock::trylock</a> — Attempt to acquire the lock without blocking</li><li><a href="swoole-coroutine-lock.unlock.php">Swoole\Coroutine\Lock::unlock</a> — Release the lock</li></ul>
</div>
<?php manual_footer($setup); ?>