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
//! Thread model for .NET emulation.
//!
//! This module provides per-thread execution state for .NET emulation,
//! including call frames, evaluation stacks, thread scheduling, and
//! synchronization primitives. It implements a cooperative multithreading
//! model that allows emulating multi-threaded .NET applications.
//!
//! # Architecture
//!
//! The thread subsystem is organized into three main components:
//!
//! - **Thread State** ([`EmulationThread`]): Per-thread execution context including
//! call stack, evaluation stack, local variables, and exception handling state.
//!
//! - **Scheduling** ([`ThreadScheduler`]): Cooperative scheduler that manages thread
//! execution order using priority-based scheduling with time quantum slicing.
//!
//! - **Synchronization** ([`SyncState`]): Emulated .NET synchronization primitives
//! including monitors (`lock`), mutexes, semaphores, and events.
//!
//! # Thread Lifecycle
//!
//! Threads transition through the following states:
//!
//! 1. [`ThreadState::Ready`] - Thread is queued and ready to execute
//! 2. [`ThreadState::Running`] - Thread is actively executing instructions
//! 3. [`ThreadState::Waiting`] - Thread is blocked on a synchronization primitive
//! 4. [`ThreadState::Completed`] / [`ThreadState::Faulted`] / [`ThreadState::Aborted`] - Terminal states
//!
//! # Usage
//!
//! ```ignore
//! use dotscope::emulation::thread::{EmulationThread, ThreadScheduler, ThreadState};
//!
//! // Create a scheduler with 1000 instructions per time quantum
//! let mut scheduler = ThreadScheduler::new(1000);
//!
//! // Add the main thread
//! scheduler.add_main_thread(main_thread);
//!
//! // Run until completion or limit reached
//! while let Some(thread_id) = scheduler.select_next() {
//! // Execute instructions for the current thread
//! // ...
//! }
//! ```
//!
//! # Components
//!
//! ## Thread Execution
//!
//! - [`EmulationThread`] - Per-thread execution state with call stack and evaluation stack
//! - [`ThreadCallFrame`] - Individual call frame with locals, arguments, and instruction pointer
//! - [`ThreadState`] - Current execution state of a thread
//! - [`ThreadPriority`] - Thread scheduling priority levels
//! - [`WaitReason`] - Reason a thread is blocked/waiting
//!
//! ## Scheduling
//!
//! - [`ThreadScheduler`] - Cooperative thread scheduler with priority queues
//! - [`SchedulerOutcome`] - Result of scheduler operations (completion, deadlock, etc.)
//! - [`WakeCondition`] - Conditions for waking blocked threads
//!
//! ## Synchronization
//!
//! - [`SyncState`] - Central state for all synchronization primitives
//! - [`MonitorState`] - State of a monitor lock (used by `lock` keyword)
//! - [`MutexState`] - State of a mutex with ownership tracking
//! - [`SemaphoreState`] - State of a counting semaphore
//! - [`EventState`] - State of manual/auto reset events
//! - [`SyncError`] - Errors from synchronization operations
pub use ;
pub use ;
pub use ;