https://github.com/zevv/nimcoro
Nim coroutine based async "from scratch"
https://github.com/zevv/nimcoro
Last synced: 6 months ago
JSON representation
Nim coroutine based async "from scratch"
- Host: GitHub
- URL: https://github.com/zevv/nimcoro
- Owner: zevv
- License: mit
- Created: 2020-05-27T12:30:42.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-08-09T05:04:32.000Z (over 4 years ago)
- Last Synced: 2025-04-03T13:49:38.486Z (11 months ago)
- Language: Nim
- Homepage:
- Size: 28.3 KB
- Stars: 16
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
This is a little proof-of-concept prject to see if I can get Lua-style
coroutines semantics in Nim, and use this to build an alternative async
implementation. It's built on very low level primitives only: the `posix` lib
for sockets and `poll()`, and a small wrapper around the posix `ucontext`
functions.
There's a few moving parts in this project:
- coro.nim: This implements simple coroutines based on ucontext. This are
basically Lua-style coroutines, but because of Nims static typing it is hard
to implement the Lua way of passing data through `yield()` and `resume()`.
For now I've chosen not to pass data at all, as there are enough other ways
to do that outside of the core coroutine
- evq.nim: This is a very basic and naive event loop implementation: register
file descriptors with read or write events and a proc, and your proc will
be called back when the file descriptor is ready.
- io.nim: Here the above two modules come together to create very friendly
async I/O. Look at the `waitForFd()` proc to see what is happening.
- main.nim: This example creates a listening TCP socket which can handle
multiple clients, which are all run inside coroutines.
Note that the curent coro implementation confuses Nim's GC, run with `--gc:arc`!