Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: kernel/port.c

Issue 1437453002: [kernel][ports] Add basic ports functionality (Closed) Base URL: https://github.com/travisg/lk.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2015 Carlos Pizano-Uribe cpu@chromium.org
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * @file
26 * @brief Port object functions
27 * @defgroup event Events
28 *
29 */
30
31 #include <kernel/port.h>
32 #include <debug.h>
33 #include <list.h>
34 #include <malloc.h>
35 #include <assert.h>
36 #include <string.h>
37 #include <err.h>
38 #include <kernel/thread.h>
39
40 #define WRITEPORT_MAGIC 'prtw'
41 #define READPORT_MAGIC 'prtr'
42 #define PORTGROUP_MAGIC 'prtg'
43
44 #define PORT_BUFF_SIZE 8
45 #define PORT_BUFF_SIZE_BIG 64
46
47 #define RESCHEDULE_POLICY true
48
49 typedef struct {
50 uint max;
51 uint head;
52 uint tail;
53 port_packet_t packet[1];
54 } port_buf_t;
55
56 typedef struct {
57 int magic;
58 struct list_node node;
59 char name[PORT_NAME_LEN];
travisg 2015/11/09 23:14:16 should probably put name last, to make sure thumb
cpu_(ooo_6.6-7.5) 2015/11/11 00:31:13 Done.
60 struct list_node rp_list;
61 port_buf_t* buf;
62 } write_port_t;
63
64 // todo (cpu) add the |mode| bit to the write port and check
65 // that unicast ports have only one read port.
66
67 typedef struct {
68 int magic;
69 wait_queue_t wait;
70 struct list_node rp_list;
71 } port_group_t;
72
73 typedef struct {
74 int magic;
75 struct list_node node;
76 wait_queue_t wait;
77 void* ctx;
78 write_port_t* wport;
79 port_group_t* gport;
80 port_buf_t* buf;
81 } read_port_t;
82
83
84 static struct list_node write_port_list;
85
86
87 static port_buf_t* make_buf(uint pk_count) {
88 uint size = sizeof(port_buf_t) + ((pk_count - 1) * sizeof(port_packet_t));
89 port_buf_t* buf = (port_buf_t*) malloc(size);
90 if (!buf)
91 return NULL;
travisg 2015/11/09 23:14:16 weird indent here. Is there a mixture of spaces an
cpu_(ooo_6.6-7.5) 2015/11/11 00:31:12 Done.
92 buf->max = pk_count;
93 buf->head = buf->tail = 0;
94 return buf;
95 }
96
97 static int buf_write(const port_packet_t* packets, size_t count, port_buf_t* buf )
98 {
99 // todo (cpu). circular buffer write.
100 return 0;
101 }
102
103 static int buf_read(port_buf_t* buf, port_result_t* pr)
104 {
105 // todo (cpu). circular buffer read.
106 return 0;
107 }
108
109 // must be called before any use of ports.
110 void port_init(void)
111 {
112 list_initialize(&write_port_list);
113 }
114
115 status_t port_create(const char* name, port_mode_t mode, port_t* port)
116 {
travisg 2015/11/09 23:14:16 test for !name and !port and return ERR_INVALID_AR
cpu_(ooo_6.6-7.5) 2015/11/11 00:31:13 Done.
117 // only unicast ports can have a large buffer.
118 if (mode & port_mode_broadcast) {
119 if (mode & port_mode_big_buffer)
120 return ERR_INVALID_ARGS;
121 }
122
123 // lookup for existing port, return that if found.
124 write_port_t* wp = NULL;
125 THREAD_LOCK(state1);
126 list_for_every_entry(&write_port_list, wp, write_port_t, node) {
127 if (strcmp(wp->name, name) == 0) {
128 THREAD_UNLOCK(state1);
129 *port = (void*)wp;
130 return NO_ERROR;
131 }
132 }
133 THREAD_UNLOCK(state1);
134
135 // not found, create the write port and the circular buffer.
136 wp = malloc(sizeof(write_port_t));
137 if (!wp)
138 return ERR_NO_MEMORY;
139
140 memset(wp, 0, sizeof(write_port_t));
141 wp->magic = WRITEPORT_MAGIC;
142 strlcpy(wp->name, name, sizeof(wp->name));
143 list_initialize(&wp->rp_list);
144
145 uint size = mode & port_mode_big_buffer ? PORT_BUFF_SIZE_BIG : PORT_BUFF_SI ZE;
146 wp->buf = make_buf(size);
travisg 2015/11/09 23:14:16 test and unroll wp->buf == NULL here.
cpu_(ooo_6.6-7.5) 2015/11/11 00:31:13 Done.
147
148 THREAD_LOCK(state2);
149 list_add_tail(&write_port_list, &wp->node);
travisg 2015/11/09 23:14:16 add comment that there's a bit of a race here and
cpu_(ooo_6.6-7.5) 2015/11/11 00:31:13 good catch! I'll do that now and later on I'll fi
150 THREAD_UNLOCK(state2);
151
152 *port = (void*)wp;
153 return NO_ERROR;
154 }
155
156 status_t port_open(const char* name, void* ctx, port_t* port)
157 {
158 // assume success; create the read port and buffer now.
159 read_port_t* rp = malloc(sizeof(read_port_t));
160 if (!rp)
161 return ERR_NO_MEMORY;
162
163 memset(rp, 0, sizeof(read_port_t));
164 rp->magic = READPORT_MAGIC;
165 wait_queue_init(&rp->wait);
166 rp->ctx = ctx;
167
168 port_buf_t* buf = make_buf(PORT_BUFF_SIZE);
travisg 2015/11/09 23:14:16 test for !buf
169
170 // find the named write port.
171 THREAD_LOCK(state);
172 write_port_t* wp = NULL;
173 list_for_every_entry(&write_port_list, wp, write_port_t, node) {
174 if (strcmp(wp->name, name) == 0) {
175 // found; add read port to write port list.
176 list_add_tail(&wp->rp_list, &rp->node);
177 rp->wport = wp;
178 if (wp->buf) {
179 // this is the first read port; transfer the circular buffer.
180 rp->buf = wp->buf;
181 wp->buf = NULL;
182 } else {
183 // not first read port; use the new (small) circular buffer.
184 rp->buf = buf;
185 buf = NULL;
186 }
187 THREAD_UNLOCK(state);
188 *port = (void*)rp;
189 return NO_ERROR;
190 }
191 }
192
193 // named port not found.
194 THREAD_UNLOCK(state);
195 free(buf);
196 free(rp);
197 return ERR_NOT_FOUND;
198 }
199
200 status_t port_group(port_t* ports, size_t count, port_t* group)
201 {
202 // assume success; create port group now.
203 port_group_t* pg = malloc(sizeof(port_group_t));
204 if (!pg)
205 return ERR_NO_MEMORY;
206
207 memset(pg, 0, sizeof(port_group_t));
208 pg->magic = PORTGROUP_MAGIC;
209 wait_queue_init(&pg->wait);
210 list_initialize(&pg->rp_list);
211
212 THREAD_LOCK(state);
213 for(size_t ix = 0; ix != count; ix++) {
214 read_port_t* rp = (read_port_t*)ports[ix];
215 if ((rp->magic != READPORT_MAGIC) || rp->gport) {
216 // wrong type of port, or port already part of a group,
217 // in any case, undo the changes to the previous read ports.
218 for (size_t jx = 0; jx != ix; jx++) {
219 ((read_port_t*)ports[jx])->gport = NULL;
220 }
221 THREAD_UNLOCK(state);
222 free(pg);
223 return ERR_BAD_HANDLE;
224 }
225 // link port group and read port.
226 rp->gport = pg;
227 list_add_tail(&pg->rp_list, &rp->node);
228 }
229 THREAD_UNLOCK(state);
230 *group = (port_t*)pg;
231 return NO_ERROR;
232 }
233
234 status_t port_write(port_t port, const port_packet_t* pk, size_t count)
235 {
236 write_port_t* wp = (write_port_t*)port;
237 THREAD_LOCK(state);
238 if (wp->magic != WRITEPORT_MAGIC) {
239 // wrong port type.
240 THREAD_UNLOCK(state);
241 return ERR_BAD_HANDLE;
242 }
243
244 if (wp->buf) {
245 // there are no read ports, just write to the buffer.
246 buf_write(pk, count, wp->buf);
247 } else {
248 // there are read ports. for each, write and attempt to wake a thread
249 // from the port group or from the read port itself.
250 read_port_t* rp;
251 list_for_every_entry(&wp->rp_list, rp, read_port_t, node) {
252 buf_write(pk, count, rp->buf);
253
254 int count = 0;
255 if (rp->gport) {
256 count = wait_queue_wake_one(&rp->gport->wait, RESCHEDULE_POLICY, NO_ERROR);
257 }
258 if (!count) {
259 wait_queue_wake_one(&rp->wait, RESCHEDULE_POLICY, NO_ERROR);
260 }
261 }
262 }
263
264 THREAD_UNLOCK(state);
265 return NO_ERROR;
266 }
267
268 static inline status_t read_no_lock(read_port_t* rp, lk_time_t timeout, port_res ult_t* result)
269 {
270 int read = buf_read(rp->buf, result);
271 if (read > 0) {
272 result->ctx = rp->ctx;
273 return NO_ERROR;
274 } else if (read < 0) {
275 return (status_t)read;
276 }
277 status_t rc = wait_queue_block(&rp->wait, timeout);
travisg 2015/11/09 23:14:16 should probably test for timeout == 0 and not both
cpu_(ooo_6.6-7.5) 2015/11/11 00:31:13 Done.
278 if (rc != NO_ERROR)
279 return rc;
280 // recursive tail call is usually optimized away with a goto.
281 return read_no_lock(rp, timeout, result);
282 }
283
284 status_t port_read(port_t port, lk_time_t timeout, port_result_t* result)
285 {
286 status_t rc = ERR_GENERIC;
287 read_port_t* rp = (read_port_t*)port;
288
289 THREAD_LOCK(state);
290 if (rp->magic == READPORT_MAGIC) {
291 // dealing with a single port.
292 rc = read_no_lock(rp, timeout, result);
293 } else if (rp->magic == PORTGROUP_MAGIC) {
294 // dealing with a port group.
295 port_group_t* pg = (port_group_t*)port;
296 do {
297 // read each port with no timeout.
298 list_for_every_entry(&pg->rp_list, rp, read_port_t, node) {
299 rc = read_no_lock(rp, 0, result);
300 if (rc != ERR_TIMED_OUT)
301 goto read_exit;
302 }
303 // no data, block on the group waitqueue.
304 rc = wait_queue_block(&pg->wait, timeout);
305 } while (rc == NO_ERROR);
306 } else {
307 // wrong port type.
308 rc = ERR_BAD_HANDLE;
309 }
310
311 read_exit:
312 THREAD_UNLOCK(state);
313 return rc;
314 }
315
316 status_t port_destroy(port_t port)
317 {
318 // todo (cpu)
319 return NO_ERROR;
320 }
321
322 status_t port_close(port_t port)
323 {
324 // todo (cpu)
325 return NO_ERROR;
326 }
327
OLDNEW
« include/kernel/port.h ('K') | « include/kernel/port.h ('k') | kernel/rules.mk » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698