diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-18 15:10:44 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-18 15:10:44 -0600 |
commit | f24523d46c4da32870e3368f4e0caecafb19090f (patch) | |
tree | 09448e33d8c9d7a309bf9757c2b8e0ee73c5e144 /coroutine_chan.h | |
parent | 4469398272d78adb81968178276180f16cc8e647 (diff) |
tidy, comments
Diffstat (limited to 'coroutine_chan.h')
-rw-r--r-- | coroutine_chan.h | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/coroutine_chan.h b/coroutine_chan.h new file mode 100644 index 0000000..3dfccc5 --- /dev/null +++ b/coroutine_chan.h @@ -0,0 +1,85 @@ +/* coroutine_chan.h - Simple request/response system for coroutine.{h,c} + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * 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_ */ |