nim-chronos/README.md

98 lines
5.1 KiB
Markdown
Raw Normal View History

# Chronos - The Efficient Asynchronous library
2018-09-05 03:27:01 +00:00
[![Build Status (Travis)](https://img.shields.io/travis/status-im/nim-asyncdispatch2/master.svg?label=Linux%20/%20macOS "Linux/macOS build status (Travis)")](https://travis-ci.org/status-im/nim-asyncdispatch2)
2018-09-05 03:36:14 +00:00
[![Windows build status (Appveyor)](https://img.shields.io/appveyor/ci/nimbus/nim-asyncdispatch2/master.svg?label=Windows "Windows build status (Appveyor)")](https://ci.appveyor.com/project/nimbus/nim-asyncdispatch2)
[![License: Apache](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
2018-09-05 03:27:01 +00:00
![Stability: experimental](https://img.shields.io/badge/stability-experimental-orange.svg)
2018-05-21 22:15:34 +00:00
## Core differences between the standard library asyncdispatch and Chronos
2018-05-28 23:35:15 +00:00
2018-05-29 16:24:34 +00:00
1. Unified callback type `CallbackFunc`:
2018-05-28 23:35:15 +00:00
2018-05-29 16:24:34 +00:00
Current version of asyncdispatch uses many types of callbacks:
2018-05-28 23:35:15 +00:00
2018-05-29 16:24:34 +00:00
* `proc ()` is used in callSoon() callbacks and Future[T] completion callbacks.
* `proc (fut: Future[T])` is used in Future[T] completion callbacks.
* `proc (fd: AsyncFD, bytesTransferred: Dword, errcode: OSErrorCode)` is used in Windows IO completion callbacks.
* `proc (fd: AsyncFD): bool` is used in Unix IO event callbacks.
2018-05-28 23:35:15 +00:00
2018-05-29 16:24:34 +00:00
Such a large number of different types creates big problems in the storage and processing of callbacks and in interaction between callbacks. Lack of ability to pass custom user data to
a callback also creates difficulties and inefficiency with passing custom, user-defined data needed for using closures (one more allocation).
To resolve this issue, we have introduced a unified callback type, `CallbackFunc`:
2018-05-28 23:45:15 +00:00
2018-05-28 23:38:53 +00:00
```nim
type
CallbackFunc* = proc (arg: pointer = nil) {.gcsafe.}
2018-05-28 23:35:15 +00:00
```
2018-05-29 16:24:34 +00:00
Also, one more type was introduced for the callback storage, `AsyncCallback`:
2018-05-28 23:38:53 +00:00
```nim
2018-05-28 23:35:15 +00:00
type
AsyncCallback* = object
function*: CallbackFunc
udata*: pointer
```
2018-05-29 16:24:34 +00:00
2. The order of Future[T] completion callbacks:
2018-05-28 23:35:15 +00:00
2018-05-29 16:49:05 +00:00
Current version of asyncdispatch processes Future[T] completion callbacks in reverse order, but asyncdispatch2 schedules callbacks in forward order: https://github.com/nim-lang/Nim/issues/7197
2018-05-28 23:35:15 +00:00
2018-05-29 16:24:34 +00:00
3. Changed the behavior of OS descriptor event callbacks:
2018-05-28 23:35:15 +00:00
For some unknown reason, the current version of asyncdispatch uses seq[T] to hold a list of descriptor event listeners. However, in the asynchronous environment, there is no need for a list of event listeners. In Chronos, there is only one place for one READ listener and one place for one WRITE listener.
2018-05-28 23:35:15 +00:00
2018-05-29 16:24:34 +00:00
4. Removed the default timeout value for the poll() procedure, which allows incorrect usage of asyncdispatch and produces 500-ms timeouts in correct usage.
5. Changed the behavior of the scheduler in the poll() procedure, and fixed the following issues:
2018-05-28 23:45:15 +00:00
* https://github.com/nim-lang/Nim/issues/7758
* https://github.com/nim-lang/Nim/issues/7197
* https://github.com/nim-lang/Nim/issues/7193
* https://github.com/nim-lang/Nim/issues/7192
* https://github.com/nim-lang/Nim/issues/6846
* https://github.com/nim-lang/Nim/issues/6929
2018-05-28 23:35:15 +00:00
6. Chronos no longer uses `epochTime()`; instead, it uses the fastest time primitives for a specific OS, `fastEpochTime()`. Also, because MacOS supports only a millisecond resolution in `kqueue`, sub-millisecond resolution is not needed. For details, see https://github.com/nim-lang/Nim/issues/3909.
2018-05-29 16:24:34 +00:00
7. Removed all IO primitives (`recv()`, `recvFrom()`, `connect()`, `accept()`, `send()`, and `sendTo()`) from the public API, and moved all their functionality into Transports.
2018-05-28 23:35:15 +00:00
2018-05-29 16:24:34 +00:00
8. Introduced an `addTimer()` / `removeTimer()` callback interface.
2018-05-28 23:35:15 +00:00
2018-05-28 23:45:15 +00:00
9. Introduced `removeReader()` for `addReader()` and `removeWriter()` for `addWriter()`.
2018-05-28 23:35:15 +00:00
2018-05-29 16:24:34 +00:00
10. Changed the behavior of the `addReader()`, `addWriter()`, and `addTimer()` callbacks. Now, only the explicit removal of the callbacks must be supplied via `removeReader()`, `removeWriter()`, and `removeTimer()`.
2018-05-28 23:35:15 +00:00
2018-05-29 16:24:34 +00:00
11. Added the support for the cross-platform `sendfile()` operation.
2018-05-28 23:35:15 +00:00
2018-05-29 16:24:34 +00:00
12. Removed the expensive `AsyncEvent` and the support for hardware timers and `addProcess`. `addProcess` will be implemented as SubprocessTransport, while hardware-based `AsyncEvent` will be renamed to `ThreadAsyncEvent`.
2018-05-28 23:35:15 +00:00
2018-05-29 16:24:34 +00:00
13. Added cheap synchronization primitives: `AsyncLock`, `AsyncEvent`, and `AsyncQueue[T]`.
2018-05-28 23:35:15 +00:00
2018-05-30 03:34:58 +00:00
## Documentation
You can find more documentation, notes and examples in [Wiki](https://github.com/status-im/nim-chronos/wiki).
2018-05-30 03:34:58 +00:00
2018-05-30 03:32:47 +00:00
## Installation
You can use Nim official package manager `nimble` to install `chronos`. The most recent version of the library can be installed via:
2018-05-28 23:35:15 +00:00
2018-05-30 03:32:47 +00:00
```
$ nimble install https://github.com/status-im/nim-chronos.git
2018-05-30 03:32:47 +00:00
```
2018-05-28 23:35:15 +00:00
2018-05-30 03:32:47 +00:00
## TODO
* Pipe/Subprocess Transports.
* Multithreading Stream/Datagram servers
* Future[T] cancelation
2018-05-28 23:35:15 +00:00
2018-09-05 03:36:14 +00:00
## License
2018-09-05 18:45:22 +00:00
Licensed and distributed under either of
* MIT license: [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT
2019-01-02 13:53:00 +00:00
or
* Apache License, Version 2.0, ([LICENSE-APACHEv2](LICENSE-APACHEv2) or http://www.apache.org/licenses/LICENSE-2.0)
2018-09-05 03:36:14 +00:00
2019-01-02 13:53:00 +00:00
at your option. This file may not be copied, modified, or distributed except according to those terms.