Skip to content

Commit

Permalink
Add some docs 🤔
Browse files Browse the repository at this point in the history
  • Loading branch information
Cloudef committed Jun 17, 2024
1 parent e597244 commit 9d9851d
Show file tree
Hide file tree
Showing 13 changed files with 695 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Build and Deploy
on: [push]
permissions:
contents: write
jobs:
build-and-deploy:
concurrency: ci-${{ github.ref }}
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Install and Build 🔧
run: |
cd docs
npm run build
- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: docs/dist
9 changes: 9 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
dist
node_modules
_lib
tsconfig.tsbuildinfo
tsconfig.*.tsbuildinfo
vocs.config.ts.timestamp-*
.vercel
.vocs
17 changes: 17 additions & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "zig-aio",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vocs dev",
"build": "vocs build",
"preview": "vocs preview"
},
"dependencies": {
"@types/react": "latest",
"react": "latest",
"react-dom": "latest",
"typescript": "latest",
"vocs": "latest"
}
}
49 changes: 49 additions & 0 deletions docs/pages/aio-dynamic.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# AIO API

## Dynamic IO

In case the amount of IO operations isn't known ahead of time the dynamic api can be used.

### Initializing Dynamic instance

Creating a Dynamic instance requires an allocator and upper bound for non-completed operations.
The instance allocates only during the `init`, and frees the memory during `deinit`.
Same allocator must be used in `deinit` that was used in `init`.

```zig
const max_operations = 32;
var work = try Dynamic.init(std.heap.page_allocator, max_operations);
defer work.deinit(std.heap.page_allocator);
```

### Queuing operations

It is possible to queue either single or multiple operations just like with the immediate api.
The call to queue is atomic, if the call fails, none of the operations will be actually performed.

```zig
// Multiple operations
try work.queue(.{
aio.Read{...},
aio.Write{...},
aio.Fsync{...},
});
// Single operation
try work.queue(aio.Timeout{...});
```

### Completing operations

It is possible to complete the operations either in blocking or non-blocking fashion.
The blocking mode will wait for at least one operation to complete.
The non-blocking always returns immediately even if no operations were completed.
The call to complete returns `aio.CompletionResult` containing the number of operations that were completed
and the number of errors that occured.

```zig
// blocks until at least 1 operation completes
const res = try work.complete(.blocking);
// returns immediately
const res = try work.complete(.nonblocking);
```
62 changes: 62 additions & 0 deletions docs/pages/aio-immediate.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# AIO API

## Immediate IO

For immediate blocking IO, `zig-aio` provides the following functions in the `aio` module.

### Perform a single operation

Completes a single operation, the call blocks until it's complete.
Returns error of the operation if the operation failed.
Returns `void` if there was no error.

```zig
try aio.single(aio.Write{.file = f, .buffer = "contents"});
```

### Perform multiple operations

`zig-aio` provides two methods for batching IO operations.

#### Using multi

Completes a list of operations immediately, blocks until complete
Returns `error.SomeOperationFailed` if any operation failed
Returns `void` if there were no errors.

```zig
var my_buffer: [1024]u8 = undefined;
var my_len: usize = undefined;
try aio.multi(.{
aio.Write{.file = f, .buffer = "contents", .link_next = true},
aio.Read{.file = f, .buffer = &my_buffer, .out_read = &my_len},
});
```

The `.link_next` field of operation can be used to link the operation to the next operation.
When linking operations, the next operation won't start until the previous operation is complete.

#### Using batch

Batch is similar to multi, but it will not return `error.SomeOperationFailed` in case any of the operations fail.
Instead batch returns `aio.CompletionResult` which contains the number of operations that was completed, and number of
errors that occured. To find out which operations failed, errors have to be stored somewhere by setting the `.out_error`
field of the operation. The batch call may still fail in implementation defined ways, such as running out of system resources.

```zig
var my_buffer: [1024]u8 = undefined;
var my_len: usize = undefined;
var write_error: std.posix.WriteError = undefined;
var read_error: std.posix.ReadError = undefined;
const res = try aio.batch(.{
aio.Write{.file = f, .buffer = "contents", .out_error = &write_error, .link_next = true},
aio.Read{.file = f, .buffer = &my_buffer, .out_error = &read_error, .out_read = &my_len},
});
if (res.num_errors > 0) {
if (write_error != error.Success) @panic("write failed");
if (read_error != error.Success) @panic("read failed");
}
```
Loading

0 comments on commit 9d9851d

Please sign in to comment.