Skip to content

Commit ae7fcf3

Browse files
committed
update usage/docs & GHA, fix/add example
1 parent 34b55cb commit ae7fcf3

10 files changed

Lines changed: 349 additions & 26 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- target: amd64
1616
flags: -m64
1717
steps:
18-
- uses: actions/checkout@v3
18+
- uses: actions/checkout@v4
1919
- name: Prepare
2020
run: |
2121
sudo dpkg --add-architecture i386
@@ -25,7 +25,7 @@ jobs:
2525
run: |
2626
mkdir build
2727
cd build
28-
cmake -DCMAKE_BUILD_TYPE=Debug -BUILD_TESTS=ON -DCMAKE_C_FLAGS=${{ matrix.flags }} ..
28+
cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ON -DCMAKE_C_FLAGS=${{ matrix.flags }} ..
2929
cmake --build .
3030
- name: Run test examples
3131
run: |
@@ -34,7 +34,7 @@ jobs:
3434
3535
build-windows:
3636
name: Windows (${{ matrix.arch }})
37-
runs-on: windows-2019
37+
runs-on: windows-latest
3838
strategy:
3939
fail-fast: false
4040
matrix:
@@ -43,7 +43,7 @@ jobs:
4343
- uses: ilammy/msvc-dev-cmd@v1
4444
with:
4545
arch: ${{ matrix.arch }}
46-
- uses: actions/checkout@v3
46+
- uses: actions/checkout@v4
4747
- name: Configure & build
4848
run: |
4949
mkdir build

.github/workflows/ci_macos.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
name: macOS
1010
runs-on: macos-11
1111
steps:
12-
- uses: actions/checkout@v3
12+
- uses: actions/checkout@v4
1313
- name: Setup
1414
run: |
1515
brew install cmake

.github/workflows/ci_qemu.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ jobs:
1818
- target: s390x
1919
arch: s390x
2020
steps:
21-
- uses: actions/checkout@v3
22-
- uses: uraimo/run-on-arch-action@v2
21+
- uses: actions/checkout@v4
22+
- uses: uraimo/run-on-arch-action@v3
2323
with:
2424
arch: ${{ matrix.arch }}
2525
distro: ubuntu_latest

.github/workflows/ci_qemu_others.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ jobs:
2020
- target: ppc64v2
2121
arch: ppc64le
2222
steps:
23-
- uses: actions/checkout@v3
24-
- uses: uraimo/run-on-arch-action@v2
23+
- uses: actions/checkout@v4
24+
- uses: uraimo/run-on-arch-action@v3
2525
with:
2626
arch: ${{ matrix.arch }}
2727
distro: ubuntu_latest

README.md

Lines changed: 164 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,167 @@
1313

1414
## Introduction
1515

