-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounded.cpp
43 lines (37 loc) · 995 Bytes
/
bounded.cpp
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
//===========================================================================//
// Project: Projekat iz Operativnih sistema 1
// File: bounded.cpp
// Date: Jun 2018
//===========================================================================//
#include "bounded.h"
#include <stdlib.h>
#include "intLock.h"
BoundedBuffer::BoundedBuffer (unsigned size) : Size(size),
mutexa(1), mutext(1), spaceAvailable(size), itemAvailable(0),
head(0), tail(0) {
buffer = new char[size];
if (!buffer) exit(1);
}
BoundedBuffer::~BoundedBuffer(){
intLock
delete [] buffer;
intUnlock
}
int BoundedBuffer::append (char d) {
spaceAvailable.wait(1);
mutexa.wait(1);
buffer[tail] = d;
tail = (tail+1)%Size;
mutexa.signal();
itemAvailable.signal();
return 0;
}
char BoundedBuffer::take () {
itemAvailable.wait(1);
mutext.wait(1);
char d = buffer[head];
head = (head+1)%Size;
mutext.signal();
spaceAvailable.signal();
return d;
}