|
1 | 1 | # Tinync
|
2 |
| -Tinync is a simplified version of [nc](https://en.wikipedia.org/wiki/Netcat), which aims to demonstrate the usage of coroutine macros and their effects. |
| 2 | +Tinync is a simplified version of [nc](https://en.wikipedia.org/wiki/Netcat), which aims to demonstrate the usage of [coroutine](https://en.wikipedia.org/wiki/Coroutine) macros and their effects. |
3 | 3 |
|
4 |
| -## Usage |
| 4 | +## Internals |
5 | 5 | ### Declaring a Coroutine Function
|
6 | 6 | At the very beginning, a coroutine function should be declared first, coroutine function is part of program that will be executed simultaneously with other coroutines. All coroutines should be specified with the macro `cr_proto`, following shows the example:
|
7 | 7 | ```cpp
|
@@ -30,22 +30,29 @@ static void cr_proto(stdin_loop, byte_queue_t *out)
|
30 | 30 | cr_local uint8_t b;
|
31 | 31 | cr_local int r;
|
32 | 32 |
|
33 |
| - cr_begin(); // Initiates the context of this coroutine. |
| 33 | + /* Initiates the context of this coroutine. */ |
| 34 | + cr_begin(); |
34 | 35 | for (;;) {
|
35 |
| - cr_sys(r = read(STDIN_FILENO, &b, 1)); // Wait for read system call to be success. |
| 36 | + /* Wait for read system call to be success. */ |
| 37 | + cr_sys(r = read(STDIN_FILENO, &b, 1)); |
36 | 38 | if (r == 0) {
|
37 |
| - cr_wait(cr_queue_empty(out)); // Wait until queue out is flushed. |
38 |
| - cr_exit(1); // Exit the coroutine with status update as finished. |
| 39 | + /* Wait until queue out is flushed. */ |
| 40 | + cr_wait(cr_queue_empty(out)); |
| 41 | +
|
| 42 | + /* Exit the coroutine with status update as finished. */ |
| 43 | + cr_exit(1); |
39 | 44 | }
|
40 |
| - cr_wait(!cr_queue_full(out)); // Wait until there is place in queue out. |
| 45 | + /* Wait until there is place in queue out. */ |
| 46 | + cr_wait(!cr_queue_full(out)); |
41 | 47 | cr_queue_push(out, b);
|
42 | 48 | }
|
43 |
| - cr_end(); // End up this coroutine, status will be updated as finished. |
| 49 | + /* End up this coroutine, status will be updated as finished. */ |
| 50 | + cr_end(); |
44 | 51 | }
|
45 | 52 | ```
|
46 | 53 |
|
47 | 54 | ### Coroutine Context
|
48 |
| -There is another important part of coroutine, that is, context. Context of a coroutine will preserve its execution point which could be resumed later. To define a context for a coroutine, use `cr_context` macro and initiates it with macro `cr_context_init`. It is important to **assign an identical name to context and its corresponding function**. With example presented at the beginning, its corresponding context should be specified as follows: |
| 55 | +Context is an important part for coroutine, which will preserve the execution point of a coroutine that could be resumed later. To define a context for a coroutine, use `cr_context` macro and initiates it with macro `cr_context_init`. It is important to **assign an identical name to context and its corresponding function**. With example presented at the beginning, its corresponding context should be specified as follows: |
49 | 56 | ```cpp
|
50 | 57 | cr_context(coroutine_name) = cr_context_init();
|
51 | 58 | ```
|
|
0 commit comments