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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use crate::io::IoWorkerConfig;
use crate::utils::SpinLock;
use crate::BUG_MESSAGE;
use std::mem::discriminant;

/// A shared config of state of the all runtime.
/// It is used to prevent unsafe behavior in the runtime.
///
/// For example, when the task that uses an IO worker is
/// shared with the [`Executor`](crate::runtime::executor::Executor), that has no IO worker.
#[allow(clippy::struct_excessive_bools, reason = "False positive")]
struct ConfigStats {
    number_of_executors_with_enabled_io_worker_and_work_sharing: usize,
    number_of_executors_with_enabled_thread_pool_and_work_sharing: usize,
    number_of_executors_with_work_sharing_and_without_io_worker: usize,
    number_of_executors_with_work_sharing_and_without_thread_pool: usize,
}

impl ConfigStats {
    /// Create a new config stats.
    const fn new() -> Self {
        Self {
            number_of_executors_with_enabled_io_worker_and_work_sharing: 0,
            number_of_executors_with_enabled_thread_pool_and_work_sharing: 0,
            number_of_executors_with_work_sharing_and_without_io_worker: 0,
            number_of_executors_with_work_sharing_and_without_thread_pool: 0,
        }
    }
}

/// A shared config of state of the all runtime.
static GLOBAL_CONFIG_STATS: SpinLock<ConfigStats> = SpinLock::new(ConfigStats::new());

/// The default [`buffers`](crate::io::Buffer) capacity.
pub const DEFAULT_BUF_CAP: u32 = 4096;

/// Config that can be used to create an Executor, because it is valid.
#[derive(Clone)]
pub(crate) struct ValidConfig {
    pub(crate) buffer_cap: u32,
    pub(crate) io_worker_config: Option<IoWorkerConfig>,
    pub(crate) number_of_thread_workers: usize,
    /// If it is `usize::MAX`, it means that work sharing is disabled.
    pub(crate) work_sharing_level: usize,
}

impl ValidConfig {
    /// Returns whether the IO worker is enabled.
    pub const fn is_work_sharing_enabled(&self) -> bool {
        self.work_sharing_level != usize::MAX
    }

    /// Returns whether the thread pool is enabled.
    pub const fn is_thread_pool_enabled(&self) -> bool {
        self.number_of_thread_workers != 0
    }
}

impl Drop for ValidConfig {
    fn drop(&mut self) {
        if self.work_sharing_level != usize::MAX {
            let mut guard = Some(GLOBAL_CONFIG_STATS.lock());
            let shared_config_stats = guard.as_mut().expect(BUG_MESSAGE);
            if self.io_worker_config.is_some() {
                shared_config_stats.number_of_executors_with_enabled_io_worker_and_work_sharing -=
                    1;
            } else {
                shared_config_stats.number_of_executors_with_work_sharing_and_without_io_worker -=
                    1;
            }

            if self.is_thread_pool_enabled() {
                shared_config_stats
                    .number_of_executors_with_enabled_thread_pool_and_work_sharing -= 1;
            } else {
                shared_config_stats
                    .number_of_executors_with_work_sharing_and_without_thread_pool -= 1;
            }
        }
    }
}

/// `Config` is a configuration struct used for controlling various parameters
/// related to buffers, I/O workers, thread workers, and work-sharing behavior.
///
/// # Fields
/// - `buffer_cap`: The size of the [`buffers`](crate::io::Buffer).
///
/// - `io_worker_config`: An optional configuration for I/O workers. If none is provided,
///   the IO worker will be disabled.
///
/// - `number_of_thread_workers`: The number of thread workers to spawn. If zero is provided,
///   the thread pool will be disabled.
///
/// - `work_sharing_level`: The level of work sharing between threads. It is responsible for
///   how many tasks the [`Executor`](crate::runtime::executor::Executor) can hold before assigning
///   them to the shared queue.
///   If [`usize::MAX`] is provided, work sharing will be disabled.
#[derive(Clone, Copy)]
pub struct Config {
    /// The size of the [`buffers`](crate::io::Buffer).
    buffer_cap: u32,
    /// An optional configuration for I/O workers. If none is provided,
    /// the IO worker will be disabled.
    io_worker_config: Option<IoWorkerConfig>,
    /// The number of thread workers to spawn. If zero is provided,
    /// the thread pool will be disabled.
    number_of_thread_workers: usize,
    /// The level of work sharing between threads. It is responsible for
    /// how many tasks the [`Executor`](crate::runtime::executor::Executor) can hold before assigning
    /// them to the shared queue.
    /// If [`usize::MAX`] is provided, work sharing will be disabled.
    work_sharing_level: usize,
}

