#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 forks[num_philosophers];
    int fork_state[num_philosophers];
    pthread_mutex_t state_mut;
    int i;
} data_t;

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

    sem_init(&d->data_sem, 0, 0);
    for (i = 0; i < num_philosophers; i++) {
        d->has_fork[i][left_hand] = 1;
        d->has_fork[i][right_hand] = 0;
        d->wants_fork[i][left_hand] = 0;
        d->wants_fork[i][right_hand] = 0;
        sem_init(&d->forks[i], 0, 1);
        d->fork_state[i] = dirty;
    }
    pthread_mutex_init(&d->state_mut, NULL);
}

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

    sem_destroy(&d->data_sem);
    for (i = 0; i < num_philosophers; i++)
        sem_destroy(&d->forks[i]);
    pthread_mutex_destroy(&d->state_mut);
}

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 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 shift_i, int sem_i, int hand,
                      int include_wants)
{
    int other_hand = (hand + 1) % num_hands;

    if (d->wants_fork[shift_i][other_hand] &&
            (!include_wants || d->has_fork[i][hand] &&
             d->fork_state[sem_i] == dirty)) {
        pthread_mutex_lock(&d->state_mut);
        d->fork_state[sem_i] = clean;
        if (include_wants)
            d->wants_fork[i][hand] = 1;
        d->has_fork[i][hand] = 0;
        d->has_fork[shift_i][other_hand] = 1;
        pthread_mutex_unlock(&d->state_mut);
        sem_post(&d->forks[sem_i]);
    }
}

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

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

        give_fork(d, i, left_of(i), i, left_hand, 1);
        if (!d->has_fork[i][left_hand]) {
            pthread_mutex_lock(&d->state_mut);
            d->wants_fork[i][left_hand] = 1;
            pthread_mutex_unlock(&d->state_mut);
            sem_wait(&d->forks[i]);
        }

        give_fork(d, i, right_of(i), right_of(i), right_hand, 1);
        if (!d->has_fork[i][right_hand]) {
            pthread_mutex_lock(&d->state_mut);
            d->wants_fork[i][right_hand] = 1;
            pthread_mutex_unlock(&d->state_mut);
            sem_wait(&d->forks[right_of(i)]);
        }

        eat(i);
        d->fork_state[i] = dirty;
        d->fork_state[right_of(i)] = dirty;

        give_fork(d, i, left_of(i), i, left_hand, 0);
        give_fork(d, i, right_of(i), right_of(i), right_hand, 0);
    }

    return NULL;
}

int main(int argc, char **argv)
{
    int i;
    pthread_t t[num_philosophers];
    data_t d;

    data_init(&d);

    for (i = 0; i < num_philosophers; i++) {
        d.i = i;
        pthread_create(&t[i], NULL, philosopher, &d);
        sem_wait(&d.data_sem);
    }

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

    data_destroy(&d);

    return 0;
}
