-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounded.cpp
41 lines (37 loc) · 989 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
//===========================================================================//
// Project: Projekat iz Operativnih sistema 1
// File: bounded.cpp
// Date: Jun 2015
//===========================================================================//
#include <stdlib.h>
#include "bounded.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();
mutexa.wait();
buffer[tail] = d;
tail = (tail+1)%Size;
mutexa.signal();
itemAvailable.signal();
return 0;
}
char BoundedBuffer::take () {
itemAvailable.wait();
mutext.wait();
char d = buffer[head];
head = (head+1)%Size;
mutext.signal();
spaceAvailable.signal();
return d;
}