summaryrefslogtreecommitdiff
path: root/libcr_ipc/coroutine_rpc.h
blob: 11d40d7c5b1c266c8fa821554b86a63c45125b03 (plain)
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
/* coroutine_rpc.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_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 <assert.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`.
 *
 * There may be multiple concurrent requesters, but only one
 * concurrent responder.  If you need multiple concurrent responders,
 * set up a system of coroutine_chan.h channels.
 */
#define cr_rpc_t(req_t, resp_t) struct {            \
		cid_t   requester, *tail_requester; \
		cid_t   responder;                  \
		req_t  *req_p;                      \
		resp_t *resp_p;                     \
	}

/* These "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`.
 *
 * Blocking: Always; until the responder has called both
 * cr_rpc_recv_req() and cr_rpc_send_resp().
 */
#define cr_rpc_req(ch, _resp_p, _req) do {                    \
		cid_t next = 0;                               \
		if ((ch)->requester) {                        \
			*((ch)->tail_requester = cr_getcid(); \
			(ch)->tail_requester = &next;         \
			cr_pause_and_yield();                 \
		} else {                                      \
			(ch)->requester = cr_getcid();        \
			(ch)->tail_requester = &next;         \
		}                                             \
		assert((ch)->requester == cr_getcid());       \
		(ch)->req_p = &(_req);                        \
		(ch)->resp_p = (_resp_p;                      \
		if ((ch)->responder != 0)                     \
			cr_unpause((ch)->responder);          \
		cr_pause_and_yield();                         \
		if (next) {                                   \
			(ch)->requester = next;               \
			cr_unpause(next);                     \
		} else {                                      \
			(ch)->requester = 0;                  \
			(ch)->tail_requester = NULL;          \
		}
	} 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.
 *
 * Blocking: Never.
 */
#define cr_rpc_have_req(ch) ((ch)->req_p != NULL)

/**
 * 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.
 *
 * Blocking: Maybe.
 */
#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_p);    \
		(ch)->req_p = NULL;            \
	} 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.
 *
 * Blocking: Never.
 */
#define cr_rpc_send_resp(ch, _resp) do {       \
		(ch)->responder = 0;           \
		*((ch)->resp_p) = (_resp);     \
		(ch)->resp_p = NULL;           \
		cr_unpause((ch)->requester);   \
	} while (0)

#endif /* _COROUTINE_RPC_H_ */