2022-04-05 01:12:40 +00:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <unistd.h>
|
2022-04-05 20:37:42 +00:00
|
|
|
#include "list.hpp" // list implementation
|
|
|
|
#include "globals.hpp" // lists, sems, muts
|
2022-04-04 18:39:30 +00:00
|
|
|
#include "consumer.hpp"
|
|
|
|
|
2022-04-05 20:37:42 +00:00
|
|
|
#define unlink(x) list_unlink(x)
|
|
|
|
#define link(x) list_link(x)
|
|
|
|
|
2022-04-05 01:12:40 +00:00
|
|
|
int consume(block* c);
|
|
|
|
|
|
|
|
void *consumer (void *) {
|
|
|
|
while (true) {
|
2022-04-05 20:37:42 +00:00
|
|
|
|
|
|
|
wait(sem_list2);
|
|
|
|
wait(mut_list2);
|
2022-04-05 01:12:40 +00:00
|
|
|
block* c = list_unlink(list2);
|
2022-04-05 20:37:42 +00:00
|
|
|
signal(mut_list2);
|
|
|
|
|
2022-04-05 01:12:40 +00:00
|
|
|
//* consume information in block c
|
|
|
|
consume(c);
|
2022-04-05 20:37:42 +00:00
|
|
|
|
|
|
|
wait(mut_freelist);
|
2022-04-05 01:12:40 +00:00
|
|
|
list_link(freelist, c);
|
2022-04-05 20:37:42 +00:00
|
|
|
signal(mut_freelist);
|
|
|
|
signal(sem_freelist);
|
|
|
|
|
2022-04-05 01:12:40 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int consume(block *c) {
|
2022-04-05 20:37:42 +00:00
|
|
|
usleep(75*timescale);
|
|
|
|
return c->data;
|
2022-04-05 01:12:40 +00:00
|
|
|
}
|