-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbounded_queue.c
More file actions
161 lines (119 loc) · 3.31 KB
/
Copy pathbounded_queue.c
File metadata and controls
161 lines (119 loc) · 3.31 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "bounded_queue.h"
#include "./common/ring_buffer.h"
struct bounded_queue {
struct ring_buffer rb;
pthread_mutex_t lock;
pthread_cond_t not_full;
pthread_cond_t not_empty;
};
int bq_init(bounded_queue_t *q , size_t capacity)
{
if(!q || capacity==0)
{
return -1;
}
q->rb.buffer=calloc(capacity, sizeof(void *));
if(!q->rb.buffer)
{return -1;}
q->rb.capacity=capacity;
q->rb.size=0;
q->rb.head=0;
q->rb.tail=0;
if(pthread_mutex_init(&q->lock,NULL) !=0)
goto err_mutex;
if(pthread_cond_init(&q->not_full, NULL)!=0)
goto err_not_full;
if(pthread_cond_init(&q->not_empty, NULL)!=0)
goto err_not_empty;
return 0;
err_not_empty:
pthread_cond_destroy(&q->not_full);
err_not_full:
pthread_mutex_destroy(&q->lock);
err_mutex:
free(q->rb.buffer);
return -1;
}
void bq_destroy(bounded_queue_t *q)
{
if(!q)
return;
pthread_cond_destroy(&q->not_empty);
pthread_cond_destroy(&q->not_full);
pthread_mutex_destroy(&q->lock);
free(q->rb.buffer);
}
int bq_enqueue(bounded_queue_t *q, void *item)
{
if(!q)
return -1;
pthread_mutex_lock(&q->lock);
//block while queue is full
while(q->rb.size==q->rb.capacity)
{
pthread_cond_wait(&q->not_full, &q->lock);
}
//invariants that must hold inside the critical section
assert(q->rb.size<q->rb.capacity);
assert(q->rb.tail<q->rb.capacity);
//enque a single item
q->rb.buffer[q->rb.tail]=item;
q->rb.tail=(q->rb.tail + 1)%q->rb.capacity;
q->rb.size++;
//queue is no longer empty; wake one consumer
pthread_cond_signal(&q->not_empty);
pthread_mutex_unlock(&q->lock);
return 0;
}
int bq_dequeue(bounded_queue_t *q, void **item)
{
if(!q || !item)
return -1;
pthread_mutex_lock(&q->lock);
//block while queue is empty
while(q->rb.size==0)
{
pthread_cond_wait(&q->not_empty,&q->lock);
}
//invariants that must hold inside the critical section
assert(q->rb.size>0);
assert(q->rb.head<q->rb.capacity);
//dequeu the item
*item=q->rb.buffer[q->rb.head];
q->rb.head=(q->rb.head+1)%q->rb.capacity;
q->rb.size--;
//queue is no longer full; wake one producer
pthread_cond_signal(&q->not_full);
pthread_mutex_unlock(&q->lock);
return 0;
}
size_t bq_capacity(bounded_queue_t *q)
{
if(!q)
return 0;
return q->rb.capacity;
}
size_t bq_size(bounded_queue_t *q)
{
if(!q)
return 0;
pthread_mutex_lock(&q->lock);
size_t size=q->rb.size;
pthread_mutex_unlock(&q->lock);
return size;
}
bounded_queue_t* bq_create(size_t capacity)
{
bounded_queue_t *q=malloc(sizeof(struct bounded_queue));
if(!q) return NULL;
if(bq_init(q, capacity) != 0 )
{
free(q);
return NULL;
}
return q;
}