const AN_ATTEMPT_TO_CREATE_EXECUTOR_WITH_WORK_SHARING_AND_IO_WORKER: &str = "\
    An attempt to create an Executor with work sharing and with an \
    IO worker has failed because another Executor was created with \
    work sharing enabled and without an IO worker enabled. \
    This is unacceptable because an Executor who does not have an \
    IO worker cannot take on a task that requires an IO worker.";

const AN_ATTEMPT_TO_CREATE_EXECUTOR_WITH_WORK_SHARING_AND_WITHOUT_IO_WORKER: &str = "\
    An attempt to create an Executor with work sharing and without an \
    IO worker has failed because another Executor was created with \
    an IO worker and work sharing enabled. \
    This is unacceptable because an Executor who does not have an \
    IO worker cannot take on a task that requires an IO worker.";

const AN_ATTEMPT_TO_CREATE_EXECUTOR_WITH_WORK_SHARING_AND_THREAD_POOL: &str = "\
    An attempt to create an Executor with work sharing and with a \
    thread pool enabled has failed because another Executor was created with \
    work sharing enabled and without a thread pool enabled. \
    This is unacceptable because an Executor who does not have a \
    thread pool cannot take on a task that requires a thread pool.";

const AN_ATTEMPT_TO_CREATE_EXECUTOR_WITH_WORK_SHARING_AND_WITHOUT_THREAD_POOL: &str = "\
    An attempt to create an Executor with work sharing and without a \
    thread pool enabled has failed because another Executor was created with \
    both a thread pool and work sharing enabled. \
    This is unacceptable because an Executor who does not have a \
    thread pool cannot take on a task that requires a thread pool.";

impl Config {
    /// Returns a default [`Config`].
    pub const fn default() -> Self {
        Self {
            buffer_cap: DEFAULT_BUF_CAP,
            io_worker_config: Some(IoWorkerConfig::default()),
            number_of_thread_workers: 1,
            work_sharing_level: 7,
        }
    }

    /// Returns the capacity of the [`buffers`](crate::io::Buffer).
    pub const fn buffer_cap(&self) -> u32 {
        self.buffer_cap
    }

    /// Sets the capacity of the [`buffers`](crate::io::Buffer).
    #[must_use]
    pub const fn set_buffer_cap(mut self, buf_cap: u32) -> Self {
        self.buffer_cap = buf_cap;

        self
    }

    /// Returns the optional configuration for I/O workers. If none is returned,
    /// the IO worker is disabled.
    pub const fn io_worker_config(&self) -> Option<IoWorkerConfig> {
        self.io_worker_config
    }

    /// Sets the optional configuration for I/O workers. If none is provided,
    /// the IO worker will be disabled.
    pub const fn set_io_worker_config(
        mut self,
        io_worker_config: Option<IoWorkerConfig>,
    ) -> Result<Self, &'static str> {
        match io_worker_config {
            Some(io_worker_config) => {
                if let Err(err) = io_worker_config.validate() {
                    return Err(err);
                }

                self.io_worker_config = Some(io_worker_config);
            }
            None => {
                self.io_worker_config = None;
            }
        }

