use futex in user space?

3.4k Views Asked by At

I need functionality of do_futex() call in user space outside of lock/unlock context. I.e., I do not need a mutex, but the exact semantics of the kernel call do_futex.

It would seem it should be available in user space, since the intent was to minimize the number of system calls, but I can't link with it.

Or is there a syscall?

Update:

I'm currently using syscall(__NR_futex, ...) to run do_futex(). But

  1. I have to include to get __NR_futex, which is ugly
  2. I have to include to get FUTEX_WAIT and FUTEX_WAKE, but I still don't get EWOULDBLOCK, or the maximum number of threads for WAKE

Is there a coherent wrapper?

2

There are 2 best solutions below

0
On

As it's said on http://locklessinc.com/articles/obscure_synch/:

Finally, in order to block on a kernel wait-list, we need to use the Futex system call, which unfortunately isn't exposed by linux/futex.h.

And they define their own simple wrapper:

#include <linux/futex.h>
#include <sys/syscall.h>

static long sys_futex(void *addr1, int op, int val1, struct timespec *timeout,
                      void *addr2, int val3) {
    return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
}
2
On

futex(2) system call is probably what you are looking for.

man 2 futex

Also, on side note, man syscalls gives list of all system calls.