16-
This branch `main`, is now an **WIP** to incorporate [libuv](http://docs.libuv.org) with [c-raii](https://github.com/zelang-dev/c-raii). See `branches` for previous setup.
16+
This library provides **ease of use** *convenience* wrappers for **[libuv](http://docs.libuv.org)** combined with the power of **[c-raii](https://zelang-dev.github.io/c-raii)**, a **high level memory management** library similar to other languages, among other features. Like **[coroutine](https://github.com/zelang-dev/c-raii/blob/main/include/coro.h)** support, the otherwise **callback** needed, is now automatically back to the caller with *results*.
1717

18-
There seems a similar approach has been made for ***C++20***, an implementation in [uvco](https://github.com/dermesser/uvco).
19-
The *[tests](https://github.com/dermesser/uvco/tree/master/test)* presented there currently being reimplemented for *C89* here, this project will be considered stable when *completed*.
18+
* All functions requiring *allocation* and *passing* **pointers**, now returns them instead, if needed.
19+
* The general naming convention is to drop **~~uv_~~** prefix and require only *necessary* arguments/options.
20+
* This integration also requires the use of **`uv_main(int argc, char **argv)`** as the *startup* entry routine:
21+
22+
**libuv** example from <https://github.com/libuv/libuv/tree/master/docs/code/>
23+
24+
<table>
25+
<tr>
26+
<th>helloworld.c</th>
27+
<th>helloworld/main.c</th>
28+
</tr>
29+
<tr>
30+
<td>
31+
32+
```c
33+
#include "uv_coro.h"
34+
35+
int uv_main(int argc, char **argv) {
36+
printf("Now quitting.\n");
37+
yielding();
38+
39+
return coro_err_code();
40+
}
41+
```
42+
43+
</td>
44+
<td>
45+
46+
```c
47+
#include <stdio.h>
48+
#include <stdlib.h>
49+
#include <uv.h>
50+
51+
int main() {
52+
uv_loop_t *loop = malloc(sizeof(uv_loop_t));
53+
uv_loop_init(loop);
54+
55+
printf("Now quitting.\n");
56+
uv_run(loop, UV_RUN_DEFAULT);
57+
58+
uv_loop_close(loop);
59+
free(loop);
60+
return 0;
61+
}
62+
```
63+
64+
</td>
65+
</tr>
66+
</table>
67+
68+
**This general means there will be a dramatic reduction of lines of code repeated, repeatedly.**
69+
70+
*Libuv guides/examples:*
71+
72+
* [Reading/Writing files](https://docs.libuv.org/en/v1.x/guide/filesystem.html#reading-writing-files) as in [uvcat/main.c](https://github.com/libuv/libuv/blob/master/docs/code/uvcat/main.c) - 62 line *script*.
73+
* [Buffers and Streams](https://docs.libuv.org/en/v1.x/guide/filesystem.html#buffers-and-streams) as in [uvtee/main.c](https://github.com/libuv/libuv/blob/master/docs/code/uvtee/main.c) - 79 line *script*.
74+
* [Networking/TCP](https://docs.libuv.org/en/v1.x/guide/networking.html#tcp) as in [tcp-echo-server/main.c](https://github.com/libuv/libuv/blob/master/docs/code/tcp-echo-server/main.c) - 87 line *script*.
75+
76+
*Reduced to:*
77+
<table>
78+
<tr>
79+
<th>uvcat.c - 13 lines</th>
80+
<th>uvtee.c - 20 lines</th>
81+
</tr>
82+
<tr>
83+
<td>
84+
85+
```c
86+
#include "uv_coro.h"
87+
88+
int uv_main(int argc, char **argv) {
89+
uv_file fd = fs_open(argv[1], O_RDONLY, 0);
90+
if (fd > 0) {
91+
string text = fs_read(fd, -1);
92+
fs_write(STDOUT_FILENO, text, -1);
93+
94+
return fs_close(fd);
95+
}
96+
97+
return fd;
98+
}
99+
```
100+
101+
</td>
102+
<td>
103+
104+
```c
105+
#include "uv_coro.h"
106+
107+
int uv_main(int argc, char **argv) {
108+
string text = nullptr;
109+
uv_file fd = fs_open(argv[1], O_CREAT | O_RDWR, 0644);
110+
if (fd > 0) {
111+
pipe_file_t *file_pipe = pipe_file(fd, false);
112+
pipe_out_t *stdout_pipe = pipe_stdout(false);
113+
pipe_in_t *stdin_pipe = pipe_stdin(false);
114+
while (text = stream_read(stdin_pipe->reader)) {
115+
if (stream_write(stdout_pipe->writer, text)
116+
|| stream_write(file_pipe->handle, text))
117+
break;
118+
}
119+
120+
return fs_close(fd);
121+
}
122+
123+
return fd;
124+
}
125+
```
126+
127+
</td>
128+
</tr>
129+
</table>
130+
131+
<table>
132+
<tr>
133+
<th>tcp-echo-server.c - 27 lines</th>
134+
</tr>
135+
<tr>
136+
<td>
137+
138+
```c
139+
#include "uv_coro.h"
140+
141+
#define DEFAULT_PORT 7000
142+
#define DEFAULT_BACKLOG 128
143+
144+
void new_connection(uv_stream_t *socket) {
145+
string data = stream_read(socket);
146+
stream_write(socket, data);
147+
}
148+
149+
int uv_main(int argc, char **argv) {
150+
uv_stream_t *client, *server;
151+
char addr[UV_MAXHOSTNAMESIZE] = nil;
152+
153+
if (snprintf(addr, sizeof(addr), "0.0.0.0:%d", DEFAULT_PORT)) {
154+
server = stream_bind(addr, 0);
155+
while (server) {
156+
if (is_empty(client = stream_listen(server, DEFAULT_BACKLOG))) {
157+
fprintf(stderr, "Listen error %s\n", uv_strerror(coro_err_code()));
158+
break;
159+
}
160+
161+
stream_handler(new_connection, client);
162+
}
163+
}
164+
165+
return coro_err_code();
166+
}
167+
```
168+
169+
</td>
170+
</tr>
171+
</table>
172+
173+
See `branches` for previous setup, `main` is an complete makeover of previous implementation approaches.
174+
175+
Similar approach has been made for ***C++20***, an implementation in [uvco](https://github.com/dermesser/uvco).
176+
The *[tests](https://github.com/dermesser/uvco/tree/master/test)* presented there currently being reimplemented for *C89* here, this project will be considered stable when *completed*. And another approach in [libasync](https://github.com/btrask/libasync) mixing [libco](https://github.com/higan-emu/libco) with **libuv**. Both approaches are **Linux** only.
20177
21178
## Synopsis
22179
@@ -26,18 +183,18 @@ The *[tests](https://github.com/dermesser/uvco/tree/master/test)* presented ther
26183

27184
## Usage
28185

29-
### See [examples](https://github.com/zelang-dev/c-coroutine/tree/main/examples) folder
186+
### See [examples](https://github.com/zelang-dev/c-coroutine/tree/main/examples) and [tests](https://github.com/zelang-dev/c-coroutine/tree/main/tests) folder
30187

31188
## Installation
32189

33-
The build system uses **cmake**, that produces _single_ **static** library stored under `built`, and the complete `include` folder is needed.
190+
The build system uses **cmake**, that produces **static** libraries by default.
34191

35192
**Linux**
36193

37194
```shell
38195
mkdir build
39196
cd build
40-
cmake .. -DCMAKE_BUILD_TYPE=Debug/Release -DBUILD_EXAMPLES=ON # use to build files in examples folder
197+
cmake .. -D CMAKE_BUILD_TYPE=Debug/Release -D BUILD_EXAMPLES=ON -D BUILD_TESTS=ON # use to build files in tests/examples folder
41198
cmake --build .
42199
```
43200

@@ -46,7 +203,7 @@ cmake --build .
46203
```shell
47204
mkdir build
48205
cd build
49-
cmake .. -D BUILD_EXAMPLES=ON # use to build files in examples folder
206+
cmake .. -D BUILD_EXAMPLES=ON -D BUILD_TESTS=ON # use to build files in tests/examples folder
50207
cmake --build . --config Debug/Release
51208
```
52209

0 commit comments

Comments
 (0)