misty_vm/client/
core.rs

1use std::{
2    ops::Deref,
3    sync::atomic::{AtomicBool, AtomicI32},
4};
5
6use crate::{
7    async_task::{IAsyncTaskRuntimeAdapter, MistyAsyncTaskPools},
8    resources::MistyResourceManager,
9    schedule::ScheduleManager,
10    services::MistyServiceManager,
11    signals::SignalEmitter,
12    states::MistyStateManager,
13    views::ViewNotifier,
14};
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub struct MistyClientId(i32);
18
19const _: () = {
20    static ALLOCATED: AtomicI32 = AtomicI32::new(1);
21    impl MistyClientId {
22        pub fn alloc() -> Self {
23            let id = ALLOCATED.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
24            return Self(id);
25        }
26    }
27    impl Deref for MistyClientId {
28        type Target = i32;
29
30        fn deref(&self) -> &Self::Target {
31            &self.0
32        }
33    }
34};
35
36pub(crate) struct MistyClientInner {
37    pub id: MistyClientId,
38    pub state_manager: MistyStateManager,
39    pub view_manager: Box<dyn ViewNotifier + Send + Sync>,
40    pub service_manager: MistyServiceManager,
41    pub resource_manager: MistyResourceManager,
42    pub async_task_pools: MistyAsyncTaskPools,
43    pub async_task_runtime: Box<dyn IAsyncTaskRuntimeAdapter + Send + Sync>,
44    pub schedule_manager: ScheduleManager,
45    pub signal_emitter: SignalEmitter,
46    pub destroyed: AtomicBool,
47}
48
49impl MistyClientInner {
50    pub fn is_destroyed(&self) -> bool {
51        self.destroyed.load(std::sync::atomic::Ordering::SeqCst)
52    }
53
54    pub fn destroy(&self) {
55        self.destroyed
56            .swap(true, std::sync::atomic::Ordering::SeqCst);
57
58        self.async_task_pools
59            .reset(self.async_task_runtime.as_ref());
60    }
61}
62
63impl Drop for MistyClientInner {
64    fn drop(&mut self) {
65        tracing::debug!("client {:?} is destroyed", self.id);
66    }
67}