Skip to content

Commit 6d29ac0

Browse files
committed
Make coroutine function be declared in C-Style
Macro cr_proto is introduced to replace cr_define, which makes programmers to declare a coroutine function in C-style, that is, more flexible than cr_define. Macro cr_run is modified to make respond to cr_proto. The new version of cr_run accepts variable length argument list. From now, programmers could specify arguments that are going to be passed to coroutines, by listing arguments after name while starting coroutine with macro cr_run. Since the way of passing arguments was changed, the field arg in structure cr is removed and macros that aimed to fetch value from arg are also removed.
1 parent c23c0a1 commit 6d29ac0

File tree

1 file changed

+15
-30
lines changed

1 file changed

+15
-30
lines changed

tinync/tinync.c

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,21 @@ enum {
1414
struct cr {
1515
void *label;
1616
int status;
17-
void *arg;
1817
};
1918

2019
#define cr_context_name(name) __cr_context_##name
2120
#define cr_context(name) struct cr cr_context_name(name)
22-
#define cr_context_init(argptr) \
23-
{ \
24-
.label = NULL, .status = CR_BLOCKED, .arg = argptr \
21+
#define cr_context_init() \
22+
{ \
23+
.label = NULL, .status = CR_BLOCKED \
2524
}
2625

2726
#define cr_func_name(name) __cr_func_##name
28-
#define cr_define(name) static void cr_func_name(name)(struct cr * ctx)
27+
#define cr_proto(name, ...) cr_func_name(name)(struct cr * ctx, ##__VA_ARGS__)
2928

30-
#define cr_run(name) cr_func_name(name)(&cr_context_name(name))
29+
#define cr_run(name, ...) \
30+
cr_func_name(name)(&cr_context_name(name), ##__VA_ARGS__)
3131

32-
#define cr_arg(type) (type *) (ctx->arg)
33-
#define cr_arg_member(type, memb) &((type *) (ctx->arg))->memb;
3432
#define cr_local static
3533

3634
#define cr_begin() \
@@ -99,14 +97,8 @@ struct cr {
9997

10098
typedef cr_queue(uint8_t, 4096) byte_queue_t;
10199

102-
typedef struct {
103-
int fd;
104-
byte_queue_t *queue;
105-
} conn_data_t;
106-
107-
cr_define(stdin_loop)
100+
static void cr_proto(stdin_loop, byte_queue_t *out)
108101
{
109-
byte_queue_t *out = cr_arg(byte_queue_t);
110102
cr_local uint8_t b;
111103
cr_local int r;
112104
cr_begin();
@@ -122,10 +114,8 @@ cr_define(stdin_loop)
122114
cr_end();
123115
}
124116

125-
cr_define(socket_write_loop)
117+
static void cr_proto(socket_write_loop, byte_queue_t *in, int fd)
126118
{
127-
byte_queue_t *in = *cr_arg_member(conn_data_t, queue);
128-
int fd = *cr_arg_member(conn_data_t, fd);
129119
cr_local uint8_t *b;
130120
cr_begin();
131121
for (;;) {
@@ -136,9 +126,8 @@ cr_define(socket_write_loop)
136126
cr_end();
137127
}
138128

139-
cr_define(socket_read_loop)
129+
static void cr_proto(socket_read_loop, int fd)
140130
{
141-
int fd = *cr_arg(int);
142131
cr_local uint8_t b;
143132
cr_local int r;
144133
cr_begin();
@@ -198,14 +187,10 @@ int main(int argc, char *argv[])
198187
connect(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
199188

200189
byte_queue_t queue = cr_queue_init();
201-
conn_data_t conn_data = {
202-
.fd = fd,
203-
.queue = &queue,
204-
};
205190

206-
cr_context(stdin_loop) = cr_context_init(&queue);
207-
cr_context(socket_read_loop) = cr_context_init(&fd);
208-
cr_context(socket_write_loop) = cr_context_init(&conn_data);
191+
cr_context(stdin_loop) = cr_context_init();
192+
cr_context(socket_read_loop) = cr_context_init();
193+
cr_context(socket_write_loop) = cr_context_init();
209194

210195
while (cr_status(stdin_loop) == CR_BLOCKED &&
211196
cr_status(socket_read_loop) == CR_BLOCKED) {
@@ -216,9 +201,9 @@ int main(int argc, char *argv[])
216201
FD_SET(fd, &fds);
217202
select(fd + 1, &fds, NULL, NULL, NULL);
218203
}
219-
cr_run(socket_read_loop);
220-
cr_run(socket_write_loop);
221-
cr_run(stdin_loop);
204+
cr_run(socket_read_loop, fd);
205+
cr_run(socket_write_loop, &queue, fd);
206+
cr_run(stdin_loop, &queue);
222207
}
223208

224209
close(fd);

0 commit comments

Comments
 (0)