Skip to content

Instantly share code, notes, and snippets.

@dimalinux
Created March 2, 2018 04:39
Show Gist options
  • Save dimalinux/72f714ced08c634f3b6b90b05ff1fe03 to your computer and use it in GitHub Desktop.
Save dimalinux/72f714ced08c634f3b6b90b05ff1fe03 to your computer and use it in GitHub Desktop.

Revisions

  1. dimalinux created this gist Mar 2, 2018.
    25 changes: 25 additions & 0 deletions futexsleep.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #include <stdio.h>
    #include <stdint.h>
    #include <time.h>
    #include <unistd.h>
    #include <sys/syscall.h>
    #include <linux/futex.h>

    int
    futex_sleep (time_t seconds, long nanoseconds)
    {
    uint32_t futexWord = 0;
    struct timespec timeout = { seconds, nanoseconds };
    return syscall (SYS_futex, &futexWord, FUTEX_WAIT, futexWord, &timeout,
    NULL, 0);
    }

    int
    main ()
    {
    time_t secs = 2;
    printf ("Before futex_sleep for %ld seconds\n", secs);
    futex_sleep (2, 0);
    printf ("After futex_sleep\n");
    return 0;
    }