orengine 0.7.0-alpha.1

Optimized ring engine for Rust. It is a lighter and faster asynchronous library than tokio-rs, async-std, may, and even smol.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use crate::io::config::IoWorkerConfig;
use crate::io::io_request_data::IoRequestData;
use crate::io::sys::{MessageRecvHeader, OpenHow, OsMessageHeader, RawFd, WorkerSys};
use crate::BUG_MESSAGE;
use nix::libc;
use nix::libc::sockaddr;
use std::cell::UnsafeCell;
use std::net::Shutdown;
use std::time::{Duration, Instant};

thread_local! {
    /// Thread-local worker for async io operations.
    pub(crate) static LOCAL_WORKER: UnsafeCell<Option<WorkerSys>> = const {
        UnsafeCell::new(None)
    };
}

/// Returns the thread-local worker wrapped in an [`Option`].
pub(crate) fn get_local_worker_ref() -> &'static mut Option<WorkerSys> {
    LOCAL_WORKER.with(|local_worker| unsafe { &mut *local_worker.get() })
}

/// Initializes the thread-local worker.
pub(crate) unsafe fn init_local_worker(config: IoWorkerConfig) {
    assert!(!get_local_worker_ref().is_some(), "{BUG_MESSAGE}");

    *get_local_worker_ref() = Some(WorkerSys::new(config));
}

/// Returns the thread-local worker.
///
/// # Panics
///
/// If the thread-local worker has not been initialized.
///
/// # Undefined Behavior
///
/// If the thread-local worker has not been initialized in `release` mode.
#[inline(always)]
pub(crate) fn local_worker() -> &'static mut WorkerSys {
    #[cfg(debug_assertions)]
    {
        get_local_worker_ref().as_mut().expect(
            "An attempt to call io-operation has failed, \
             because an Executor has no io-worker. Look at the config of the Executor.",
        )
    }

    #[cfg(not(debug_assertions))]
    unsafe {
        get_local_worker_ref().as_mut().unwrap_unchecked()
    }
}

