Skip to content

Instantly share code, notes, and snippets.

@akihikodaki
Last active November 4, 2025 05:41
Show Gist options
  • Select an option

  • Save akihikodaki/8e6cbe3a94f731602cba9ab6ed3a719f to your computer and use it in GitHub Desktop.

Select an option

Save akihikodaki/8e6cbe3a94f731602cba9ab6ed3a719f to your computer and use it in GitHub Desktop.
static inline void qemu_futex_wait(Futex *f, unsigned val)
{
push(f->targets);
if (f->val == val) {
_lwp_park(f);
}
}
static inline void qemu_futex_wake_all(Futex *f)
{
_lwp_unpark_all(move(f->targets), f->val);
}
static inline void qemu_futex_post_wake(Futex *f)
{
if (!f->refs--) {
free(f);
}
}
static inline void qemu_futex_ref(Futex *f)
{
f->refs++;
}
static inline void qemu_futex_unref(Futex *f)
{
if (!f->refs--) {
free(f);
}
}
init(f) {
qemu_futex_ref(f);
}
wake(f) {
qemu_futex_ref(f);
if (xchg(&f->val, SET) == BUSY) {
qemu_futex_wake_all(f);
}
qemu_futex_unref(f);
}
wait(f) {
f->val = BUSY;
qemu_futex_wait(f, BUSY);
}
destroy(f) {
qemu_futex_unref(f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment