From d282266beb71bfb9b21cf00f3025c01df7dd8e4d Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Tue, 24 Sep 2024 14:16:35 -0600 Subject: rename cr_chan to cr_rpc --- coroutine.h | 6 ++-- coroutine_chan.h | 85 -------------------------------------------------------- coroutine_rpc.h | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 6 ++-- usb_keyboard.c | 8 +++--- usb_keyboard.h | 4 +-- 6 files changed, 96 insertions(+), 97 deletions(-) delete mode 100644 coroutine_chan.h create mode 100644 coroutine_rpc.h diff --git a/coroutine.h b/coroutine.h index d34ff2e..4b16b1f 100644 --- a/coroutine.h +++ b/coroutine.h @@ -17,8 +17,8 @@ * as normal C functions and do not forbid switch() blocks in * coroutines. * - * See also: coroutine_chan.h is a request/response system built on - * top of coroutine.{h,c}. + * See also: coroutine_rpc.h is a request/response system built on top + * of coroutine.{h,c}. */ #ifndef _COROUTINE_H_ #define _COROUTINE_H_ @@ -66,7 +66,7 @@ typedef size_t cid_t; * Specifically, coroutine_add() and * cr_{yield,pause_and_yield,exit,end}() are explicitly forbidden to * call from within a coroutine before cr_begin() (note that the - * cr_chan_*() macros call these functions). + * cr_rpc_*() macros call these functions). */ typedef void (*cr_fn_t)(void *args); #define COROUTINE __attribute__ ((noreturn)) void diff --git a/coroutine_chan.h b/coroutine_chan.h deleted file mode 100644 index 3dfccc5..0000000 --- a/coroutine_chan.h +++ /dev/null @@ -1,85 +0,0 @@ -/* coroutine_chan.h - Simple request/response system for coroutine.{h,c} - * - * Copyright (C) 2024 Luke T. Shumaker - * SPDX-Licence-Identifier: AGPL-3.0-or-later - */ - -/** - * The cr_chan_* macros form a simple request/response channel - * mechanism built on top of the cr_pause_and_yeild() and cr_unpause() - * primitives. - */ -#ifndef _COROUTINE_CHAN_H_ -#define _COROUTINE_CHAN_H_ - -#include "coroutine.h" - -/** - * cr_chan_t(req_t, resp_t) returns the type definition for a channel - * on which the requester submits a value of type `req_t` and the - * responder responds with a value of type `resp_t`. - */ -#define cr_chan_t(req_t, resp_t) struct { \ - cid_t requester; \ - cid_t responder; \ - req_t req; \ - resp_t resp; \ - } - -/* These are "functions" are preprocessor macros instead of real C - * functions so that the compiler can do type-checking instead of - * having these functions take `void*`. */ - -/** - * ch_chan_req(cr_chan_t(req_t, resp_t) *ch, resp_t *resp, req_t req) - * submits the `req` request to `ch` puts the response in `*resp_p`. - * - * Blocks until the responder has called both cr_chan_recv_req() and - * cr_chan_send_resp(). - */ -#define cr_chan_req(ch, _resp_p, _req) do { \ - (ch)->requester = cr_getcid(); \ - (ch)->req = (_req); \ - if ((ch)->responder != 0) \ - cr_unpause((ch)->responder); \ - cr_pause_and_yield(); \ - if ((typeof(&(ch)->resp))(_resp_p)) \ - *((typeof(&(ch)->resp))(_resp_p)) = (ch)->resp; \ - } while (0) - -/** - * cr_chan_have_req(cr_chan_t(req_t, resp_t) *ch) allows a responder - * to check whether or not there is a request waiting to be received - * (with cr_chan_recv_req()) without blocking if there is not. - * - * Never blocks. - */ -#define cr_chan_have_req(ch) ((ch)->requester != 0) - -/** - * cr_chan_recv_req(cr_chan_t(req_t, resp_t) *ch, req_t *req_p) reads - * a request from ch into `*req_p`. - * - * If there is not a pending request on `ch`, blocks until there is. - */ -#define cr_chan_recv_req(ch, _req_p) do { \ - (ch)->responder = cr_getcid(); \ - if ((ch)->requester == 0) \ - cr_pause_and_yield(); \ - *(_req_p) = (ch)->req; \ - } while (0) - -/** - * cr_chan_send_resp(cr_chan_t(req_t, resp_t) *ch, resp_t resp) sends - * the reply to the most-recently-read request. - * - * Never blocks. - */ -#define cr_chan_send_resp(ch, _resp) do { \ - cr_unpause((ch)->requester); \ - (ch)->responder = 0; \ - (ch)->requester = 0; \ - (ch)->resp = (_resp); \ - } while (0) - -#endif /* _COROUTINE_CHAN_H_ */ diff --git a/coroutine_rpc.h b/coroutine_rpc.h new file mode 100644 index 0000000..00aa165 --- /dev/null +++ b/coroutine_rpc.h @@ -0,0 +1,84 @@ +/* coroutine_rpc.h - Simple request/response system for coroutine.{h,c} + * + * Copyright (C) 2024 Luke T. Shumaker + * SPDX-Licence-Identifier: AGPL-3.0-or-later + */ + +/** + * The cr_rpc_* macros form a simple request/response mechanism built + * on top of the cr_pause_and_yeild() and cr_unpause() primitives. + */ +#ifndef _COROUTINE_RPC_H_ +#define _COROUTINE_RPC_H_ + +#include "coroutine.h" + +/** + * cr_rpc_t(req_t, resp_t) returns the type definition for a + * rcp-channel on which the requester submits a value of type `req_t` + * and the responder responds with a value of type `resp_t`. + */ +#define cr_rpc_t(req_t, resp_t) struct { \ + cid_t requester; \ + cid_t responder; \ + req_t req; \ + resp_t resp; \ + } + +/* These are "functions" are preprocessor macros instead of real C + * functions so that the compiler can do type-checking instead of + * having these functions take `void*`. */ + +/** + * ch_rpc_req(cr_rpc_t(req_t, resp_t) *ch, resp_t *resp, req_t req) + * submits the `req` request to `ch` puts the response in `*resp_p`. + * + * Blocks until the responder has called both cr_rpc_recv_req() and + * cr_rpc_send_resp(). + */ +#define cr_rpc_req(ch, _resp_p, _req) do { \ + (ch)->requester = cr_getcid(); \ + (ch)->req = (_req); \ + if ((ch)->responder != 0) \ + cr_unpause((ch)->responder); \ + cr_pause_and_yield(); \ + if ((typeof(&(ch)->resp))(_resp_p)) \ + *((typeof(&(ch)->resp))(_resp_p)) = (ch)->resp; \ + } while (0) + +/** + * cr_rpc_have_req(cr_rpc_t(req_t, resp_t) *ch) allows a responder + * to check whether or not there is a request waiting to be received + * (with cr_rpc_recv_req()) without blocking if there is not. + * + * Never blocks. + */ +#define cr_rpc_have_req(ch) ((ch)->requester != 0) + +/** + * cr_rpc_recv_req(cr_rpc_t(req_t, resp_t) *ch, req_t *req_p) reads + * a request from ch into `*req_p`. + * + * If there is not a pending request on `ch`, blocks until there is. + */ +#define cr_rpc_recv_req(ch, _req_p) do { \ + (ch)->responder = cr_getcid(); \ + if ((ch)->requester == 0) \ + cr_pause_and_yield(); \ + *(_req_p) = (ch)->req; \ + } while (0) + +/** + * cr_rpc_send_resp(cr_rpc_t(req_t, resp_t) *ch, resp_t resp) sends + * the reply to the most-recently-read request. + * + * Never blocks. + */ +#define cr_rpc_send_resp(ch, _resp) do { \ + cr_unpause((ch)->requester); \ + (ch)->responder = 0; \ + (ch)->requester = 0; \ + (ch)->resp = (_resp); \ + } while (0) + +#endif /* _COROUTINE_RPC_H_ */ diff --git a/main.c b/main.c index 30a6dfc..1fd9f8c 100644 --- a/main.c +++ b/main.c @@ -17,11 +17,11 @@ COROUTINE hello_world_cr(void *_chan) { const char *msg = "Hello world!\n"; - usb_keyboard_chan_t *chan = _chan; + usb_keyboard_rpc_t *chan = _chan; cr_begin(); for (size_t i = 0;; i = (i+1) % strlen(msg)) { - cr_chan_req(chan, NULL, msg[i]); + cr_rpc_req(chan, NULL, msg[i]); } cr_end(); @@ -39,7 +39,7 @@ int main() { /* set up coroutines */ coroutine_add(usb_common_cr, NULL); - usb_keyboard_chan_t keyboard_chan = {0}; + usb_keyboard_rpc_t keyboard_chan = {0}; coroutine_add(usb_keyboard_cr, &keyboard_chan); coroutine_add(hello_world_cr, &keyboard_chan); diff --git a/usb_keyboard.c b/usb_keyboard.c index e6638ec..1989b35 100644 --- a/usb_keyboard.c +++ b/usb_keyboard.c @@ -43,7 +43,7 @@ void usb_keyboard_init() { static uint8_t ascii2keycode[128][2] = { HID_ASCII_TO_KEYCODE }; COROUTINE usb_keyboard_cr(void *_chan) { - usb_keyboard_chan_t *chan = _chan; + usb_keyboard_rpc_t *chan = _chan; cr_begin(); uint8_t report_id = 0; @@ -53,9 +53,9 @@ COROUTINE usb_keyboard_cr(void *_chan) { while (!tud_hid_n_ready(kbd_ifc)) cr_yield(); - if (cr_chan_have_req(chan)) { + if (cr_rpc_have_req(chan)) { uint32_t rune; - cr_chan_recv_req(chan, &rune); + cr_rpc_recv_req(chan, &rune); modifier = ascii2keycode[rune][0] ? KEYBOARD_MODIFIER_LEFTSHIFT : 0; keycodes[0] = ascii2keycode[rune][1]; @@ -68,7 +68,7 @@ COROUTINE usb_keyboard_cr(void *_chan) { keycodes[0] = 0; tud_hid_n_keyboard_report(kbd_ifc, report_id, modifier, keycodes); - cr_chan_send_resp(chan, 1); + cr_rpc_send_resp(chan, 1); } else { modifier = 0; keycodes[0] = 0; diff --git a/usb_keyboard.h b/usb_keyboard.h index 656add8..6b65360 100644 --- a/usb_keyboard.h +++ b/usb_keyboard.h @@ -7,9 +7,9 @@ #ifndef _USB_KEYBOARD_H_ #define _USB_KEYBOARD_H_ -#include "coroutine_chan.h" +#include "coroutine_rpc.h" -typedef cr_chan_t(uint32_t, int) usb_keyboard_chan_t; +typedef cr_rpc_t(uint32_t, int) usb_keyboard_rpc_t; void usb_keyboard_init(void); COROUTINE usb_keyboard_cr(void *arg); -- cgit v1.2.3-2-g168b