/// A worker for async io operations.
pub(crate) trait IoWorker {
    /// Creates a new worker.
    fn new(config: IoWorkerConfig) -> Self;
    /// Registers a new time-bounded io task. It will be cancelled if the deadline is reached.
    ///
    /// It takes `&mut Instant` as a deadline because it increments the deadline by 1 nanosecond
    /// if it is not unique.
    fn register_time_bounded_io_task(
        &mut self,
        io_request_data: &IoRequestData,
        deadline: &mut Instant,
    );
    /// Deregisters a time-bounded io task.
    /// It is used to say [`IoWorker`] to not cancel the task.
    ///
    /// Deadline is always unique, therefore we can use it as a key.
    fn deregister_time_bounded_io_task(&mut self, deadline: &Instant);
    /// Returns whether `worker` has work to do.
    fn has_work(&self) -> bool;
    /// Submits an accumulated tasks to the kernel and polls it for completion if needed.
    ///
    /// It also gets `timeout` for polling. If it is `None`, it will not wait (__busy polling__).
    fn must_poll(&mut self, timeout_option: Option<Duration>);
    /// Registers a new `socket` io operation.
    fn socket(
        &mut self,
        domain: socket2::Domain,
        sock_type: socket2::Type,
        protocol: socket2::Protocol,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `accept` io operation.
    fn accept(
        &mut self,
        listen_fd: RawFd,
        addr: *mut sockaddr,
        addrlen: *mut libc::socklen_t,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `accept` io operation with deadline.
    fn accept_with_deadline(
        &mut self,
        listen_fd: RawFd,
        addr: *mut sockaddr,
        addrlen: *mut libc::socklen_t,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.accept(listen_fd, addr, addrlen, request_ptr);
    }
    /// Registers a new `connect` io operation.
    fn connect(
        &mut self,
        socket_fd: RawFd,
        addr_ptr: *const sockaddr,
        addr_len: libc::socklen_t,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `connect` io operation with deadline.
    fn connect_with_deadline(
        &mut self,
        socket_fd: RawFd,
        addr_ptr: *const sockaddr,
        addr_len: libc::socklen_t,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.connect(socket_fd, addr_ptr, addr_len, request_ptr);
    }

    // region poll fd

    /// Registers a new `poll` for readable io operation.
    fn poll_fd_read(&mut self, fd: RawFd, request_ptr: *mut IoRequestData);
    /// Registers a new `poll` for readable io operation with deadline.
    fn poll_fd_read_with_deadline(
        &mut self,
        fd: RawFd,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.poll_fd_read(fd, request_ptr);
    }
    /// Registers a new `poll` for writable io operation.
    fn poll_fd_write(&mut self, fd: RawFd, request_ptr: *mut IoRequestData);
    /// Registers a new `poll` for writable io operation with deadline.
    fn poll_fd_write_with_deadline(
        &mut self,
        fd: RawFd,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.poll_fd_write(fd, request_ptr);
    }

    // endregion

    // region recv

    /// Registers a new `recv` io operation.
    fn recv(&mut self, fd: RawFd, ptr: *mut u8, len: u32, request_ptr: *mut IoRequestData);
    /// Registers a new `recv` io operation with __fixed__ buffer.
    fn recv_fixed(
        &mut self,
        fd: RawFd,
        ptr: *mut u8,
        len: u32,
        buf_index: u16,
        request_ptr: *mut IoRequestData,
    );

    /// Registers a new `recv` io operation with deadline.
    fn recv_with_deadline(
        &mut self,
        fd: RawFd,
        ptr: *mut u8,
        len: u32,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.recv(fd, ptr, len, request_ptr);
    }

    /// Registers a new `recv` io operation with deadline with __fixed__ buffer.
    fn recv_fixed_with_deadline(
        &mut self,
        fd: RawFd,
        ptr: *mut u8,
        len: u32,
        buf_index: u16,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.recv_fixed(fd, ptr, len, buf_index, request_ptr);
    }

    // endregion

    // region recv_from

    /// Registers a new `recv_from` io operation.
    // TODO with fixed buffer
    fn recv_from(
        &mut self,
        fd: RawFd,
        msg_header: &mut MessageRecvHeader,
        request_ptr: *mut IoRequestData,
    );

    /// Registers a new `recv_from` io operation with deadline.
    fn recv_from_with_deadline(
        &mut self,
        fd: RawFd,
        msg_header: &mut MessageRecvHeader,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.recv_from(fd, msg_header, request_ptr);
    }

    // endregion

    // region send

    /// Registers a new `send` io operation.
    fn send(&mut self, fd: RawFd, ptr: *const u8, len: u32, request_ptr: *mut IoRequestData);
    /// Registers a new `send` io operation with __fixed__ buffer.
    fn send_fixed(
        &mut self,
        fd: RawFd,
        ptr: *const u8,
        len: u32,
        buf_index: u16,
        request_ptr: *mut IoRequestData,
    );

    /// Registers a new `send` io operation with deadline.
    fn send_with_deadline(
        &mut self,
        fd: RawFd,
        ptr: *const u8,
        len: u32,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.send(fd, ptr, len, request_ptr);
    }

    /// Registers a new `send` io operation with deadline with __fixed__ buffer.
    fn send_fixed_with_deadline(
        &mut self,
        fd: RawFd,
        ptr: *const u8,
        len: u32,
        buf_index: u16,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.send_fixed(fd, ptr, len, buf_index, request_ptr);
    }

    // endregion

    // region send_to

    /// Registers a new `send_to` io operation.
    // TODO with fixed buffer
    fn send_to(
        &mut self,
        fd: RawFd,
        msg_header: *const OsMessageHeader,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `send_to` io operation with deadline.
    fn send_to_with_deadline(
        &mut self,
        fd: RawFd,
        msg_header: *const OsMessageHeader,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.send_to(fd, msg_header, request_ptr);
    }

    // endregion

    // region peek

    /// Registers a new `peek` io operation.
    fn peek(&mut self, fd: RawFd, ptr: *mut u8, len: u32, request_ptr: *mut IoRequestData);
    /// Registers a new `peek` io operation with __fixed__ buffer.
    fn peek_fixed(
        &mut self,
        fd: RawFd,
        ptr: *mut u8,
        len: u32,
        buf_index: u16,
        request_ptr: *mut IoRequestData,
    );

    /// Registers a new `peek` io operation with deadline.
    fn peek_with_deadline(
        &mut self,
        fd: RawFd,
        ptr: *mut u8,
        len: u32,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.peek(fd, ptr, len, request_ptr);
    }

    /// Registers a new `peek` io operation with deadline with __fixed__ buffer.
    fn peek_fixed_with_deadline(
        &mut self,
        fd: RawFd,
        ptr: *mut u8,
        len: u32,
        buf_index: u16,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.peek_fixed(fd, ptr, len, buf_index, request_ptr);
    }

    // endregion

    // region peek_from

    /// Registers a new `peek_from` io operation.
    // TODO with fixed buffer
    fn peek_from(
        &mut self,
        fd: RawFd,
        msg: &mut MessageRecvHeader,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `peek_from` io operation with deadline.
    fn peek_from_with_deadline(
        &mut self,
        fd: RawFd,
        msg: &mut MessageRecvHeader,
        request_ptr: *mut IoRequestData,
        deadline: &mut Instant,
    ) {
        self.register_time_bounded_io_task(unsafe { &*request_ptr } as _, deadline);
        self.peek_from(fd, msg, request_ptr);
    }

    // endregion

    /// Registers a new `shutdown` io operation.
    fn shutdown(&mut self, fd: RawFd, how: Shutdown, request_ptr: *mut IoRequestData);
    /// Registers a new `open` io operation.
    fn open(
        &mut self,
        path: *const libc::c_char,
        open_how: *const OpenHow,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `fallocate` io operation if the kernel supports it.
    fn fallocate(
        &mut self,
        fd: RawFd,
        offset: u64,
        len: u64,
        flags: i32,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `sync_all` io operation.
    fn sync_all(&mut self, fd: RawFd, request_ptr: *mut IoRequestData);
    /// Registers a new `sync_data` io operation.
    fn sync_data(&mut self, fd: RawFd, request_ptr: *mut IoRequestData);

    // region read

    /// Registers a new `read` io operation.
    fn read(&mut self, fd: RawFd, ptr: *mut u8, len: u32, request_ptr: *mut IoRequestData);
    /// Registers a new `read` io operation with __fixed__ buffer.
    fn read_fixed(
        &mut self,
        fd: RawFd,
        ptr: *mut u8,
        len: u32,
        buf_index: u16,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `pread` io operation.
    fn pread(
        &mut self,
        fd: RawFd,
        ptr: *mut u8,
        len: u32,
        offset: usize,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `pread` io operation with __fixed__ buffer.
    fn pread_fixed(
        &mut self,
        fd: RawFd,
        ptr: *mut u8,
        len: u32,
        buf_index: u16,
        offset: usize,
        request_ptr: *mut IoRequestData,
    );

    // endregion

    // region write

    /// Registers a new `write` io operation.
    fn write(&mut self, fd: RawFd, ptr: *const u8, len: u32, request_ptr: *mut IoRequestData);
    /// Registers a new `write` io operation with __fixed__ buffer.
    fn write_fixed(
        &mut self,
        fd: RawFd,
        ptr: *const u8,
        len: u32,
        buf_index: u16,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `pwrite` io operation.
    fn pwrite(
        &mut self,
        fd: RawFd,
        ptr: *const u8,
        len: u32,
        offset: usize,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `pwrite` io operation with __fixed__ buffer.
    fn pwrite_fixed(
        &mut self,
        fd: RawFd,
        ptr: *const u8,
        len: u32,
        buf_index: u16,
        offset: usize,
        request_ptr: *mut IoRequestData,
    );

    // endregion

    /// Registers a new `close` io operation.
    fn close(&mut self, fd: RawFd, request_ptr: *mut IoRequestData);
    /// Registers a new `rename` io operation.
    fn rename(
        &mut self,
        old_path: *const libc::c_char,
        new_path: *const libc::c_char,
        request_ptr: *mut IoRequestData,
    );
    /// Registers a new `mkdir` io operation.
    fn create_dir(&mut self, path: *const libc::c_char, mode: u32, request_ptr: *mut IoRequestData);
    /// Registers a new `unlink` io operation.
    fn remove_file(&mut self, path: *const libc::c_char, request_ptr: *mut IoRequestData);
    /// Registers a new `rmdir` io operation.
    fn remove_dir(&mut self, path: *const libc::c_char, request_ptr: *mut IoRequestData);
}