#include <stdio.h>
#include <errno.h>

#include <pthread.h>
#include <unistd.h>
#include <sched.h>

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


static int val = 0;

/* Lets main know Thread 3 has already read val. */
static volatile int thread3_read = 0;


/* futex_wait: sleep only if *addr == expected */
static int futex_wait(int *addr, int expected)
{
    return syscall(SYS_futex, addr, FUTEX_WAIT, expected, NULL, NULL, 0);
}


/* Wake all threads waiting on addr */
static int futex_wake(int *addr)
{
    return syscall(SYS_futex, addr, FUTEX_WAKE, 100, NULL, NULL, 0);
}


/* ------------------------------------------------ */
/* Thread 2                                         */
/* ------------------------------------------------ */

static void *thread2(void *arg)
{
    int oldval;

    (void)arg;

    printf("Thread 2: read val\n");

    oldval = __sync_fetch_and_add(&val, 0);

    printf("Thread 2: val = %d\n", oldval);

    printf("Thread 2: futex_wait\n");

    futex_wait(&val, oldval);

    printf("Thread 2: return from futex_wait\n");

    return NULL;
}


/* ------------------------------------------------ */
/* Thread 3                                         */
/* ------------------------------------------------ */

static void *thread3(void *arg)
{
    int oldval;
    int result;

    (void)arg;

    printf("Thread 3: read val\n");

    oldval = __sync_fetch_and_add(&val, 0);

    printf("Thread 3: val = %d\n", oldval);

    /*
     * Tell Thread 1 that Thread 3 has read val,
     * but has not called futex_wait yet.
     */
    __sync_lock_test_and_set(&thread3_read, 1);

    /*
     * Delay Thread 3.
     *
     * Thread 1 will change val and call
     * futex_wake while we are here.
     */
    usleep(500000);

    printf("Thread 3: futex_wait\n");

    errno = 0;

    result = futex_wait(&val, oldval);

    printf("Thread 3: return from futex_wait\n");

    if (result == -1 && errno == EAGAIN)
    {
        printf("Thread 3: EAGAIN - val changed, "
               "so the thread did not sleep\n");
    }

    return NULL;
}


/* ------------------------------------------------ */
/* Thread 1                                         */
/* ------------------------------------------------ */

int main(void)
{
    pthread_t t2;
    pthread_t t3;
    int woken;

    /*
     * Start Thread 2.
     */
    pthread_create(&t2, NULL, thread2, NULL);

    /*
     * Give Thread 2 some time to enter futex_wait.
     */
    usleep(100000);

    /*
     * Start Thread 3.
     */
    pthread_create(&t3, NULL, thread3, NULL);

    /*
     * Wait until Thread 3 has read val.
     */
    while (__sync_fetch_and_add(&thread3_read, 0) == 0)
    {
        sched_yield();
    }

    /*
     * main() is Thread 1.
     */

    printf("Thread 1: ++val\n");

    __sync_add_and_fetch(&val, 1);

    printf("Thread 1: val = %d\n",
           __sync_fetch_and_add(&val, 0));

    printf("Thread 1: futex_wake\n");

    woken = futex_wake(&val);

    printf("Thread 1: woke %d thread(s)\n", woken);

    pthread_join(t2, NULL);
    pthread_join(t3, NULL);

    return 0;
}
