summaryrefslogtreecommitdiff
path: root/monoio/src/driver/thread.rs
blob: a5465bfd99d3e9b03a4b9d7d73e10102f1afdd00 (plain)
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
use std::{
    sync::{LazyLock, Mutex},
    task::Waker,
};

use flume::Sender;
use fxhash::FxHashMap;

use crate::driver::UnparkHandle;

static UNPARK: LazyLock<Mutex<FxHashMap<usize, UnparkHandle>>> =
    LazyLock::new(|| Mutex::new(FxHashMap::default()));

static WAKER_SENDER: LazyLock<Mutex<FxHashMap<usize, Sender<Waker>>>> =
    LazyLock::new(|| Mutex::new(FxHashMap::default()));

macro_rules! lock {
    ($x: ident) => {
        $x.lock()
            .expect("Unable to lock global map, which is unexpected")
    };
}

pub(crate) fn register_unpark_handle(id: usize, unpark: UnparkHandle) {
    lock!(UNPARK).insert(id, unpark);
}

pub(crate) fn unregister_unpark_handle(id: usize) {
    lock!(UNPARK).remove(&id);
}

pub(crate) fn get_unpark_handle(id: usize) -> Option<UnparkHandle> {
    lock!(UNPARK).get(&id).cloned()
}

pub(crate) fn register_waker_sender(id: usize, sender: Sender<Waker>) {
    lock!(WAKER_SENDER).insert(id, sender);
}

pub(crate) fn unregister_waker_sender(id: usize) {
    lock!(WAKER_SENDER).remove(&id);
}

pub(crate) fn get_waker_sender(id: usize) -> Option<Sender<Waker>> {
    lock!(WAKER_SENDER).get(&id).cloned()
}