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

enum {
    buf_size = 4096,
    num_consumers = 4
};

typedef struct {
    int i;
    double d[buf_size];
} buffer_t;

static void buf_init(buffer_t *buf) {
    buf->i = 0;
}

static void buf_push(buffer_t *buf, double x) {
    buf->d[buf->i] = x;
    buf->i++;
}

static double buf_pop(buffer_t *buf) {
    buf->i--;
    return buf->d[buf->i];
}

typedef struct {
    sem_t data_sem;
    sem_t empty;
    sem_t full;
    const char *file_name;
    pthread_mutex_t buf_mutex;
    buffer_t buf;
    pthread_mutex_t sum_mutex;
    double sum;
    long count;
    pthread_mutex_t num_producers_mutex;
    int num_producers;
    pthread_mutex_t data_pieces_mutex;
    int data_pieces;
    int done;
} data_t;

static void data_init(data_t *d)
{
    sem_init(&d->data_sem, 0, 0);
    sem_init(&d->empty, 0, buf_size);
    sem_init(&d->full, 0, 0);
    pthread_mutex_init(&d->buf_mutex, NULL);
    buf_init(&d->buf);
    pthread_mutex_init(&d->sum_mutex, NULL);
    d->sum = 0;
    d->count = 0;
    pthread_mutex_init(&d->num_producers_mutex, NULL);
    d->num_producers = 0;
    pthread_mutex_init(&d->data_pieces_mutex, NULL);
    d->data_pieces = 0;
    d->done = 0;
}

static void data_destroy(data_t *d)
{
    sem_destroy(&d->data_sem);
    sem_destroy(&d->empty);
    sem_destroy(&d->full);
    pthread_mutex_destroy(&d->buf_mutex);
    pthread_mutex_destroy(&d->sum_mutex);
    pthread_mutex_destroy(&d->num_producers_mutex);
    pthread_mutex_destroy(&d->data_pieces_mutex);
}

static void *producer(void *data)
{
    data_t *d = data;
    char *file_name = strdup(d->file_name);
    sem_post(&d->data_sem);
    double x;
    FILE *f;

    f = fopen(file_name, "r");
    if (!f) {
        perror(file_name);
        goto cleanup;
    }
    while (fscanf(f, "%lf", &x) == 1) {
        pthread_mutex_lock(&d->data_pieces_mutex);
        d->data_pieces++;
        pthread_mutex_unlock(&d->data_pieces_mutex);
        sem_wait(&d->empty);
        pthread_mutex_lock(&d->buf_mutex);
        buf_push(&d->buf, x);
        pthread_mutex_unlock(&d->buf_mutex);
        sem_post(&d->full);
    }
    fclose(f);

cleanup:
    pthread_mutex_lock(&d->num_producers_mutex);
    d->num_producers--;
    pthread_mutex_unlock(&d->num_producers_mutex);
    free(file_name);
    return NULL;
}

static void *consumer(void *data)
{
    double x;
    data_t *d = data;

    while (1) {
        sem_wait(&d->full);
        pthread_mutex_lock(&d->buf_mutex);
        if (d->done == 1 && d->buf.i == 0) {
            pthread_mutex_unlock(&d->buf_mutex);
            break;
        }
        x = buf_pop(&d->buf);
        pthread_mutex_unlock(&d->buf_mutex);
        sem_post(&d->empty);
        x = log(x);
        pthread_mutex_lock(&d->sum_mutex);
        d->sum += x;
        d->count++;
        pthread_mutex_unlock(&d->sum_mutex);
        pthread_mutex_lock(&d->data_pieces_mutex);
        d->data_pieces--;
        pthread_mutex_unlock(&d->data_pieces_mutex);
    }

    return NULL;
}

int main(int argc, char **argv)
{
    int i, n;
    pthread_t *tc, *tp;
    data_t d;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s <file name> ... \n", argv[0]);
        return 1;
    }
    data_init(&d);

    n = argc - 1;
    d.num_producers = n;
    tp = malloc(n * sizeof(*tp));
    for (i = 0; i < n; i++) {
        d.file_name = argv[i+1];
        pthread_create(&tp[i], NULL, producer, &d);
        sem_wait(&d.data_sem);
    }

    tc = malloc(num_consumers * sizeof(*tc));
    for (i = 0; i < num_consumers; i++)
        pthread_create(&tc[i], NULL, consumer, &d);

    while (1) {
        pthread_mutex_lock(&d.sum_mutex);
        if(d.count > 0)
            printf("total average: %f (sum = %f; count = %ld)\n",
                   d.sum/((double) d.count), d.sum, d.count);
        pthread_mutex_unlock(&d.sum_mutex);
        pthread_mutex_lock(&d.num_producers_mutex);
        pthread_mutex_lock(&d.data_pieces_mutex);
        if (d.num_producers == 0 && d.data_pieces == 0)
            break;
        pthread_mutex_unlock(&d.data_pieces_mutex);
        pthread_mutex_unlock(&d.num_producers_mutex);
        usleep(200000);
    }
    pthread_mutex_unlock(&d.data_pieces_mutex);
    pthread_mutex_unlock(&d.num_producers_mutex);

    pthread_mutex_lock(&d.buf_mutex);
    d.done = 1;
    pthread_mutex_unlock(&d.buf_mutex);
    for (i = 0; i < num_consumers; i++)
        sem_post(&d.full);

    for (i = 0; i < n; i++)
        pthread_join(tp[i], NULL);

    for (i = 0; i < num_consumers; i++)
        pthread_join(tc[i], NULL);

    data_destroy(&d);

    return 0;
}
