rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 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 <debug.h> |
| 32 | #include <list.h> |
| 33 | #include <malloc.h> |
| 34 | #include <string.h> |
| 35 | #include <pow2.h> |
| 36 | #include <err.h> |
| 37 | #include <kernel/thread.h> |
| 38 | #include <kernel/port.h> |
| 39 | |
| 40 | // write ports can be in two states, open and closed, which have a |
| 41 | // different magic number. |
| 42 | |
| 43 | #define WRITEPORT_MAGIC_W 'prtw' |
| 44 | #define WRITEPORT_MAGIC_X 'prtx' |
| 45 | |
| 46 | #define READPORT_MAGIC 'prtr' |
| 47 | #define PORTGROUP_MAGIC 'prtg' |
| 48 | |
| 49 | #define PORT_BUFF_SIZE 8 |
| 50 | #define PORT_BUFF_SIZE_BIG 64 |
| 51 | |
| 52 | #define RESCHEDULE_POLICY 1 |
| 53 | |
| 54 | #define MAX_PORT_GROUP_COUNT 256 |
| 55 | |
| 56 | typedef struct { |
| 57 | uint log2; |
| 58 | uint avail; |
| 59 | uint head; |
| 60 | uint tail; |
| 61 | port_packet_t packet[1]; |
| 62 | } port_buf_t; |
| 63 | |
| 64 | typedef struct { |
| 65 | int magic; |
| 66 | struct list_node node; |
| 67 | port_buf_t* buf; |
| 68 | struct list_node rp_list; |
| 69 | port_mode_t mode; |
| 70 | char name[PORT_NAME_LEN]; |
| 71 | } write_port_t; |
| 72 | |
| 73 | typedef struct { |
| 74 | int magic; |
| 75 | wait_queue_t wait; |
| 76 | struct list_node rp_list; |
| 77 | } port_group_t; |
| 78 | |
| 79 | typedef struct { |
| 80 | int magic; |
| 81 | struct list_node w_node; |
| 82 | struct list_node g_node; |
| 83 | port_buf_t* buf; |
| 84 | void* ctx; |
| 85 | wait_queue_t wait; |
| 86 | write_port_t* wport; |
| 87 | port_group_t* gport; |
| 88 | } read_port_t; |
| 89 | |
| 90 | |
| 91 | static struct list_node write_port_list; |
| 92 | |
| 93 | |
| 94 | static port_buf_t* make_buf(uint pk_count) |
| 95 | { |
| 96 | uint size = sizeof(port_buf_t) + ((pk_count - 1) * sizeof(port_packet_t)); |
| 97 | port_buf_t* buf = (port_buf_t*) malloc(size); |
| 98 | if (!buf) |
| 99 | return NULL; |
| 100 | buf->log2 = log2_uint(pk_count); |
| 101 | buf->head = buf->tail = 0; |
| 102 | buf->avail = pk_count; |
| 103 | return buf; |
| 104 | } |
| 105 | |
| 106 | static status_t buf_write(port_buf_t* buf, const port_packet_t* packets, size_t count) |
| 107 | { |
| 108 | if (buf->avail < count) |
| 109 | return ERR_NOT_ENOUGH_BUFFER; |
| 110 | |
| 111 | for (size_t ix = 0; ix != count; ix++) { |
| 112 | buf->packet[buf->tail] = packets[ix]; |
| 113 | buf->tail = modpow2(++buf->tail, buf->log2); |
| 114 | } |
| 115 | buf->avail -= count; |
| 116 | return NO_ERROR; |
| 117 | } |
| 118 | |
| 119 | static status_t buf_read(port_buf_t* buf, port_result_t* pr) |
| 120 | { |
| 121 | if (buf->avail == valpow2(buf->log2)) |
| 122 | return ERR_NO_MSG; |
| 123 | pr->packet = buf->packet[buf->head]; |
| 124 | buf->head = modpow2(++buf->head, buf->log2); |
| 125 | ++buf->avail; |
| 126 | return NO_ERROR; |
| 127 | } |
| 128 | |
| 129 | // must be called before any use of ports. |
| 130 | void port_init(void) |
| 131 | { |
| 132 | list_initialize(&write_port_list); |
| 133 | } |
| 134 | |
| 135 | status_t port_create(const char* name, port_mode_t mode, port_t* port) |
| 136 | { |
| 137 | if (!name || !port) |
| 138 | return ERR_INVALID_ARGS; |
| 139 | |
| 140 | // only unicast ports can have a large buffer. |
| 141 | if (mode & PORT_MODE_BROADCAST) { |
| 142 | if (mode & PORT_MODE_BIG_BUFFER) |
| 143 | return ERR_INVALID_ARGS; |
| 144 | } |
| 145 | |
| 146 | if (strlen(name) >= PORT_NAME_LEN) |
| 147 | return ERR_INVALID_ARGS; |
| 148 | |
| 149 | // lookup for existing port, return that if found. |
| 150 | write_port_t* wp = NULL; |
| 151 | THREAD_LOCK(state1); |
| 152 | list_for_every_entry(&write_port_list, wp, write_port_t, node) { |
| 153 | if (strcmp(wp->name, name) == 0) { |
| 154 | // can't return closed ports. |
| 155 | if (wp->magic == WRITEPORT_MAGIC_X) |
| 156 | wp = NULL; |
| 157 | THREAD_UNLOCK(state1); |
| 158 | if (wp) { |
| 159 | *port = (void*) wp; |
| 160 | return ERR_ALREADY_EXISTS; |
| 161 | } else { |
| 162 | return ERR_BUSY; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | THREAD_UNLOCK(state1); |
| 167 | |
| 168 | // not found, create the write port and the circular buffer. |
| 169 | wp = calloc(1, sizeof(write_port_t)); |
| 170 | if (!wp) |
| 171 | return ERR_NO_MEMORY; |
| 172 | |
| 173 | wp->magic = WRITEPORT_MAGIC_W; |
| 174 | wp->mode = mode; |
| 175 | strlcpy(wp->name, name, sizeof(wp->name)); |
| 176 | list_initialize(&wp->rp_list); |
| 177 | |
| 178 | uint size = (mode & PORT_MODE_BIG_BUFFER) ? PORT_BUFF_SIZE_BIG : PORT_BUFF_SIZE; |
| 179 | wp->buf = make_buf(size); |
| 180 | if (!wp->buf) { |
| 181 | free(wp); |
| 182 | return ERR_NO_MEMORY; |
| 183 | } |
| 184 | |
| 185 | // todo: race condtion! a port with the same name could have been created |
| 186 | // by another thread at is point. |
| 187 | THREAD_LOCK(state2); |
| 188 | list_add_tail(&write_port_list, &wp->node); |
| 189 | THREAD_UNLOCK(state2); |
| 190 | |
| 191 | *port = (void*)wp; |
| 192 | return NO_ERROR; |
| 193 | } |
| 194 | |
| 195 | status_t port_open(const char* name, void* ctx, port_t* port) |
| 196 | { |
| 197 | if (!name || !port) |
| 198 | return ERR_INVALID_ARGS; |
| 199 | |
| 200 | // assume success; create the read port and buffer now. |
| 201 | read_port_t* rp = calloc(1, sizeof(read_port_t)); |
| 202 | if (!rp) |
| 203 | return ERR_NO_MEMORY; |
| 204 | |
| 205 | rp->magic = READPORT_MAGIC; |
| 206 | wait_queue_init(&rp->wait); |
| 207 | rp->ctx = ctx; |
| 208 | |
| 209 | // |buf| might not be needed, but we always allocate outside the lock. |
| 210 | // this buffer is only needed for broadcast ports, but we don't know |
| 211 | // that here. |
| 212 | port_buf_t* buf = make_buf(PORT_BUFF_SIZE); |
| 213 | if (!buf) { |
| 214 | free(rp); |
| 215 | return ERR_NO_MEMORY; |
| 216 | } |
| 217 | |
| 218 | // find the named write port and associate it with read port. |
| 219 | status_t rc = ERR_NOT_FOUND; |
| 220 | |
| 221 | THREAD_LOCK(state); |
| 222 | write_port_t* wp = NULL; |
| 223 | list_for_every_entry(&write_port_list, wp, write_port_t, node) { |
| 224 | if (strcmp(wp->name, name) == 0) { |
| 225 | // found; add read port to write port list. |
| 226 | rp->wport = wp; |
| 227 | if (wp->buf) { |
| 228 | // this is the first read port; transfer the circular buffer. |
| 229 | list_add_tail(&wp->rp_list, &rp->w_node); |
| 230 | rp->buf = wp->buf; |
| 231 | wp->buf = NULL; |
| 232 | rc = NO_ERROR; |
| 233 | } else if (buf) { |
| 234 | // not first read port. |
| 235 | if (wp->mode & PORT_MODE_UNICAST) { |
| 236 | // cannot add a second listener. |
| 237 | rc = ERR_NOT_ALLOWED; |
| 238 | break; |
| 239 | } |
| 240 | // use the new (small) circular buffer. |
| 241 | list_add_tail(&wp->rp_list, &rp->w_node); |
| 242 | rp->buf = buf; |
| 243 | buf = NULL; |
| 244 | rc = NO_ERROR; |
| 245 | } else { |
| 246 | // |buf| allocation failed and the buffer was needed. |
| 247 | rc = ERR_NO_MEMORY; |
| 248 | } |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | THREAD_UNLOCK(state); |
| 253 | |
| 254 | if (buf) |
| 255 | free(buf); |
| 256 | |
| 257 | if (rc == NO_ERROR) { |
| 258 | *port = (void*)rp; |
| 259 | } else { |
| 260 | free(rp); |
| 261 | } |
| 262 | return rc; |
| 263 | } |
| 264 | |
| 265 | status_t port_group(port_t* ports, size_t count, port_t* group) |
| 266 | { |
| 267 | if (count > MAX_PORT_GROUP_COUNT) |
| 268 | return ERR_TOO_BIG; |
| 269 | |
| 270 | if (!ports || !group) |
| 271 | return ERR_INVALID_ARGS; |
| 272 | |
| 273 | // assume success; create port group now. |
| 274 | port_group_t* pg = calloc(1, sizeof(port_group_t)); |
| 275 | if (!pg) |
| 276 | return ERR_NO_MEMORY; |
| 277 | |
| 278 | pg->magic = PORTGROUP_MAGIC; |
| 279 | wait_queue_init(&pg->wait); |
| 280 | list_initialize(&pg->rp_list); |
| 281 | |
| 282 | status_t rc = NO_ERROR; |
| 283 | |
| 284 | THREAD_LOCK(state); |
| 285 | for (size_t ix = 0; ix != count; ix++) { |
| 286 | read_port_t* rp = (read_port_t*)ports[ix]; |
| 287 | if ((rp->magic != READPORT_MAGIC) || rp->gport) { |
| 288 | // wrong type of port, or port already part of a group, |
| 289 | // in any case, undo the changes to the previous read ports. |
| 290 | for (size_t jx = 0; jx != ix; jx++) { |
| 291 | ((read_port_t*)ports[jx])->gport = NULL; |
| 292 | } |
| 293 | rc = ERR_BAD_HANDLE; |
| 294 | break; |
| 295 | } |
| 296 | // link port group and read port. |
| 297 | rp->gport = pg; |
| 298 | list_add_tail(&pg->rp_list, &rp->g_node); |
| 299 | } |
| 300 | THREAD_UNLOCK(state); |
| 301 | |
| 302 | if (rc == NO_ERROR) { |
| 303 | *group = (port_t*)pg; |
| 304 | } else { |
| 305 | free(pg); |
| 306 | } |
| 307 | return rc; |
| 308 | } |
| 309 | |
| 310 | status_t port_write(port_t port, const port_packet_t* pk, size_t count) |
| 311 | { |
| 312 | if (!port || !pk) |
| 313 | return ERR_INVALID_ARGS; |
| 314 | |
| 315 | write_port_t* wp = (write_port_t*)port; |
| 316 | THREAD_LOCK(state); |
| 317 | if (wp->magic != WRITEPORT_MAGIC_W) { |
| 318 | // wrong port type. |
| 319 | THREAD_UNLOCK(state); |
| 320 | return ERR_BAD_HANDLE; |
| 321 | } |
| 322 | |
| 323 | status_t status = NO_ERROR; |
| 324 | int awake_count = 0; |
| 325 | |
| 326 | if (wp->buf) { |
| 327 | // there are no read ports, just write to the buffer. |
| 328 | status = buf_write(wp->buf, pk, count); |
| 329 | } else { |
| 330 | // there are read ports. for each, write and attempt to wake a thread |
| 331 | // from the port group or from the read port itself. |
| 332 | read_port_t* rp; |
| 333 | list_for_every_entry(&wp->rp_list, rp, read_port_t, w_node) { |
| 334 | if (buf_write(rp->buf, pk, count) < 0) { |
| 335 | // buffer full. |
| 336 | status = ERR_PARTIAL_WRITE; |
| 337 | continue; |
| 338 | } |
| 339 | |
| 340 | int awaken = 0; |
| 341 | if (rp->gport) { |
| 342 | awaken = wait_queue_wake_one(&rp->gport->wait, false, NO_ERROR); |
| 343 | } |
| 344 | if (!awaken) { |
| 345 | awaken = wait_queue_wake_one(&rp->wait, false, NO_ERROR); |
| 346 | } |
| 347 | |
| 348 | awake_count += awaken; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | THREAD_UNLOCK(state); |
| 353 | |
| 354 | #if RESCHEDULE_POLICY |
| 355 | if (awake_count) |
| 356 | thread_yield(); |
| 357 | #endif |
| 358 | |
| 359 | return status; |
| 360 | } |
| 361 | |
| 362 | static inline status_t read_no_lock(read_port_t* rp, lk_time_t timeout, port_result_t* result) |
| 363 | { |
| 364 | status_t status = buf_read(rp->buf, result); |
| 365 | result->ctx = rp->ctx; |
| 366 | |
| 367 | if (status != ERR_NO_MSG) |
| 368 | return status; |
| 369 | |
| 370 | // early return allows compiler to elide the rest for the group read case. |
| 371 | if (!timeout) |
| 372 | return ERR_TIMED_OUT; |
| 373 | |
| 374 | status_t wr = wait_queue_block(&rp->wait, timeout); |
| 375 | if (wr != NO_ERROR) |
| 376 | return wr; |
| 377 | // recursive tail call is usually optimized away with a goto. |
| 378 | return read_no_lock(rp, timeout, result); |
| 379 | } |
| 380 | |
| 381 | status_t port_read(port_t port, lk_time_t timeout, port_result_t* result) |
| 382 | { |
| 383 | if (!port || !result) |
| 384 | return ERR_INVALID_ARGS; |
| 385 | |
| 386 | status_t rc = ERR_GENERIC; |
| 387 | read_port_t* rp = (read_port_t*)port; |
| 388 | |
| 389 | THREAD_LOCK(state); |
| 390 | if (rp->magic == READPORT_MAGIC) { |
| 391 | // dealing with a single port. |
| 392 | rc = read_no_lock(rp, timeout, result); |
| 393 | } else if (rp->magic == PORTGROUP_MAGIC) { |
| 394 | // dealing with a port group. |
| 395 | port_group_t* pg = (port_group_t*)port; |
| 396 | do { |
| 397 | // read each port with no timeout. |
| 398 | // todo: this order is fixed, probably a bad thing. |
| 399 | list_for_every_entry(&pg->rp_list, rp, read_port_t, g_node) { |
| 400 | rc = read_no_lock(rp, 0, result); |
| 401 | if (rc != ERR_TIMED_OUT) |
| 402 | goto read_exit; |
| 403 | } |
| 404 | // no data, block on the group waitqueue. |
| 405 | rc = wait_queue_block(&pg->wait, timeout); |
| 406 | } while (rc == NO_ERROR); |
| 407 | } else { |
| 408 | // wrong port type. |
| 409 | rc = ERR_BAD_HANDLE; |
| 410 | } |
| 411 | |
| 412 | read_exit: |
| 413 | THREAD_UNLOCK(state); |
| 414 | return rc; |
| 415 | } |
| 416 | |
| 417 | status_t port_destroy(port_t port) |
| 418 | { |
| 419 | if (!port) |
| 420 | return ERR_INVALID_ARGS; |
| 421 | |
| 422 | write_port_t* wp = (write_port_t*) port; |
| 423 | port_buf_t* buf = NULL; |
| 424 | |
| 425 | THREAD_LOCK(state); |
| 426 | if (wp->magic != WRITEPORT_MAGIC_X) { |
| 427 | // wrong port type. |
| 428 | THREAD_UNLOCK(state); |
| 429 | return ERR_BAD_HANDLE; |
| 430 | } |
| 431 | // remove self from global named ports list. |
| 432 | list_delete(&wp->node); |
| 433 | |
| 434 | if (wp->buf) { |
| 435 | // we have no readers. |
| 436 | buf = wp->buf; |
| 437 | } else { |
| 438 | // for each reader: |
| 439 | read_port_t* rp; |
| 440 | list_for_every_entry(&wp->rp_list, rp, read_port_t, w_node) { |
| 441 | // wake the read and group ports. |
| 442 | wait_queue_wake_all(&rp->wait, false, ERR_CANCELLED); |
| 443 | if (rp->gport) { |
| 444 | wait_queue_wake_all(&rp->gport->wait, false, ERR_CANCELLED); |
| 445 | } |
| 446 | // remove self from reader ports. |
| 447 | rp->wport = NULL; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | wp->magic = 0; |
| 452 | THREAD_UNLOCK(state); |
| 453 | |
| 454 | free(buf); |
| 455 | free(wp); |
| 456 | return NO_ERROR; |
| 457 | } |
| 458 | |
| 459 | status_t port_close(port_t port) |
| 460 | { |
| 461 | if (!port) |
| 462 | return ERR_INVALID_ARGS; |
| 463 | |
| 464 | read_port_t* rp = (read_port_t*) port; |
| 465 | port_buf_t* buf = NULL; |
| 466 | |
| 467 | THREAD_LOCK(state); |
| 468 | if (rp->magic == READPORT_MAGIC) { |
| 469 | // dealing with a read port. |
| 470 | if (rp->wport) { |
| 471 | // remove self from write port list and reassign the bufer if last. |
| 472 | list_delete(&rp->w_node); |
| 473 | if (list_is_empty(&rp->wport->rp_list)) { |
| 474 | rp->wport->buf = rp->buf; |
| 475 | rp->buf = NULL; |
| 476 | } else { |
| 477 | buf = rp->buf; |
| 478 | } |
| 479 | } |
| 480 | if (rp->gport) { |
| 481 | // remove self from port group list. |
| 482 | list_delete(&rp->g_node); |
| 483 | } |
| 484 | // wake up waiters, the return code is ERR_OBJECT_DESTROYED. |
| 485 | wait_queue_destroy(&rp->wait, true); |
| 486 | rp->magic = 0; |
| 487 | |
| 488 | } else if (rp->magic == PORTGROUP_MAGIC) { |
| 489 | // dealing with a port group. |
| 490 | port_group_t* pg = (port_group_t*) port; |
| 491 | // wake up waiters. |
| 492 | wait_queue_destroy(&pg->wait, true); |
| 493 | // remove self from reader ports. |
| 494 | rp = NULL; |
| 495 | list_for_every_entry(&pg->rp_list, rp, read_port_t, g_node) { |
| 496 | rp->gport = NULL; |
| 497 | } |
| 498 | pg->magic = 0; |
| 499 | |
| 500 | } else if (rp->magic == WRITEPORT_MAGIC_W) { |
| 501 | // dealing with a write port. |
| 502 | write_port_t* wp = (write_port_t*) port; |
| 503 | // mark it as closed. Now it can be read but not written to. |
| 504 | wp->magic = WRITEPORT_MAGIC_X; |
| 505 | THREAD_UNLOCK(state); |
| 506 | return NO_ERROR; |
| 507 | |
| 508 | } else { |
| 509 | THREAD_UNLOCK(state); |
| 510 | return ERR_BAD_HANDLE; |
| 511 | } |
| 512 | |
| 513 | THREAD_UNLOCK(state); |
| 514 | |
| 515 | free(buf); |
| 516 | free(port); |
| 517 | return NO_ERROR; |
| 518 | } |
| 519 | |