#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>

enum {
    num_philosophers = 5,
    num_hands = 2
};

enum hands {
    left_hand,
    right_hand
};

enum fork_states {
    dirty,
    clean
};

typedef struct {
    sem_t data_sem;
    int has_fork[num_philosophers][num_hands];
    int wants_fork[num_philosophers][num_hands];
    sem_t events[num_philosophers];
    int fork_state[num_philosophers];
    pthread_mutex_t state_mut;
    int i;
} data_t;

static int left_of(int i)
{
    return (i - 1 + num_philosophers) % num_philosophers;
}

static int right_of(int i)
{
    return (i + 1) % num_philosophers;
}

static int fork_of(int i, int hand)
{
    return (hand == left_hand) ? i : right_of(i);
}

static int neighbor_of(int i, int hand)
{
    return (hand == left_hand) ? left_of(i) : right_of(i);
}

static int neighbor_hand(int hand)
{
    return (hand == left_hand) ? right_hand : left_hand;
}

static void data_init(data_t *d)
{
    int i, a, b;

    sem_init(&d->data_sem, 0, 0);

    for (i = 0; i < num_philosophers; i++) {

        d->has_fork[i][left_hand] = 0;
        d->has_fork[i][right_hand] = 0;

        d->wants_fork[i][left_hand] = 0;
        d->wants_fork[i][right_hand] = 0;

        sem_init(&d->events[i], 0, 0);
        d->fork_state[i] = dirty;
    }

    pthread_mutex_init(&d->state_mut, NULL);

    for (i = 0; i < num_philosophers; i++) {
        a = i;
        b = left_of(i);

        if (a < b)
            d->has_fork[a][left_hand] = 1;
        else
            d->has_fork[b][right_hand] = 1;
    }
}

static void data_destroy(data_t *d)
{
    int i;

    sem_destroy(&d->data_sem);

    for (i = 0; i < num_philosophers; i++)
        sem_destroy(&d->events[i]);

    pthread_mutex_destroy(&d->state_mut);
}

static void think(int i)
{
    printf("Philosopher %d is thinking\n", i);
}

static void eat(int i)
{
    printf("Philosopher %d is eating\n", i);
}

static void give_fork(data_t *d, int i, int hand, int hungry)
{
    int neighbor, other_hand, fork, gave;

    neighbor = neighbor_of(i, hand);
    other_hand = neighbor_hand(hand);
    fork = fork_of(i, hand);
    gave = 0;

    pthread_mutex_lock(&d->state_mut);

    if (d->has_fork[i][hand] &&
            d->wants_fork[neighbor][other_hand] &&
            d->fork_state[fork] == dirty) {

        d->fork_state[fork] = clean;
        d->wants_fork[neighbor][other_hand] = 0;

        d->has_fork[i][hand] = 0;
        d->has_fork[neighbor][other_hand] = 1;

        if (hungry)
            d->wants_fork[i][hand] = 1;

        gave = 1;
    }

    pthread_mutex_unlock(&d->state_mut);

    if (gave)
        sem_post(&d->events[neighbor]);
}

static void give_forks(data_t *d, int i, int hungry)
{
    give_fork(d, i, left_hand, hungry);
    give_fork(d, i, right_hand, hungry);
}

static void request_fork(data_t *d, int i, int hand)
{
    int neighbor, notify;

    neighbor = neighbor_of(i, hand);
    notify = 0;

    pthread_mutex_lock(&d->state_mut);

    if (!d->has_fork[i][hand] && !d->wants_fork[i][hand]) {
        d->wants_fork[i][hand] = 1;
        notify = 1;
    }

    pthread_mutex_unlock(&d->state_mut);

    if (notify)
        sem_post(&d->events[neighbor]);
}

static void request_forks(data_t *d, int i)
{
    request_fork(d, i, left_hand);
    request_fork(d, i, right_hand);
}

static int ready_to_eat(data_t *d, int i)
{
    int hand, result, neighbor, other_hand, fork;

    result = 1;

    pthread_mutex_lock(&d->state_mut);

    if (!d->has_fork[i][left_hand] || !d->has_fork[i][right_hand])
        result = 0;
    else
        for (hand = 0; hand < num_hands; hand++) {
            neighbor = neighbor_of(i, hand);
            other_hand = neighbor_hand(hand);
            fork = fork_of(i, hand);

            if (d->wants_fork[neighbor][other_hand] &&
                    d->fork_state[fork] == dirty) {
                result = 0;
                break;
            }
        }

    pthread_mutex_unlock(&d->state_mut);

    return result;
}

static void make_forks_dirty(data_t *d, int i)
{
    int left, right;

    left = fork_of(i, left_hand);
    right = fork_of(i, right_hand);

    pthread_mutex_lock(&d->state_mut);

    if (d->has_fork[i][left_hand])
        d->fork_state[left] = dirty;

    if (d->has_fork[i][right_hand])
        d->fork_state[right] = dirty;

    pthread_mutex_unlock(&d->state_mut);
}

static void *philosopher(void *data)
{
    data_t *d = data;
    int i = d->i;
    sem_post(&d->data_sem);

    while (1) {
        think(i);
        sleep(1);

        while (1) {
            give_forks(d, i, 1);
            request_forks(d, i);

            if (ready_to_eat(d, i))
                break;

            sem_wait(&d->events[i]);
        }

        eat(i);
        sleep(1);

        make_forks_dirty(d, i);
        give_forks(d, i, 0);
    }

    return NULL;
}

int main()
{
    int i, failed;
    pthread_t t[num_philosophers];
    data_t d;

    data_init(&d);

    for (i = 0; i < num_philosophers; i++) {
        d.i = i;

        failed = pthread_create(&t[i], NULL, philosopher, &d);
        if (failed) {
            fprintf(stderr, "pthread_create failed\n");
            return 1;
        }

        sem_wait(&d.data_sem);
    }

    for (i = 0; i < num_philosophers; i++)
        pthread_join(t[i], NULL);

    data_destroy(&d);

    return 0;
}
