-
Notifications
You must be signed in to change notification settings - Fork 0
/
jbuffer.cpp
97 lines (86 loc) · 2.33 KB
/
jbuffer.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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
*
* This file is part of GarbageCollector.
*
* GarbageCollector is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GarbageCollector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GarbageCollector. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "jbuffer.h"
#include <stdio.h>
#include <stdbool.h>
#include "sem.h"
#include "VerificationJob.h"
struct BNDBUF {
volatile int indNextIn;
volatile int indNextOut;
struct VerificationJob **values;
int size;
SEM *anz_frei;
SEM *anz_bel;
};
BNDBUF *bbCreate (size_t size) {
BNDBUF *buf = (BNDBUF *)malloc(sizeof(BNDBUF));
if (buf == NULL) {
return NULL;
}
buf->values = (struct VerificationJob**) malloc(sizeof(struct VerificationJob*)*size);
if (buf->values == NULL) {
free(buf);
return NULL;
}
buf->indNextIn = 0;
buf->indNextOut = 0;
int size_int = (int)size;
buf->size = size_int;
buf->anz_bel = semCreate(0);
if(buf->anz_bel == NULL) {
free(buf->values);
free(buf);
return NULL;
}
buf->anz_frei = semCreate(size_int);
if(buf->anz_frei == NULL) {
semDestroy(buf->anz_bel);
free(buf->values);
free(buf);
return NULL;
}
return buf;
}
void bbDestroy (BNDBUF *buf) {
if(buf == NULL) {
return;
}
free(buf->values);
semDestroy(buf->anz_frei);
semDestroy(buf->anz_bel);
free(buf);
}
void bbPut (BNDBUF *buf, struct VerificationJob *value) {
P(buf->anz_frei);
buf->values[buf->indNextIn] = value;
buf->indNextIn = ((buf->indNextIn)+1)%(buf->size);
V(buf->anz_bel);
}
struct VerificationJob *bbGet(BNDBUF *buf) {
int indNextOutCopy;
struct VerificationJob *value;
P(buf->anz_bel);
do {
indNextOutCopy = buf->indNextOut;
value = buf->values[buf->indNextOut];
} while(!__sync_bool_compare_and_swap(&buf->indNextOut,indNextOutCopy,((buf->indNextOut)+1)%(buf->size)));
V(buf->anz_frei);
return value;
}