        Ok(self)
    }

    /// Disables the IO worker.
    #[must_use]
    pub const fn disable_io_worker(mut self) -> Self {
        self.io_worker_config = None;

        self
    }

    /// Returns the number of thread workers to spawn. If zero is returned,
    /// the thread pool is disabled.
    pub const fn number_of_thread_workers(&self) -> usize {
        self.number_of_thread_workers
    }

    /// Returns whether the thread pool is enabled.
    pub const fn is_thread_pool_enabled(&self) -> bool {
        self.number_of_thread_workers != 0
    }

    /// Sets the number of thread workers to spawn. If zero is provided,
    /// the thread pool will be disabled.
    #[must_use]
    pub const fn set_numbers_of_thread_workers(mut self, number_of_thread_workers: usize) -> Self {
        self.number_of_thread_workers = number_of_thread_workers;

        self
    }

    /// Returns whether the work sharing is enabled.
    pub const fn is_work_sharing_enabled(&self) -> bool {
        self.work_sharing_level != usize::MAX
    }

    /// Enables the work sharing.
    #[must_use]
    pub const fn enable_work_sharing(mut self) -> Self {
        if self.work_sharing_level == usize::MAX {
            self.work_sharing_level = 7;
        }

        self
    }

    /// Disables the work sharing.
    #[must_use]
    pub const fn disable_work_sharing(mut self) -> Self {
        self.work_sharing_level = usize::MAX;

        self
    }

    /// Sets the level of work sharing between threads. It is responsible for
    /// how many tasks the [`Executor`](crate::runtime::executor::Executor) can hold before assigning
    /// them to the shared queue.
    /// If [`usize::MAX`] is provided, work sharing will be disabled.
    #[must_use]
    pub const fn set_work_sharing_level(mut self, work_sharing_level: usize) -> Self {
        if work_sharing_level == 0 {
            self.work_sharing_level = 1;
        } else {
            self.work_sharing_level = work_sharing_level;
        }

        self
    }

    /// Validates the configuration.
    #[must_use]
    pub(crate) fn validate(self) -> ValidConfig {
        if self.work_sharing_level != usize::MAX {
            let mut shared_config_stats = GLOBAL_CONFIG_STATS.lock();

            if self.io_worker_config.is_some() {
                if shared_config_stats.number_of_executors_with_work_sharing_and_without_io_worker
                    != 0
                {
                    panic!("{AN_ATTEMPT_TO_CREATE_EXECUTOR_WITH_WORK_SHARING_AND_IO_WORKER}");
                }

                shared_config_stats.number_of_executors_with_enabled_io_worker_and_work_sharing +=
                    1;
            } else {
                if shared_config_stats.number_of_executors_with_enabled_io_worker_and_work_sharing
                    != 0
                {
                    panic!(
                        "{AN_ATTEMPT_TO_CREATE_EXECUTOR_WITH_WORK_SHARING_AND_WITHOUT_IO_WORKER}"
                    );
                }

                shared_config_stats.number_of_executors_with_work_sharing_and_without_io_worker +=
                    1;
            }

            if self.is_thread_pool_enabled() {
                if shared_config_stats.number_of_executors_with_work_sharing_and_without_thread_pool
                    != 0
                {
                    panic!("{AN_ATTEMPT_TO_CREATE_EXECUTOR_WITH_WORK_SHARING_AND_THREAD_POOL}");
                }

                shared_config_stats
                    .number_of_executors_with_enabled_thread_pool_and_work_sharing += 1;
            } else {
                if shared_config_stats.number_of_executors_with_enabled_thread_pool_and_work_sharing
                    != 0
                {
                    panic!(
                        "{AN_ATTEMPT_TO_CREATE_EXECUTOR_WITH_WORK_SHARING_AND_WITHOUT_THREAD_POOL}"
                    );
                }

                shared_config_stats
                    .number_of_executors_with_work_sharing_and_without_thread_pool += 1;
            }
        }

        ValidConfig {
            buffer_cap: self.buffer_cap,
            io_worker_config: self.io_worker_config,
            number_of_thread_workers: self.number_of_thread_workers,
            work_sharing_level: self.work_sharing_level,
        }
    }
}

impl From<&ValidConfig> for Config {
    fn from(config: &ValidConfig) -> Self {
        Self {
            buffer_cap: config.buffer_cap,
            io_worker_config: config.io_worker_config,
            number_of_thread_workers: config.number_of_thread_workers,
            work_sharing_level: config.work_sharing_level,
        }
    }
}

impl PartialEq for Config {
    fn eq(&self, other: &Self) -> bool {
        self.buffer_cap == other.buffer_cap
            && discriminant(&self.io_worker_config) == discriminant(&other.io_worker_config)
            && self.number_of_thread_workers == other.number_of_thread_workers
            && self.work_sharing_level == other.work_sharing_level
    }
}

