open_entry_bindings/
virtual_thread.rs

1use std::{pin::Pin, sync::{Arc, atomic::AtomicU64}, marker::PhantomPinned, collections::HashSet};
2
3use tokio::sync::MutexGuard;
4
5use crate::{runtime::Runtime, shared_memory::SharedMemory, executor::ExecutorLock, stack::Stack, register::Register, extensions::Extension, block_info::BlockInfo, shutdown_type::ShutdownType, ffi_primitive::FfiPrimitive, extension_data::ExtensionData};
6
7pub type VThread = Pin<Arc<VirtualThread>>;
8
9pub struct VirtualThread {
10    pub runtime: Arc<Runtime>,
11    pub memory: SharedMemory,
12    pub lock: ExecutorLock,
13    pub stack: Stack,
14
15    pub extension_data: ExtensionData,
16
17    pub registers: [Register; 16],
18    _stack_size: usize,
19    pub flags: AtomicU64,
20
21    _phantom: PhantomPinned
22}
23
24impl VirtualThread {
25    #[inline(always)] pub fn get_extension(&self, id: u32) -> Arc<Extension> {
26        unsafe { crate::VirtualThread__get_extension.unwrap_unchecked()(self, id) }
27    }
28    
29    #[inline(always)] pub async fn get_temp_vmstrs(&self) -> MutexGuard<'_, HashSet<(u64, usize)>> {
30        unsafe { crate::VirtualThread__get_temp_vmstrs.unwrap_unchecked()(self).await }
31    }
32
33    #[inline(always)] pub async fn set_error_data(&self, data: impl Into<String>) {
34        unsafe { crate::VirtualThread__set_error_data.unwrap_unchecked()(self, data.into()).await }
35    }
36
37    #[inline(always)] pub fn get_block_info(&self) -> Arc<BlockInfo> {
38        unsafe { crate::VirtualThread__get_block_info.unwrap_unchecked()(self) }
39    }
40
41    #[inline(always)] pub fn should_stop(&self) -> bool {
42        unsafe { crate::VirtualThread__should_stop.unwrap_unchecked()(self) }
43    }
44    
45    #[inline(always)] pub async fn spawn(&self, addr: u64) {
46        unsafe { crate::VirtualThread__spawn.unwrap_unchecked()(self, addr).await }
47    }
48    
49    #[inline(always)] pub fn set_flag(&self, id: u64, value: bool) {
50        unsafe { crate::VirtualThread__set_flag.unwrap_unchecked()(self, id, value) }
51    }
52
53    #[inline(always)] pub fn get_flag(&self, id: u64) -> bool {
54        unsafe { crate::VirtualThread__get_flag.unwrap_unchecked()(self, id) }
55    }
56
57    #[inline(always)] pub fn sub32(&self, register: u8, amount: u32) {
58        unsafe { crate::VirtualThread__sub32.unwrap_unchecked()(self, register, amount) }
59    }
60    
61    #[inline(always)] pub fn add32(&self, register: u8, amount: u32) {
62        unsafe { crate::VirtualThread__add32.unwrap_unchecked()(self, register, amount) }
63    }
64
65    #[inline(always)] pub fn inc_inst(&self, amount: u64) {
66        unsafe { crate::VirtualThread__inc_inst.unwrap_unchecked()(self, amount) }
67    }
68
69    #[inline(always)] pub fn push(&self, value: u64) {
70        unsafe { crate::VirtualThread__push.unwrap_unchecked()(self, value) }
71    }
72
73    #[inline(always)] pub fn pop(&self) -> u64 {
74        unsafe { crate::VirtualThread__pop.unwrap_unchecked()(self) }
75    }
76    
77    #[inline(always)] pub fn shutdown(self: VThread, shutdown_type: ShutdownType) {
78        unsafe { crate::VirtualThread__shutdown.unwrap_unchecked()(self, shutdown_type) }
79    }
80    
81    #[inline(always)] pub fn dispose(self: VThread) {
82        unsafe { crate::VirtualThread__dispose.unwrap_unchecked()(self) }
83    }
84    
85    #[inline(always)] pub fn get_mem<T: FfiPrimitive>(&self, addr: usize) -> T {
86        unsafe { T::get_mem(self, addr) }
87    }
88
89    #[inline(always)] pub fn get_mem_absolute<T: FfiPrimitive>(&self, addr: usize) -> T {
90        unsafe { T::get_mem_absolute(self, addr) }
91    }
92
93    #[inline(always)] pub fn set_mem_absolute<T: FfiPrimitive>(&self, addr: usize, data: T) {
94        unsafe { T::set_mem_absolute(self, addr, data) }
95    }
96
97    #[inline(always)] pub fn set_reg<T: FfiPrimitive>(&self, register: u8, data: T) {
98        unsafe { T::set_reg(self, register, data) }
99    }
100
101    #[inline(always)] pub fn get_reg<T: FfiPrimitive>(&self, register: u8) -> T {
102        unsafe { T::get_reg(self, register) }
103    }
104}