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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Platform abstraction layer for system APIs.
//!
//! This module provides unified access to platform-specific implementations of
//! common system APIs like time and threading. It automatically selects the
//! appropriate implementation based on the compilation target, allowing the rest
//! of the codebase to use these APIs without platform-specific conditionals.
//!
//! # Overview
//!
//! The module re-exports different implementations depending on the target:
//! - **Native platforms**: Uses the standard library implementations
//! - **WebAssembly**: Uses WebAssembly-compatible alternatives
//!
//! This abstraction is crucial for the exfiltrate library's ability to run
//! both as a native application and in WebAssembly environments (like browsers),
//! which is essential for its debugging and MCP server embedding capabilities.
//!
//! # Platform Implementations
//!
//! ## Time API
//! - **Native**: `std::time` - Full standard library time support
//! - **WASM**: `web_time` - Browser-compatible time API using performance.now()
//!
//! ## Threading API
//! - **Native**: `std::thread` - OS threads with full standard library support
//! - **WASM**: `wasm_thread` - Web Workers-based threading for browsers
//!
//! # Design Philosophy
//!
//! As mentioned in the project README, this codebase deliberately avoids tokio
//! and async runtimes in favor of threads. This module ensures that threading
//! works consistently across all platforms, including WebAssembly where traditional
//! threads aren't available. "Threads for everyone" means using Web Workers on
//! WASM and OS threads on native platforms.
//!
//! # Examples
//!
//! ```
//! # mod sys {
//! # pub use std::time;
//! # pub use std::thread;
//! # }
//! use sys::time::{Duration, Instant};
//! use sys::thread;
//!
//! // Time operations work consistently across platforms
//! let start = Instant::now();
//! thread::sleep(Duration::from_millis(100));
//! let elapsed = start.elapsed();
//! println!("Operation took {:?}", elapsed);
//!
//! // Threading works on both native and WASM
//! let handle = thread::spawn(|| {
//! println!("Running in a thread!");
//! 42
//! });
//! let result = handle.join().unwrap();
//! assert_eq!(result, 42);
//! ```
//!
//! # Usage Guidelines
//!
//! Always import time and thread APIs through this module rather than directly
//! from std or platform-specific crates. This ensures your code remains portable:
/// Platform-appropriate threading API.
///
/// Re-exports the appropriate threading implementation based on the target platform:
/// - Native platforms: `std::thread` (OS threads)
/// - WebAssembly: `wasm_thread` (Web Workers)
///
/// # Available Functionality
///
/// The following threading primitives are available:
/// - `spawn` - Create a new thread
/// - `sleep` - Block the current thread for a duration
/// - `yield_now` - Yield execution to other threads
/// - `JoinHandle` - Handle for joining spawned threads
/// - `ThreadId` - Unique thread identifier
/// - `current` - Get current thread information
///
/// # Examples
///
/// ```
/// # mod sys {
/// # pub use std::thread;
/// # }
/// use sys::thread;
/// use std::sync::Arc;
/// use std::sync::atomic::{AtomicUsize, Ordering};
///
/// // Spawn a thread
/// let handle = thread::spawn(|| {
/// println!("Hello from thread!");
/// 42
/// });
///
/// // Wait for completion
/// let result = handle.join().unwrap();
/// assert_eq!(result, 42);
///
/// // Share data between threads
/// let counter = Arc::new(AtomicUsize::new(0));
/// let counter_clone = counter.clone();
///
/// thread::spawn(move || {
/// counter_clone.fetch_add(1, Ordering::SeqCst);
/// });
/// ```
///
/// # WASM Limitations
///
/// When running in WebAssembly:
/// - Threads are implemented using Web Workers
/// - `thread::park` and `thread::unpark` may have different semantics
/// - Thread-local storage has limitations
/// - Maximum thread count may be limited by browser
/// - Shared memory requires specific CORS headers
///
/// # Design Rationale
///
/// This abstraction supports the project's "threads for everyone" philosophy,
/// enabling consistent multi-threaded programming across all platforms without
/// requiring async/await or runtime dependencies like tokio.
pub use wasm_thread as thread;
pub use thread;