impl Eq for Config {}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate as orengine;
    use std::panic;
    use std::sync::atomic;
    use std::sync::{Condvar as STDCvar, Mutex as STDMutex};

    const NUMBER_OF_TESTS: usize = 6;

    pub(crate) static WAS_READY: (STDMutex<bool>, STDCvar) = (STDMutex::new(false), STDCvar::new());
    static NUMBER_OF_READY_TESTS: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
    static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    fn handle_test_ready() {
        let prev = NUMBER_OF_READY_TESTS.fetch_add(1, atomic::Ordering::SeqCst);
        if prev == NUMBER_OF_TESTS - 1 {
            *WAS_READY.0.lock().unwrap() = true;
            WAS_READY.1.notify_all();
        }

        assert!(prev < NUMBER_OF_TESTS, "{}", BUG_MESSAGE);
    }

    fn get_lock() -> std::sync::MutexGuard<'static, ()> {
        LOCK.lock().unwrap_or_else(|e| {
            LOCK.clear_poison();
            e.into_inner()
        })
    }

    #[orengine::test::test_local]
    fn test_default_config() {
        let lock = get_lock();
        let config = Config::default().validate();
        assert_eq!(config.buffer_cap, DEFAULT_BUF_CAP);
        assert!(config.io_worker_config.is_some());
        assert!(config.is_thread_pool_enabled());
        assert_ne!(config.work_sharing_level, usize::MAX);
        drop(lock);
        handle_test_ready();
    }

    #[orengine::test::test_local]
    fn test_config() {
        let lock = get_lock();
        let config = Config::default()
            .set_buffer_cap(1024)
            .set_io_worker_config(None)
            .unwrap()
            .set_numbers_of_thread_workers(0)
            .disable_work_sharing();

        let config = config.validate();
        assert_eq!(config.buffer_cap, 1024);
        assert!(config.io_worker_config.is_none());
        assert!(!config.is_thread_pool_enabled());
        assert_eq!(config.work_sharing_level, usize::MAX);
        assert!(!config.is_work_sharing_enabled());

        drop(lock);
        handle_test_ready();
    }

    fn handle_panic_in_config_test(func: impl FnOnce() + panic::UnwindSafe) {
        let lock = get_lock();
        let res = panic::catch_unwind(func);
        handle_test_ready();
        drop(lock);

        if let Err(err) = res {
            panic::resume_unwind(err);
        } else {
            panic!("test failed");
        }
    }

    // 4 cases for panic
    // 1 - first config with io worker and task, next with work sharing and without io worker
    // 2 - first config with work sharing and without io worker, next with io worker and work sharing
    // 3 - first config with work sharing and without thread pool, next with thread pool and work sharing
    // 4 - first config with thread pool and work sharing, next with work sharing and without thread pool
    #[orengine::test::test_local]
    #[allow(
        clippy::should_panic_without_expect,
        reason = "panic message is too long"
    )]
    #[should_panic]
    fn test_config_first_case_panic() {
        // with io worker and work sharing
        handle_panic_in_config_test(|| {
            let _first_config = Config::default().validate();
            let _second_config = Config::default()
                .set_io_worker_config(None)
                .unwrap()
                .enable_work_sharing()
                .validate();
        });
    }

    #[orengine::test::test_local]
    #[allow(
        clippy::should_panic_without_expect,
        reason = "panic message is too long"
    )]
    #[should_panic]
    fn test_config_second_case_panic() {
        // with work sharing and without io worker
        handle_panic_in_config_test(|| {
            let _first_config = Config::default()
                .set_io_worker_config(None)
                .unwrap()
                .enable_work_sharing()
                .validate();
            let _second_config = Config::default().validate();
        });
    }

    #[orengine::test::test_local]
    #[allow(
        clippy::should_panic_without_expect,
        reason = "panic message is too long"
    )]
    #[should_panic]
    fn test_config_third_case_panic() {
        // with work sharing and without thread pool
        handle_panic_in_config_test(|| {
            let _first_config = Config::default()
                .set_numbers_of_thread_workers(0)
                .enable_work_sharing()
                .validate();
            let _second_config = Config::default().validate();
        });
    }

    #[orengine::test::test_local]
    #[allow(
        clippy::should_panic_without_expect,
        reason = "panic message is too long"
    )]
    #[should_panic]
    fn test_config_fourth_case_panic() {
        // with thread pool and work sharing
        handle_panic_in_config_test(|| {
            let _first_config = Config::default().validate();
            let _second_config = Config::default()
                .set_numbers_of_thread_workers(0)
                .enable_work_sharing()
                .validate();
        });
    }
}