timer-deque-rs
Description
A OS based timer and timer event dequeue which can be used to create a simple timeout scheduling. Can be used just as OS timer or use it together with the timeout deque.
This crate is still incomplete and not properly tested. The proof of concept is now developed.
- This crate i.e code is NOT an Open Source software. This is a FREE (gratis) software and follows the principle of Sources Available/Disclosed software which should be fairly used.
- It is published under FSF/OSI approved licenses however author does not follow/share/respect OSI and FSF principles and phylosophy.
- License is subject to be changed in further versions without warning.
- If you are using code in non-free (in terms of gratis) software you MUST NEVER demand a development of any features which are missing and needed for your business if you are not sponsoring/contributing those changes.
- Access to the code can be limited by author to specific entities due to the local laws (not my bad or fault)(despite what is said in the license).
- AI generated sloppy code is prohibited. AI generates slop "a priori" (anyway).
- Licenses (thank you OSS sectarians ) do not anyhow limit AI training, but f^ck you all - ChatGPT, Co
ckPilot, especially Claude and rest unidentified cr@p. - It is strongly discouraged from using the AI based tools to write or enhance the code. AI slope would 100% violate the license by introducing the 3rd party licensed code.
The pull requests are now supported because the repository was moved to Codeberg. The alternative way is to send patches over the email to patch[at]4neko.org.
In case if you would like to contribute the code, please use pull request. Your pull request should include:
-
Description of changes and why it is needed.
-
Test the pull request.
In case of you prefer email and patch files please consider the following:
-
For each feature or fix, please send patches separatly.
-
Please write what your patch is implementing or fixing.
-
I can read the code and I am able to understand it, so don't write a poem or essay in the description to the patches.
-
Please test your patch.
- Can I use the MPL-2.0 licensed code (crate) in larger project licensed with more permissive license like BSD or MIT.
I want to distribute (outside my organization) executable programs or libraries that I have compiled from someone else's unchanged MPL-licensed source code, either standalone or part of a larger work. What do I have to do?
You must inform the recipients where they can get the source for the MPLed code in the executable program or library you are distributing (i.e., you must comply with Section 3.2). You may distribute any executables you create under a license of your choosing, as long as that license does not interfere with the recipients' rights to the source under the terms of the MPL.
Yes, MPL- and Apache-licensed code can be used with an MIT codebase (so in that sense, they are "compatible"). However, the MPL- / Apache-licensed code remains under its original license. (So although compatible, you cannot relicense someone else's MPL or Apache code into the MIT license.) This means that your final codebase will contain a mix of MPL, Apache, and MIT licensed code. As an example, MPL has weak copyleft, so if you modified an MPL file, that file (including your changes) must remain under the MPL license.
You should use this license if you are located in the EU which gives you more advantages over GPL because in case of any disputes, the license allows you to defend your rights in a European Union country, in this case it will be Spain. It has also been translated into all languages of the EU member states.
Matrix of EUPL compatible open source licences
EUPL-1.2 is incompatiable with GPL according to GNU ORG
This is a free software license. By itself, it has a copyleft comparable to the GPL's, and incompatible with it.
Version
v 0.5.0-development, Rust edition 2024
Changes in the timer_portable.
- A BSD (timers) experimental support was introduced with a patch which is in
masterof the git before the crate release. It inclused a bothKQueueandtimerfd(Linux compat) basedtimerandpoll. Featuresbsd_use_timerfdandbsd_use_pollenbales a specific subsystems.bsd_use_pollswitches fromKQueuetoPoll,bsd_use_timerfdswitches fromKQueuetoTimerFd. - Changes in the
pollandtimerinterfaces.
Important
A tokio's AsyncFd throws error when trying to use it on all KQueue, timerfd, poll. The reason is unknown. Probably this is due regresson or because all three are read-only.
Changes in the layout, poll system and tasks.
- A
pollnow provides an option to interrupt the polling process from another thread. Also, polling can be interrupted by adding or removing timer. - Added a simple task spawner and executor based on the parallel execution.
Changes in the layout, time types and new deque modes.
- A layout of the crate's files was changed. The deque based on timeouts are now in directory deque_timeout.
- A new time types were introduced which would allow to perform the manipulations on the time:
AbsoluteTimeandRelativeTime. Both are also a types which modifies the behaviour of the code. - A new deque modes were introduced:
OrderdTimerDequeOnceandOrderdTimerDequePeriodic. Both are based onAbsoluteTimehowever the latter usingRelativeTimetime to offset the next timeout. The difference is: the former removes the item from the queue after timeout, the latter keeps it until manually removed.
Changes in the poll system.
- Now, only structures marked as
AsTimerFdcan be added to instance. - Now, timers are binded to the
pollinginstance by the weak reference. If timer drops, it will be automatically removed from thepoll. - New tests added.
License:
Sources are available under: MPL-2.0 OR EUPL-1.2
Supports:
- GNU/Linux.
- Three dequeue types with the scheduling using absolute time.
- Synchronious (parallel) task spawner and executor.
- Async
- FreeBSD (experimental)
ToDo
- other BSD based systems (kqueue) i.e (OSX, OpenBSD, NetBSD, DragonflyBSD)
- other timers types and queues i.e async task
Does not support:
- Windows
- no_std
Issues tracker:
The project has moved to Codeberg.
Features
Below is related to BSD systems
bsd_use_timerfd- usetimerfdinstead ofkqueuebsd_use_poll- usepollinstread ofkqueue
Usage:
see ./examples/ there
Diagrams
OrderdTimerDequeOnce operation mode
┌────────────────────────────┐
│ │
│ OrderdTimerDequeOnce │
│ │
└────────────────────────────┘
┌─────────────────────────────┐ ┌────────────────────────────┐
│ Deque │ │ TimerFD │
│ │ │ │
│ │ │ timeout: 12332456 │
│ ┌─────────────────────┐ │ │ absoute time │
│ │ ITEM │ │ └───────────┬────────────────┘
│ │ TIMEOUT: 12332456 │ ──┼──────┐ │
│ └─────────────────────┘ │ │ ──────────┼─────────────
│ │ │ │
│ │ │ ▼
│ ┌─────────────────────┐ │ │ ┌────────────────────┐
│ │ ITEM │ │ │ │ │
│ │ TIMOEUT: 12334543 ┼───┼────┐ │ │ timeout │
│ └─────────────────────┘ │ │ │ └─────────┬──────────┘
│ │ │ │ │
│ │ │ │ │
│ ┌─────────────────────┐ │ │ │ ▼
│ │ ITEM │ │ │ │ ┌────────────────────┐
│ │ TIMEOUT: 12335654 │ │ │ │ │ │
│ └─────────────────────┘ │ │ └────► │ pop_front │
│ │ │ │ timeout <= cur_tm │
│ │ │ └─────────┬──────────┘
│ ............ │ │ │
│ │ │ │
│ │ │ │
└─────────────────────────────┘ │ ▼
│ ┌──────────────────┐
│ │ │
└───────►│ set_timer │
│ │
└─────────┬────────┘
│
┌─────────────▼──────────────┐
│ TimerFD │
│ │
│ timeout: 12334543 │
│ absoute time │
└──────────┬─────────────────┘
│
┌───────▼────────────┐
│ │
│ timeout │
└─────────┬──────────┘
OrderdTimerDequeuPeriodic operation mode
┌─────────────────────────────┐
│ │
│ OrderdTimerDequeuPeriodic │
│ │
└─────────────────────────────┘
┌─────────────────────────────┐ ┌────────────────────────────┐
│ Deque │ │ TimerFD │
│ │ │ │
│ │ │ timeout: 12332456 │
│ ┌─────────────────────┐ │ │ absoute time │
│ │ ITEM │ │ ──────────┬────────────────┘
│ │ TIMEOUT: 12332456 │ ──┼───────┐ │
│ │ EXTEND: 100 │ │ │ ──────────┼─────────────
│ └─────────────────────┘ │ │ │
│ │ │ ▼
│ ┌─────────────────────┐ │ │ ┌────────────────────┐
│ │ ITEM │ │ │ │ │
│ │ TIMOEUT: 12334543 ┼───┼────┐ │ │ timeout │
│ │ EXTEND: 120 │ │ │ │ └─────────┬──────────┘
│ └─────────────────────┘ │ │ │ │
│ │ │ │ │
│ ┌─────────────────────┐ │ │ │ ▼
│ │ ITEM │ │ │ │ ┌────────────────────┐
│ │ TIMEOUT: 12335654 │ │ │ │ │ │
│ │ EXTEND: 150 │ │ │ └────►│ pop_front │
│ └─────────────────────┘ │ │ │ timeout <= cur_tm │
│ │ │ └─────────┬──────────┘
┌──────┼───► ............ │ │ │
│ │ │ │ │
│ │ │ │ │
│ └─────────────────────────────┘ │ ▼
│ │ ┌───────────────────────────┐
│ │ │ ITEM │
│ return back with new tm │ │ TIMEOUT: 12332456+100 │
└─────────────────────────────────────────┼────────┼ │
│ │ NEW_TIMEOUT: 12332556 │
│ └──────────┬────────────────┘
│ ▼
│ ┌──────────────────┐
│ │ │
│ │ set_timer │
└─────────►│ │
└─────────┬────────┘
│
┌─────────────▼──────────────┐
│ TimerFD │
│ │
│ timeout: 12334543 │
│ absoute time │
└──────────┬─────────────────┘
│
┌───────▼────────────┐
│ │
│ timeout │
└─────────┬──────────┘
...
The items are stored sorted by timeout VecDeque. The identification of the item in the queue is perfomed
by PartialEq and Eq by the instance or ID, and not by timeout value. The timer is set to the nearest (by time) value.
Examples
For every sync example, the event notification is used. In case of Linux, the EPoll is used.
Timer queue type consumer
This type of dequeue is consuming the instance.
use ;
use
;
;
Timer queue type ticket issuer
This type of dequeue is issuing the ticket for an instance which can be cancelled (ticket).
use ;
use
;
;
Timer queue type notification (signal)
This type of dequeue is sending signal i.e notification using provided functionality.
use
;
use
;
Timer queue type consumer (async) polling future (less efficient)
In this example, the consumer type of deque is used in async context by polling the timer. This is not efficient. In the next example, the AsyncFd is used it increase efficiency.
use ;
use
;
;
async
Timer queue type notifier (async) using more efficient AsyncFd
In this example, the notifier (signal) type of deque is used in async context by wrapping the timer deque in the AsyncFd. This is more efficient than jus polling the FD for events like in previous example.
use fmt;
use
;
use
;
async
Generic, OS based timer without queue
Just simple timer based on OS functionality.
use ;
use
;
A task spawn and executor
A simple parallel task spawner
use ;
use crate::;