<?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(),
));
- Output for 7.2.29 - 7.2.33, 7.3.16 - 7.3.31, 7.4.3 - 7.4.32, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- <?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(),
));
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
<?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(),
));
- Output for 8.0.13
- Parse error: syntax error, unexpected token "class" in /in/2hgn6 on line 3
Process exited with code 255. - Output for 7.0.0 - 7.0.18, 7.1.0 - 7.1.4, 7.3.32 - 7.3.33, 7.4.33
- Parse error: syntax error, unexpected 'class' (T_CLASS) in /in/2hgn6 on line 3
Process exited with code 255.
preferences:
146.63 ms | 402 KiB | 163 Q