-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a bug in assigning mutex to next owner
Summary: When we release a "lock" in XReqSync and there is already another pending waiter, we need to set the new waiter as the new owner before waking up the thread with success. Reviewed By: paulbiss Differential Revision: D67980743 fbshipit-source-id: b5bdbda85005fef22db62490de5b307788442d82
- Loading branch information
1 parent
258d4ca
commit 9e251bb
Showing
5 changed files
with
114 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?hh | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
$ctx = HH\execution_context(); | ||
if ($ctx === 'xbox') { | ||
return; | ||
} | ||
|
||
HH\Asio\join(genMain()); | ||
} | ||
|
||
function getAPCKey(string $lockname) { | ||
return $lockname.'_count'; | ||
} | ||
|
||
async function reportProgress(string $lockname): Awaitable<void> { | ||
$apc_key = getAPCKey($lockname); | ||
|
||
// For up to 20 seconds at 0.1 intervals | ||
$last_finished = 0; | ||
for ($i = 0; $i < 200; $i++) { | ||
$success = false; | ||
$count = apc_fetch($apc_key, inout $success); | ||
if ($count !== $last_finished) { | ||
$last_finished = $count; | ||
echo "Threads finished: $count\n"; | ||
if ($count == 8) { | ||
$seconds = $i * 0.1; | ||
if ($seconds > 15) { | ||
echo "Overall time over 15s\n"; | ||
} else { | ||
echo "Overall time under 15s\n"; | ||
} | ||
return; | ||
} | ||
} | ||
// 100ms | ||
await SleepWaitHandle::create(1000 * 100); | ||
} | ||
} | ||
|
||
async function genMain(): Awaitable<void> { | ||
$lockname = 'lock'.time().rand(); | ||
$apc_key = getAPCKey($lockname); | ||
apc_store($apc_key, 0); | ||
|
||
// 8 Threads, 1s each. Should take 8s to complete. | ||
concurrent { | ||
await fb_gen_user_func_array(__FILE__, 'thread', vec[$lockname]); | ||
await fb_gen_user_func_array(__FILE__, 'thread', vec[$lockname]); | ||
await fb_gen_user_func_array(__FILE__, 'thread', vec[$lockname]); | ||
await fb_gen_user_func_array(__FILE__, 'thread', vec[$lockname]); | ||
await fb_gen_user_func_array(__FILE__, 'thread', vec[$lockname]); | ||
await fb_gen_user_func_array(__FILE__, 'thread', vec[$lockname]); | ||
await fb_gen_user_func_array(__FILE__, 'thread', vec[$lockname]); | ||
await fb_gen_user_func_array(__FILE__, 'thread', vec[$lockname]); | ||
await reportProgress($lockname); | ||
} | ||
} | ||
|
||
<<__DynamicallyCallable>> | ||
function thread(string $lockname) { | ||
echo "Thread started\n"; | ||
usleep(1000 * 500); | ||
$lock = HH\XReqSync::get($lockname); | ||
HH\Asio\join($lock->genLock()); | ||
echo "Thread got lock\n"; | ||
sleep(1); | ||
echo "Thread releasing lock\n"; | ||
$lock->unlock(); | ||
$success = false; | ||
apc_inc(getAPCKey($lockname), 1, inout $success); | ||
} |
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,33 @@ | ||
Thread started | ||
Thread started | ||
Thread started | ||
Thread started | ||
Thread started | ||
Thread started | ||
Thread started | ||
Thread started | ||
Thread got lock | ||
Thread releasing lock | ||
Thread got lock | ||
Threads finished: 1 | ||
Thread releasing lock | ||
Thread got lock | ||
Threads finished: 2 | ||
Thread releasing lock | ||
Thread got lock | ||
Threads finished: 3 | ||
Thread releasing lock | ||
Thread got lock | ||
Threads finished: 4 | ||
Thread releasing lock | ||
Thread got lock | ||
Threads finished: 5 | ||
Thread releasing lock | ||
Thread got lock | ||
Threads finished: 6 | ||
Thread releasing lock | ||
Thread got lock | ||
Threads finished: 7 | ||
Thread releasing lock | ||
Threads finished: 8 | ||
Overall time under 15s |
Empty file.