#include #include #define MAX 50 struct tuplo { double *buffer; int nx, ny; double xmin, xmax, ymin, ymax; }; struct fila { struct tuplo v[MAX]; int head, tail; sem_t full, empty, mutex; }; struct fila f; /************************ Buffer circular ************************/ void init(struct fila *f) { f->head = 0; f->tail = 0; } void add(struct fila *f, struct tuplo n) { f->v[f->head] = n; f->head++; if(f->head >= MAX) f->head = 0; } struct tuplo delete(struct fila *f) { struct tuplo n = f->v[f->tail]; f->tail++; if(f->tail >= MAX) f->tail = 0; return n; } /***************************** Fila *****************************/ struct fila *criar_fila(struct fila *f) { sem_init(&f->mutex, 0, 1); sem_init(&f->empty, 0, 0); sem_init(&f->full, 0, MAX); init(f); return f; } struct tuplo tirar_fila(struct fila *f) { sem_wait(&f->empty); sem_wait(&f->mutex); struct tuplo n = delete(f); sem_post(&f->mutex); sem_post(&f->full); return n; } void por_fila(struct fila *f, struct tuplo elem) { sem_wait(&f->full); sem_wait(&f->mutex); add(f, elem); sem_post(&f->mutex); sem_post(&f->empty); }