Skip to content

Instantly share code, notes, and snippets.

@adsr
Created February 23, 2022 16:16
Show Gist options
  • Save adsr/88066c9393ede9ef5e6a0804cf9fbda7 to your computer and use it in GitHub Desktop.
Save adsr/88066c9393ede9ef5e6a0804cf9fbda7 to your computer and use it in GitHub Desktop.
<?php
$ffi = FFI::cdef(<<<'EOD'
void *sem_open(const char *name, int oflag, int mode, int value);
int sem_post(void *sem);
int sem_wait(void *sem);
int sem_getvalue(void *sem, int *sval);
int sem_unlink(const char *name);
int sem_close(void *sem);
EOD
);
$pid = getmypid();
$O_CREAT = 0x0040;
$sem = $ffi->sem_open('test_sem', $O_CREAT, 0644, 1);
$sem_val = $ffi->new('int');
while (true) {
$ffi->sem_getvalue($sem, FFI::addr($sem_val));
$ffi->sem_wait($sem);
printf("%d acquired lock, sem_val was %d\n", $pid, $sem_val->cdata);
$ffi->sem_post($sem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment