forked from FelipeResende/socket_redes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.c
More file actions
43 lines (38 loc) · 779 Bytes
/
buffer.c
File metadata and controls
43 lines (38 loc) · 779 Bytes
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
#include "buffer.h"
char getCharFromBuffer(struct buffer *b)
{
sem_wait(&b->full);
char ret = *(b->head);
b->head = incrementa(b, b->head);
sem_post(&b->empty);
return ret;
}
void insertCharInBuffer(struct buffer *b, char c)
{
sem_wait(&b->empty);
*(b->tail) = c;
b->tail = incrementa(b, b->tail);
sem_post(&b->full);
}
char *incrementa(struct buffer *b, char *c)
{
if (c == &b->buff[TAM_BUFFER - 1])
{
return b->buff;
}
return ++c;
}
void initBuffer(struct buffer *b)
{
b->head = (b->buff);
b->tail = (b->buff);
b->get = &getCharFromBuffer;
b->insert = &insertCharInBuffer;
sem_init(&b->full, 0, 0);
sem_init(&b->empty, 0, TAM_BUFFER);
}
void destructBuffer(struct buffer *b)
{
sem_destroy(&b->empty);
sem_destroy(&b->full);
}