-
Notifications
You must be signed in to change notification settings - Fork 42
/
channel.c
131 lines (97 loc) · 3.18 KB
/
channel.c
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
/*
Copyright (c) 2017-2020 ByteBit
This file is part of BetterSpades.
BetterSpades 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.
BetterSpades 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 BetterSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "channel.h"
bool channel_create(struct channel* ch, size_t object_size, size_t length) {
assert(ch != NULL && object_size > 0 && length > 0);
ch->object_size = object_size;
ch->length = length;
ch->count = 0;
ch->queue = malloc(object_size * length);
ch->loc_insert = 0;
ch->loc_remove = 0;
if(!ch->queue)
return false;
if(pthread_mutex_init(&ch->lock, NULL)) {
free(ch->queue);
return false;
}
if(pthread_cond_init(&ch->signal, NULL)) {
free(ch->queue);
pthread_mutex_destroy(&ch->lock);
return false;
}
return true;
}
size_t channel_size(struct channel* ch) {
assert(ch != NULL);
pthread_mutex_lock(&ch->lock);
size_t res = ch->count;
pthread_mutex_unlock(&ch->lock);
return res;
}
void channel_destroy(struct channel* ch) {
assert(ch != NULL);
free(ch->queue);
pthread_cond_destroy(&ch->signal);
pthread_mutex_destroy(&ch->lock);
}
static void channel_grow(struct channel* ch) {
assert(ch != NULL);
size_t length = ch->length * 2;
ch->queue = realloc(ch->queue, ch->object_size * length);
if(ch->loc_insert <= ch->loc_remove) {
size_t object_count = ch->length - ch->loc_remove;
size_t new_remove_loc = length - object_count;
memcpy((uint8_t*)ch->queue + new_remove_loc * ch->object_size,
(uint8_t*)ch->queue + ch->loc_remove * ch->object_size, object_count * ch->object_size);
ch->loc_remove = new_remove_loc;
}
ch->length = length;
}
void channel_put(struct channel* ch, void* object) {
assert(ch != NULL && object != NULL);
pthread_mutex_lock(&ch->lock);
if(ch->count >= ch->length)
channel_grow(ch);
memcpy((uint8_t*)ch->queue + ch->loc_insert * ch->object_size, object, ch->object_size);
ch->loc_insert = (ch->loc_insert + 1) % ch->length;
ch->count++;
pthread_cond_signal(&ch->signal);
pthread_mutex_unlock(&ch->lock);
}
void channel_await(struct channel* ch, void* object) {
assert(ch != NULL && object != NULL);
pthread_mutex_lock(&ch->lock);
while(!ch->count)
pthread_cond_wait(&ch->signal, &ch->lock);
memcpy(object, (uint8_t*)ch->queue + ch->loc_remove * ch->object_size, ch->object_size);
ch->loc_remove = (ch->loc_remove + 1) % ch->length;
ch->count--;
pthread_cond_signal(&ch->signal);
pthread_mutex_unlock(&ch->lock);
}
void channel_clear(struct channel* ch) {
assert(ch != NULL);
pthread_mutex_lock(&ch->lock);
ch->count = 0;
ch->loc_remove = 0;
ch->loc_insert = 0;
pthread_cond_signal(&ch->signal);
pthread_mutex_unlock(&ch->lock);
}