This is an error 404
There are `0` results
preferences:
149.34 ms | 1399 KiB | 7 Q<?hh
class Guard {
private static bool $guarded = false;
public static function acquire(): void {
self::$guarded = true;
}
public static function release(): void {
self::$guarded = false;
}
public static function isGuarded(): bool {
return self::$guarded;
}
}
class BatchedWriter {
const int BATCH_SIZE = 2;
private Set<string> $queue = Set { };
private ?Awaitable<ImmMap<string, string>> $waitHandle = null;
public async function write(string $id): Awaitable<string> {
$this->queue[] = $id;
$all_results = await $this->writeAll();
return $all_results->at($id);
}
private function writeAll(): Awaitable<ImmMap<string, string>> {
$wh = $this->waitHandle;
if ($wh === null || HH\Asio\has_finished($wh)) {
$wh = $this->writeAllImpl();
$this->waitHandle = $wh;
}
return $wh;
}
private async function writeAllImpl(): Awaitable<ImmMap<string, string>> {
while (count($this->queue) < self::BATCH_SIZE) {
await HH\Asio\later();
}
$results = Map { };
foreach ($this->queue as $id) {
$results[$id] = $id.' is '.(Guard::isGuarded() ? 'guarded' : 'unguarded');
}
$this->queue = Set { };
return $results->immutable();
}
<<__Memoize>>
public static function get(): BatchedWriter {
return new BatchedWriter();
}
}
function stop_eager_execution(): Awaitable<void> {
return RescheduleWaitHandle::create(RescheduleWaitHandle::QUEUE_DEFAULT, 0);
}
function join_for_guard<T>(
(function(): Awaitable<T>) $callback,
): T {
$awaitable = async {
await stop_eager_execution();
return await $callback();
};
return HH\Asio\join($awaitable);
}
async function do_unguarded_write(): Awaitable<void> {
$result = await BatchedWriter::get()->write(__FUNCTION__);
var_dump(__FUNCTION__.': '.$result);
}
async function do_guarded_write(): Awaitable<void> {
$id = __FUNCTION__;
await HH\Asio\later();
Guard::acquire();
$result = join_for_guard(async () ==> await BatchedWriter::get()->write($id));
var_dump($id.': '.$result);
Guard::release();
}
HH\Asio\join(HH\Asio\va(
do_unguarded_write(),
do_guarded_write(),
));