/
chan.c
246 lines (194 loc) · 5.31 KB
/
chan.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* @file chan.c
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#include "simba.h"
#define CHAN_LIST_POLLING 0x1
static struct chan_t null;
int chan_module_init(void)
{
chan_init(&null, chan_read_null, chan_write_null, chan_size_null);
return (0);
}
int chan_init(struct chan_t *self_p,
chan_read_fn_t read,
chan_write_fn_t write,
chan_size_fn_t size)
{
ASSERTN(self_p != NULL, EINVAL);
ASSERTN(read != NULL, EINVAL);
ASSERTN(write != NULL, EINVAL);
ASSERTN(size != NULL, EINVAL);
self_p->read = read;
self_p->write = write;
self_p->size = size;
self_p->writer_p = NULL;
self_p->reader_p = NULL;
self_p->list_p = NULL;
return (0);
}
int chan_list_init(struct chan_list_t *list_p,
void *workspace_p,
size_t size)
{
ASSERTN(list_p != NULL, EINVAL);
ASSERTN(workspace_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
list_p->max = (size / sizeof(list_p->chans_pp[0]));
if (list_p->max == 0) {
return (-EINVAL);
}
list_p->chans_pp = workspace_p;
list_p->len = 0;
list_p->flags = 0;
return (0);
}
int chan_list_destroy(struct chan_list_t *list_p)
{
ASSERTN(list_p != NULL, EINVAL);
struct chan_t *chan_p = NULL;
size_t i;
sys_lock();
for (i = 0; i < list_p->len; i++) {
chan_p = list_p->chans_pp[i];
chan_p->list_p = NULL;
}
sys_unlock();
return (0);
}
ssize_t chan_read(void *self_p,
void *buf_p,
size_t size)
{
ASSERTN(self_p != NULL, EINVAL);
ASSERTN(buf_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
return (((struct chan_t *)self_p)->read(self_p, buf_p, size));
}
ssize_t chan_write(void *self_p,
const void *buf_p,
size_t size)
{
ASSERTN(self_p != NULL, EINVAL);
ASSERTN(buf_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
return (((struct chan_t *)self_p)->write(self_p, buf_p, size));
}
size_t chan_size(void *self_p)
{
ASSERTN(self_p != NULL, EINVAL);
return (((struct chan_t *)self_p)->size(self_p));
}
int chan_list_add(struct chan_list_t *list_p, void *chan_p)
{
ASSERTN(list_p != NULL, EINVAL);
ASSERTN(chan_p != NULL, EINVAL);
if (list_p->len == list_p->max) {
return (-ENOMEM);
}
list_p->chans_pp[list_p->len] = chan_p;
list_p->len++;
return (0);
}
int chan_list_remove(struct chan_list_t *list_p, void *chan_p)
{
ASSERTN(list_p != NULL, EINVAL);
ASSERTN(chan_p != NULL, EINVAL);
int i;
for (i = 0; i < list_p->max; i++) {
if (list_p->chans_pp[i] == chan_p) {
list_p->max--;
list_p->chans_pp[i] = list_p->chans_pp[list_p->max];
return (0);
}
}
return (-1);
}
void *chan_list_poll(struct chan_list_t *list_p,
struct time_t *timeout_p)
{
ASSERTN(list_p != NULL, EINVAL);
struct chan_t *chan_p = NULL;
size_t i;
sys_lock();
while (1) {
/* Check if data is available on any channel. */
for (i = 0; i < list_p->len; i++) {
chan_p = list_p->chans_pp[i];
if (chan_p->size(chan_p) > 0) {
goto out;
}
}
/* Add the thread as a reader on all channels.*/
list_p->flags = CHAN_LIST_POLLING;
for (i = 0; i < list_p->len; i++) {
chan_p = list_p->chans_pp[i];
chan_p->reader_p = thrd_self();
chan_p->list_p = list_p;
}
/* Not data was available, wait for data to be written to one
of the channels. */
if (thrd_suspend_isr(timeout_p) == -ETIMEDOUT) {
chan_p = NULL;
goto out;
}
}
out:
sys_unlock();
return (chan_p);
}
void *chan_poll(void *chan_p, struct time_t *timeout_p)
{
void *res_p;
struct chan_list_t list;
struct chan_t *workspace_p;
chan_list_init(&list, &workspace_p, sizeof(workspace_p));
chan_list_add(&list, chan_p);
res_p = chan_list_poll(&list, timeout_p);
chan_list_remove(&list, chan_p);
return (res_p);
}
void *chan_null(void)
{
return (&null);
}
ssize_t chan_read_null(void *self_p,
void *buf_p,
size_t size)
{
return (-1);
}
ssize_t chan_write_null(void *self_p,
const void *buf_p,
size_t size)
{
return (size);
}
size_t chan_size_null(void *self_p)
{
return (0);
}
int chan_is_polled_isr(struct chan_t *self_p)
{
if (self_p->list_p != NULL) {
if (self_p->list_p->flags & CHAN_LIST_POLLING) {
self_p->list_p->flags = 0;
return (1);
}
}
return (0);
}