-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbbuff.c
More file actions
70 lines (58 loc) · 1.1 KB
/
bbuff.c
File metadata and controls
70 lines (58 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "bbuff.h"
#include <semaphore.h>
// declare variables
int num_items;
void* buffer[BUFFER_SIZE];
// declare semaphores
sem_t mutex;
sem_t full;
sem_t empty;
// initializes variables and semaphores
void bbuff_init(void)
{
sem_init(&mutex, 0, 1);
sem_init(&full, 0, BUFFER_SIZE);
sem_init(&empty, 0, 0);
num_items = 0;
//printf("buffer_init\n");
}
//adds item to buffer while blocking
void bbuff_blocking_insert(void* item)
{
//waits
sem_wait(&full);
sem_wait(&mutex);
//add item to buffer
buffer[num_items] = item;
num_items++;
//signals
sem_post(&mutex);
sem_post(&empty);
//printf("bbuff_blocking_insert\n");
}
//extracts item from buffer while blocking
void* bbuff_blocking_extract(void)
{
//waits
sem_wait(&empty);
sem_wait(&mutex);
//removes item from buffer
void* item = buffer[num_items-1];
num_items--;
//signals
sem_post(&mutex);
sem_post(&full);
//printf("bbuff_blocking_extract\n");
return item;
}
//checks if buffer is empty
_Bool bbuff_is_empty(void)
{
if (num_items == 0)
return true;
else
return false;
}