-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72886f2
commit 7986b01
Showing
9 changed files
with
330 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
+----------------------------------------------------------------------+ | ||
| Swoole | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 2.0 of the Apache license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| http://www.apache.org/licenses/LICENSE-2.0.html | | ||
| If you did not receive a copy of the Apache2.0 license and are unable| | ||
| to obtain it through the world-wide-web, please send a note to | | ||
| [email protected] so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Author: NathanFreeman <[email protected]> | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#include "swoole.h" | ||
|
||
#ifdef HAVE_IOURING_FUTEX | ||
#include "swoole_coroutine.h" | ||
#include "swoole_iouring.h" | ||
#include "swoole_lock.h" | ||
|
||
namespace swoole { | ||
CoroutineLock::CoroutineLock() : Lock() { | ||
type_ = COROUTINE_LOCK; | ||
value = (sw_atomic_t *) sw_mem_pool()->alloc(sizeof(sw_atomic_t)); | ||
*value = 0; | ||
} | ||
|
||
CoroutineLock::~CoroutineLock() { | ||
sw_mem_pool()->free((void *) value); | ||
value = nullptr; | ||
} | ||
|
||
int CoroutineLock::lock() { | ||
return lock_impl(true); | ||
} | ||
|
||
int CoroutineLock::trylock() { | ||
return lock_impl(false); | ||
} | ||
|
||
int CoroutineLock::lock_rd() { | ||
return lock_impl(true); | ||
} | ||
|
||
int CoroutineLock::trylock_rd() { | ||
return lock_impl(false); | ||
} | ||
|
||
int CoroutineLock::unlock() { | ||
Coroutine *current_coroutine = Coroutine::get_current(); | ||
if (current_coroutine == nullptr) { | ||
swoole_warning("The coroutine lock can only be used in a coroutine environment"); | ||
return 0; | ||
} | ||
|
||
if (*value == 0) { | ||
return 0; | ||
} | ||
|
||
*value = 0; | ||
cid = 0; | ||
coroutine = nullptr; | ||
return Iouring::wakeup_futex((uint32_t *) value) > 0 ? 0 : 1; | ||
} | ||
|
||
int CoroutineLock::lock_impl(bool blocking) { | ||
Coroutine *current_coroutine = Coroutine::get_current(); | ||
if (current_coroutine == nullptr) { | ||
swoole_warning("The coroutine lock can only be used in a coroutine environment"); | ||
return 1; | ||
} | ||
|
||
if (current_coroutine == static_cast<Coroutine *>(coroutine) && current_coroutine->get_cid() == cid) { | ||
return 0; | ||
} | ||
|
||
int result = 0; | ||
if (!sw_atomic_cmp_set(value, 0, 1)) { | ||
if (!blocking) { | ||
return 1; | ||
} | ||
|
||
result = Iouring::wait_futex((uint32_t *) value); | ||
*value = 1; | ||
} | ||
|
||
cid = current_coroutine->get_cid(); | ||
coroutine = (void *) current_coroutine; | ||
return result; | ||
} | ||
} // namespace swoole | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--TEST-- | ||
swoole_lock: coroutine lock | ||
--SKIPIF-- | ||
<?php | ||
require __DIR__ . '/../include/skipif.inc'; | ||
if (!defined('SWOOLE_COROUTINELOCK')) { | ||
skip('coroutine lock require linux kernel >= 6.7 and liburing version >= 2.6'); | ||
} | ||
?> | ||
--FILE-- | ||
<?php | ||
use Swoole\Lock; | ||
use function Swoole\Coroutine\run; | ||
use function Swoole\Coroutine\go; | ||
use Swoole\Coroutine\WaitGroup; | ||
|
||
swoole_async_set([ | ||
'iouring_workers' => 32, | ||
'iouring_entries' => 20000, | ||
'iouring_flag' => SWOOLE_IOURING_SQPOLL | ||
]); | ||
|
||
$lock = new Lock(SWOOLE_COROUTINELOCK); | ||
var_dump($lock->lock()); | ||
|
||
run(function () use ($argv, $lock) { | ||
$waitGroup = new WaitGroup(); | ||
go(function () use ($waitGroup, $lock) { | ||
$waitGroup->add(); | ||
$lock->lock(); | ||
$lock->lock(); | ||
sleep(10); | ||
var_dump(1); | ||
$lock->unlock(); | ||
$waitGroup->done(); | ||
}); | ||
|
||
go(function () use ($waitGroup, $lock) { | ||
$waitGroup->add(); | ||
sleep(3); | ||
$lock->lock(); | ||
var_dump(2); | ||
$lock->unlock(); | ||
$waitGroup->done(); | ||
}); | ||
|
||
go(function () use ($waitGroup, $lock) { | ||
$waitGroup->add(); | ||
sleep(1); | ||
$lock->lock_read(); | ||
var_dump(3); | ||
$lock->unlock(); | ||
$waitGroup->done(); | ||
}); | ||
|
||
go(function () use ($waitGroup) { | ||
$waitGroup->add(); | ||
var_dump(5); | ||
$waitGroup->done(); | ||
}); | ||
|
||
$waitGroup->wait(); | ||
}); | ||
?> | ||
--EXPECTF-- | ||
%s | ||
bool(false) | ||
int(5) | ||
int(1) | ||
int(3) | ||
int(2) |
Oops, something went wrong.