Skip to content

Commit

Permalink
Add HH\SimpleLock\is_held
Browse files Browse the repository at this point in the history
Reviewed By: jano

Differential Revision: D68632411

fbshipit-source-id: 8527d255e871e07bc6df258cc58c046db5aa680b
  • Loading branch information
paulbiss authored and facebook-github-bot committed Jan 24, 2025
1 parent 1a98e72 commit a2466c5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
6 changes: 6 additions & 0 deletions hphp/runtime/ext/simplelock/ext_simplelock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ void HHVM_FUNCTION(unlock_mutex, const String& name) {
unlock(l);
}

bool HHVM_FUNCTION(is_held, const String& name) {
auto const l = get_lock(name.toCppString());
return l->load(std::memory_order_relaxed) != UNLOCKED;
}

struct SimpleLockExtension final : Extension {
SimpleLockExtension() : Extension("simplelock", "1.0", "sandbox_infra") {}

Expand All @@ -142,6 +147,7 @@ struct SimpleLockExtension final : Extension {
void moduleRegisterNative() override {
HHVM_NAMED_FE(HH\\SimpleLock\\lock, HHVM_FN(lock_mutex));
HHVM_NAMED_FE(HH\\SimpleLock\\try_lock, HHVM_FN(try_lock_mutex));
HHVM_NAMED_FE(HH\\SimpleLock\\is_held, HHVM_FN(is_held));
HHVM_NAMED_FE_STR(
"HH\\SimpleLock\\unlock", HHVM_FN(unlock_mutex), nativeFuncs()
);
Expand Down
10 changes: 10 additions & 0 deletions hphp/runtime/ext/simplelock/ext_simplelock.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ function unlock(string $name): void;
<<__Native>>
function try_lock(string $name): bool;

/*
* Checks if any thread (including the current request) is holding a lock with a
* unique name.
*
* @param string $name - the name of the lock
* @return bool - whether someone is holding the lock
*/
<<__Native>>
function is_held(string $name): bool;

/*
* Acquired a cross-request mutual exclusion lock with a unique name and a
* timeout.
Expand Down
61 changes: 61 additions & 0 deletions hphp/test/slow/simplelock/isheld.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?hh

async function thread_func() {
echo "thread\n";

await HH\SimpleLock\lock('thread_lock1');
await HH\SimpleLock\lock('thread_lock2');
await HH\SimpleLock\lock('end_lock');

echo 'thread: '; var_dump(HH\SimpleLock\is_held('main_lock1'));
echo 'thread: '; var_dump(HH\SimpleLock\is_held('main_lock2'));
echo 'thread: '; var_dump(HH\SimpleLock\is_held('thread_lock1'));
echo 'thread: '; var_dump(HH\SimpleLock\is_held('thread_lock2'));

$_ = true;
$t = apc_inc('threads', 1, inout $_);

await HH\SimpleLock\lock('sync');
HH\SimpleLock\unlock('thread_lock2');
}

function thread_main() {
HH\Asio\join(thread_func());
}

<<__EntryPoint>>
async function main() {
if (HH\execution_context() === "xbox") return;

await HH\SimpleLock\lock('main_lock1');
await HH\SimpleLock\lock('main_lock2');
await HH\SimpleLock\lock('sync');
apc_store('threads', 0);

$func = fb_call_user_func_async(
__FILE__,
'thread_main'
);

$_ = true;
while (apc_fetch('threads', inout $_) !== 1) usleep(10);

echo 'main: '; var_dump(HH\SimpleLock\is_held('main_lock1'));
echo 'main: '; var_dump(HH\SimpleLock\is_held('main_lock2'));
echo 'main: '; var_dump(HH\SimpleLock\is_held('thread_lock1'));
echo 'main: '; var_dump(HH\SimpleLock\is_held('thread_lock2'));

HH\SimpleLock\unlock('sync');
fb_end_user_func_async($func);

HH\SimpleLock\unlock('main_lock2');
await HH\SimpleLock\lock('end_lock');

echo 'main: '; var_dump(HH\SimpleLock\is_held('main_lock1'));
echo 'main: '; var_dump(HH\SimpleLock\is_held('main_lock2'));
echo 'main: '; var_dump(HH\SimpleLock\is_held('thread_lock1'));
echo 'main: '; var_dump(HH\SimpleLock\is_held('thread_lock2'));

echo "Main done.\n";
}

14 changes: 14 additions & 0 deletions hphp/test/slow/simplelock/isheld.php.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
thread
thread: bool(true)
thread: bool(true)
thread: bool(true)
thread: bool(true)
main: bool(true)
main: bool(true)
main: bool(true)
main: bool(true)
main: bool(true)
main: bool(false)
main: bool(false)
main: bool(false)
Main done.
Empty file.

0 comments on commit a2466c5

Please sign in to comment.