seq_runtime/
scheduler.rs

1//! Scheduler - Green Thread Management with May
2//!
3//! CSP-style concurrency for Seq using May coroutines.
4//! Each strand is a lightweight green thread that can communicate via channels.
5//!
6//! ## Non-Blocking Guarantee
7//!
8//! Channel operations (`send`, `receive`) use May's cooperative blocking and NEVER
9//! block OS threads. However, I/O operations (`write_line`, `read_line` in io.rs)
10//! currently use blocking syscalls. Future work will make all I/O non-blocking.
11//!
12//! ## Panic Behavior
13//!
14//! Functions panic on invalid input (null stacks, negative IDs, closed channels).
15//! In a production system, consider implementing error channels or Result-based
16//! error handling instead of panicking.
17
18use crate::stack::Stack;
19use crate::tagged_stack::StackValue;
20use may::coroutine;
21use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
22use std::sync::{Condvar, Mutex, Once};
23
24static SCHEDULER_INIT: Once = Once::new();
25
26// Strand lifecycle tracking
27//
28// Design rationale:
29// - ACTIVE_STRANDS: Lock-free atomic counter for the hot path (spawn/complete)
30//   Every strand increments on spawn, decrements on complete. This is extremely
31//   fast (lock-free atomic ops) and suitable for high-frequency operations.
32//
33// - SHUTDOWN_CONDVAR/MUTEX: Event-driven synchronization for the cold path (shutdown wait)
34//   Used only when waiting for all strands to complete (program shutdown).
35//   Condvar provides event-driven wakeup instead of polling, which is critical
36//   for a systems language - no CPU waste, proper OS-level blocking.
37//
38// Why not track JoinHandles?
39// Strands are like Erlang processes - potentially hundreds of thousands of concurrent
40// entities with independent lifecycles. Storing handles would require global mutable
41// state with synchronization overhead on the hot path. The counter + condvar approach
42// keeps the hot path lock-free while providing proper shutdown synchronization.
43pub static ACTIVE_STRANDS: AtomicUsize = AtomicUsize::new(0);
44static SHUTDOWN_CONDVAR: Condvar = Condvar::new();
45static SHUTDOWN_MUTEX: Mutex<()> = Mutex::new(());
46
47// Strand lifecycle statistics (for diagnostics)
48//
49// These counters provide observability into strand lifecycle without any locking.
50// All operations are lock-free atomic increments/loads.
51//
52// - TOTAL_SPAWNED: Monotonically increasing count of all strands ever spawned
53// - TOTAL_COMPLETED: Monotonically increasing count of all strands that completed
54// - PEAK_STRANDS: High-water mark of concurrent strands (helps detect strand leaks)
55//
56// Useful diagnostics:
57// - Currently running: ACTIVE_STRANDS
58// - Completed successfully: TOTAL_COMPLETED
59// - Potential leaks: TOTAL_SPAWNED - TOTAL_COMPLETED - ACTIVE_STRANDS > 0 (strands lost)
60// - Peak concurrency: PEAK_STRANDS
61pub static TOTAL_SPAWNED: AtomicU64 = AtomicU64::new(0);
62pub static TOTAL_COMPLETED: AtomicU64 = AtomicU64::new(0);
63pub static PEAK_STRANDS: AtomicUsize = AtomicUsize::new(0);
64
65// Unique strand ID generation
66static NEXT_STRAND_ID: AtomicU64 = AtomicU64::new(1);
67
68// =============================================================================
69// Lock-Free Strand Registry (only when diagnostics feature is enabled)
70// =============================================================================
71//
72// A fixed-size array of slots for tracking active strands without locks.
73// Each slot stores a strand ID (0 = free) and spawn timestamp.
74//
75// Design principles:
76// - Fixed size: No dynamic allocation, predictable memory footprint
77// - Lock-free: All operations use atomic CAS, no mutex contention
78// - Bounded: If registry is full, strands still run but aren't tracked
79// - Zero cost when not querying: Only diagnostics reads the registry
80//
81// Slot encoding:
82// - strand_id == 0: slot is free
83// - strand_id > 0: slot contains an active strand
84//
85// The registry size can be configured via SEQ_STRAND_REGISTRY_SIZE env var.
86// Default is 1024 slots, which is sufficient for most applications.
87//
88// When the "diagnostics" feature is disabled, the registry is not compiled,
89// eliminating the SystemTime::now() syscall and O(n) scans on every spawn.
90
91#[cfg(feature = "diagnostics")]
92/// Default strand registry size (number of trackable concurrent strands)
93const DEFAULT_REGISTRY_SIZE: usize = 1024;
94
95#[cfg(feature = "diagnostics")]
96/// A slot in the strand registry
97///
98/// Uses two atomics to store strand info without locks.
99/// A slot is free when strand_id == 0.
100pub struct StrandSlot {
101    /// Strand ID (0 = free, >0 = active strand)
102    pub strand_id: AtomicU64,
103    /// Spawn timestamp (seconds since UNIX epoch, for detecting stuck strands)
104    pub spawn_time: AtomicU64,
105}
106
107#[cfg(feature = "diagnostics")]
108impl StrandSlot {
109    const fn new() -> Self {
110        Self {
111            strand_id: AtomicU64::new(0),
112            spawn_time: AtomicU64::new(0),
113        }
114    }
115}
116
117#[cfg(feature = "diagnostics")]
118/// Lock-free strand registry
119///
120/// Provides O(n) registration (scan for free slot) and O(n) unregistration.
121/// This is acceptable because:
122/// 1. N is bounded (default 1024)
123/// 2. Registration/unregistration are infrequent compared to strand work
124/// 3. No locks means no contention, just atomic ops
125pub struct StrandRegistry {
126    slots: Box<[StrandSlot]>,
127    /// Number of slots that couldn't be registered (registry full)
128    pub overflow_count: AtomicU64,
129}
130
131#[cfg(feature = "diagnostics")]
132impl StrandRegistry {
133    /// Create a new registry with the given capacity
134    fn new(capacity: usize) -> Self {
135        let mut slots = Vec::with_capacity(capacity);
136        for _ in 0..capacity {
137            slots.push(StrandSlot::new());
138        }
139        Self {
140            slots: slots.into_boxed_slice(),
141            overflow_count: AtomicU64::new(0),
142        }
143    }
144
145    /// Register a strand, returning the slot index if successful
146    ///
147    /// Uses CAS to atomically claim a free slot.
148    /// Returns None if the registry is full (strand still runs, just not tracked).
149    pub fn register(&self, strand_id: u64) -> Option<usize> {
150        let spawn_time = std::time::SystemTime::now()
151            .duration_since(std::time::UNIX_EPOCH)
152            .map(|d| d.as_secs())
153            .unwrap_or(0);
154
155        // Scan for a free slot
156        for (idx, slot) in self.slots.iter().enumerate() {
157            // Set spawn time first, before claiming the slot
158            // This prevents a race where a reader sees strand_id != 0 but spawn_time == 0
159            // If we fail to claim the slot, the owner will overwrite this value anyway
160            slot.spawn_time.store(spawn_time, Ordering::Relaxed);
161
162            // Try to claim this slot (CAS from 0 to strand_id)
163            // AcqRel ensures the spawn_time write above is visible before strand_id becomes non-zero
164            if slot
165                .strand_id
166                .compare_exchange(0, strand_id, Ordering::AcqRel, Ordering::Relaxed)
167                .is_ok()
168            {
169                return Some(idx);
170            }
171        }
172
173        // Registry full - track overflow but strand still runs
174        self.overflow_count.fetch_add(1, Ordering::Relaxed);
175        None
176    }
177
178    /// Unregister a strand by ID
179    ///
180    /// Scans for the slot containing this strand ID and clears it.
181    /// Returns true if found and cleared, false if not found.
182    ///
183    /// Note: ABA problem is not a concern here because strand IDs are monotonically
184    /// increasing u64 values. ID reuse would require 2^64 spawns, which is practically
185    /// impossible (at 1 billion spawns/sec, it would take ~584 years).
186    pub fn unregister(&self, strand_id: u64) -> bool {
187        for slot in self.slots.iter() {
188            // Check if this slot contains our strand
189            if slot
190                .strand_id
191                .compare_exchange(strand_id, 0, Ordering::AcqRel, Ordering::Relaxed)
192                .is_ok()
193            {
194                // Successfully cleared the slot
195                slot.spawn_time.store(0, Ordering::Release);
196                return true;
197            }
198        }
199        false
200    }
201
202    /// Iterate over active strands (for diagnostics)
203    ///
204    /// Returns an iterator of (strand_id, spawn_time) for non-empty slots.
205    /// Note: This is a snapshot and may be slightly inconsistent due to concurrent updates.
206    pub fn active_strands(&self) -> impl Iterator<Item = (u64, u64)> + '_ {
207        self.slots.iter().filter_map(|slot| {
208            // Acquire on strand_id synchronizes with the Release in register()
209            let id = slot.strand_id.load(Ordering::Acquire);
210            if id > 0 {
211                // Relaxed is sufficient here - we've already synchronized via strand_id Acquire
212                // and spawn_time is written before strand_id in register()
213                let time = slot.spawn_time.load(Ordering::Relaxed);
214                Some((id, time))
215            } else {
216                None
217            }
218        })
219    }
220
221    /// Get the registry capacity
222    pub fn capacity(&self) -> usize {
223        self.slots.len()
224    }
225}
226
227// Global strand registry (lazy initialized)
228#[cfg(feature = "diagnostics")]
229static STRAND_REGISTRY: std::sync::OnceLock<StrandRegistry> = std::sync::OnceLock::new();
230
231/// Get or initialize the global strand registry
232#[cfg(feature = "diagnostics")]
233pub fn strand_registry() -> &'static StrandRegistry {
234    STRAND_REGISTRY.get_or_init(|| {
235        let size = std::env::var("SEQ_STRAND_REGISTRY_SIZE")
236            .ok()
237            .and_then(|s| s.parse().ok())
238            .unwrap_or(DEFAULT_REGISTRY_SIZE);
239        StrandRegistry::new(size)
240    })
241}
242
243/// Default coroutine stack size: 128KB (0x20000 bytes)
244/// Reduced from 1MB for better spawn performance (~16% faster in benchmarks).
245/// Can be overridden via SEQ_STACK_SIZE environment variable.
246const DEFAULT_STACK_SIZE: usize = 0x20000;
247
248/// Parse stack size from an optional string value.
249/// Returns the parsed size, or DEFAULT_STACK_SIZE if the value is missing, zero, or invalid.
250/// Prints a warning to stderr for invalid values.
251fn parse_stack_size(env_value: Option<String>) -> usize {
252    match env_value {
253        Some(val) => match val.parse::<usize>() {
254            Ok(0) => {
255                eprintln!(
256                    "Warning: SEQ_STACK_SIZE=0 is invalid, using default {}",
257                    DEFAULT_STACK_SIZE
258                );
259                DEFAULT_STACK_SIZE
260            }
261            Ok(size) => size,
262            Err(_) => {
263                eprintln!(
264                    "Warning: SEQ_STACK_SIZE='{}' is not a valid number, using default {}",
265                    val, DEFAULT_STACK_SIZE
266                );
267                DEFAULT_STACK_SIZE
268            }
269        },
270        None => DEFAULT_STACK_SIZE,
271    }
272}
273
274/// Default coroutine pool capacity.
275/// May reuses completed coroutine stacks from this pool to avoid allocations.
276/// Default of 1000 is often too small for spawn-heavy workloads.
277const DEFAULT_POOL_CAPACITY: usize = 10000;
278
279/// Initialize the scheduler.
280///
281/// # Safety
282/// Safe to call multiple times (idempotent via Once).
283/// Configures May coroutines with appropriate stack size for LLVM-generated code.
284#[unsafe(no_mangle)]
285pub unsafe extern "C" fn patch_seq_scheduler_init() {
286    SCHEDULER_INIT.call_once(|| {
287        // Configure stack size for coroutines
288        // Default is 128KB, reduced from 1MB for better spawn performance.
289        // Can be overridden via SEQ_STACK_SIZE environment variable (in bytes)
290        // Example: SEQ_STACK_SIZE=2097152 for 2MB
291        // Invalid values (non-numeric, zero) are warned and ignored.
292        let stack_size = parse_stack_size(std::env::var("SEQ_STACK_SIZE").ok());
293
294        // Configure coroutine pool capacity
295        // May reuses coroutine stacks from this pool to reduce allocation overhead.
296        // Default 10000 is 10x May's default (1000), better for spawn-heavy workloads.
297        // Can be overridden via SEQ_POOL_CAPACITY environment variable.
298        let pool_capacity = std::env::var("SEQ_POOL_CAPACITY")
299            .ok()
300            .and_then(|s| s.parse().ok())
301            .filter(|&v| v > 0)
302            .unwrap_or(DEFAULT_POOL_CAPACITY);
303
304        may::config()
305            .set_stack_size(stack_size)
306            .set_pool_capacity(pool_capacity);
307
308        // Install SIGQUIT handler for runtime diagnostics (kill -3)
309        #[cfg(feature = "diagnostics")]
310        crate::diagnostics::install_signal_handler();
311
312        // Install watchdog timer (if enabled via SEQ_WATCHDOG_SECS)
313        #[cfg(feature = "diagnostics")]
314        crate::watchdog::install_watchdog();
315    });
316}
317
318/// Run the scheduler and wait for all coroutines to complete
319///
320/// # Safety
321/// Returns the final stack (always null for now since May handles all scheduling).
322/// This function blocks until all spawned strands have completed.
323///
324/// Uses a condition variable for event-driven shutdown synchronization rather than
325/// polling. The mutex is only held during the wait protocol, not during strand
326/// execution, so there's no contention on the hot path.
327#[unsafe(no_mangle)]
328pub unsafe extern "C" fn patch_seq_scheduler_run() -> Stack {
329    let mut guard = SHUTDOWN_MUTEX.lock().expect(
330        "scheduler_run: shutdown mutex poisoned - strand panicked during shutdown synchronization",
331    );
332
333    // Wait for all strands to complete
334    // The condition variable will be notified when the last strand exits
335    while ACTIVE_STRANDS.load(Ordering::Acquire) > 0 {
336        guard = SHUTDOWN_CONDVAR
337            .wait(guard)
338            .expect("scheduler_run: condvar wait failed - strand panicked during shutdown wait");
339    }
340
341    // All strands have completed
342    std::ptr::null_mut()
343}
344
345/// Shutdown the scheduler
346///
347/// # Safety
348/// Safe to call. May doesn't require explicit shutdown, so this is a no-op.
349#[unsafe(no_mangle)]
350pub unsafe extern "C" fn patch_seq_scheduler_shutdown() {
351    // May doesn't require explicit shutdown
352    // This function exists for API symmetry with init
353}
354
355/// Spawn a strand (coroutine) with initial stack
356///
357/// # Safety
358/// - `entry` must be a valid function pointer that can safely execute on any thread
359/// - `initial_stack` must be either null or a valid pointer to a `StackValue` that:
360///   - Was heap-allocated (e.g., via Box)
361///   - Has a 'static lifetime or lives longer than the coroutine
362///   - Is safe to access from the spawned thread
363/// - The caller transfers ownership of `initial_stack` to the coroutine
364/// - Returns a unique strand ID (positive integer)
365///
366/// # Memory Management
367/// The spawned coroutine takes ownership of `initial_stack` and will automatically
368/// free the final stack returned by `entry` upon completion.
369#[unsafe(no_mangle)]
370pub unsafe extern "C" fn patch_seq_strand_spawn(
371    entry: extern "C" fn(Stack) -> Stack,
372    initial_stack: Stack,
373) -> i64 {
374    // For backwards compatibility, use null base (won't support nested spawns)
375    unsafe { patch_seq_strand_spawn_with_base(entry, initial_stack, std::ptr::null_mut()) }
376}
377
378/// Spawn a strand (coroutine) with initial stack and explicit stack base
379///
380/// This variant allows setting the STACK_BASE for the spawned strand, which is
381/// required for the child to perform operations like clone_stack (nested spawn).
382///
383/// # Safety
384/// - `entry` must be a valid function pointer that can safely execute on any thread
385/// - `initial_stack` must be a valid pointer to a `StackValue` array
386/// - `stack_base` must be the base of the stack (or null to skip setting STACK_BASE)
387/// - The caller transfers ownership of `initial_stack` to the coroutine
388/// - Returns a unique strand ID (positive integer)
389#[unsafe(no_mangle)]
390pub unsafe extern "C" fn patch_seq_strand_spawn_with_base(
391    entry: extern "C" fn(Stack) -> Stack,
392    initial_stack: Stack,
393    stack_base: Stack,
394) -> i64 {
395    // Generate unique strand ID
396    let strand_id = NEXT_STRAND_ID.fetch_add(1, Ordering::Relaxed);
397
398    // Increment active strand counter and track total spawned
399    let new_count = ACTIVE_STRANDS.fetch_add(1, Ordering::Release) + 1;
400    TOTAL_SPAWNED.fetch_add(1, Ordering::Relaxed);
401
402    // Update peak strands if this is a new high-water mark
403    // Uses a CAS loop to safely update the maximum without locks
404    // Uses Acquire/Release ordering for proper synchronization with diagnostics reads
405    let mut peak = PEAK_STRANDS.load(Ordering::Acquire);
406    while new_count > peak {
407        match PEAK_STRANDS.compare_exchange_weak(
408            peak,
409            new_count,
410            Ordering::Release,
411            Ordering::Relaxed,
412        ) {
413            Ok(_) => break,
414            Err(current) => peak = current,
415        }
416    }
417
418    // Register strand in the registry (for diagnostics visibility)
419    // If registry is full, strand still runs but isn't tracked
420    #[cfg(feature = "diagnostics")]
421    let _ = strand_registry().register(strand_id);
422
423    // Function pointers are already Send, no wrapper needed
424    let entry_fn = entry;
425
426    // Convert pointers to usize (which is Send)
427    // This is necessary because *mut T is !Send, but the caller guarantees thread safety
428    let stack_addr = initial_stack as usize;
429    let base_addr = stack_base as usize;
430
431    unsafe {
432        coroutine::spawn(move || {
433            // Reconstruct pointers from addresses
434            let stack_ptr = stack_addr as *mut StackValue;
435            let base_ptr = base_addr as *mut StackValue;
436
437            // Debug assertion: validate stack pointer alignment and reasonable address
438            debug_assert!(
439                stack_ptr.is_null()
440                    || stack_addr.is_multiple_of(std::mem::align_of::<StackValue>()),
441                "Stack pointer must be null or properly aligned"
442            );
443            debug_assert!(
444                stack_ptr.is_null() || stack_addr > 0x1000,
445                "Stack pointer appears to be in invalid memory region (< 0x1000)"
446            );
447
448            // Set STACK_BASE for this strand if provided
449            // This enables nested spawns and other operations that need clone_stack
450            if !base_ptr.is_null() {
451                crate::stack::patch_seq_set_stack_base(base_ptr);
452            }
453
454            // Execute the entry function
455            let final_stack = entry_fn(stack_ptr);
456
457            // Clean up the final stack to prevent memory leak
458            free_stack(final_stack);
459
460            // Unregister strand from registry (uses captured strand_id)
461            #[cfg(feature = "diagnostics")]
462            strand_registry().unregister(strand_id);
463
464            // Decrement active strand counter first, then track completion
465            // This ordering ensures the invariant SPAWNED = COMPLETED + ACTIVE + lost
466            // is never violated from an external observer's perspective
467            // Use AcqRel to establish proper synchronization (both acquire and release barriers)
468            let prev_count = ACTIVE_STRANDS.fetch_sub(1, Ordering::AcqRel);
469
470            // Track completion after decrementing active count
471            TOTAL_COMPLETED.fetch_add(1, Ordering::Release);
472            if prev_count == 1 {
473                // We were the last strand - acquire mutex and signal shutdown
474                // The mutex must be held when calling notify to prevent missed wakeups
475                let _guard = SHUTDOWN_MUTEX.lock()
476                    .expect("strand_spawn: shutdown mutex poisoned - strand panicked during shutdown notification");
477                SHUTDOWN_CONDVAR.notify_all();
478            }
479        });
480    }
481
482    strand_id as i64
483}
484
485/// Free a stack allocated by the runtime
486///
487/// With the tagged stack implementation, stack cleanup is handled differently.
488/// The contiguous array is freed when the TaggedStack is dropped.
489/// This function just resets the thread-local arena.
490///
491/// # Safety
492/// Stack pointer must be valid or null.
493fn free_stack(_stack: Stack) {
494    // With tagged stack, the array is freed when TaggedStack is dropped.
495    // We just need to reset the arena for thread-local strings.
496
497    // Reset the thread-local arena to free all arena-allocated strings
498    // This is safe because:
499    // - Any arena strings in Values have been dropped above
500    // - Global strings are unaffected (they have their own allocations)
501    // - Channel sends clone to global, so no cross-strand arena pointers
502    crate::arena::arena_reset();
503}
504
505/// Legacy spawn_strand function (kept for compatibility)
506///
507/// # Safety
508/// `entry` must be a valid function pointer that can safely execute on any thread.
509#[unsafe(no_mangle)]
510pub unsafe extern "C" fn patch_seq_spawn_strand(entry: extern "C" fn(Stack) -> Stack) {
511    unsafe {
512        patch_seq_strand_spawn(entry, std::ptr::null_mut());
513    }
514}
515
516/// Yield execution to allow other coroutines to run
517///
518/// # Safety
519/// Always safe to call from within a May coroutine.
520#[unsafe(no_mangle)]
521pub unsafe extern "C" fn patch_seq_yield_strand(stack: Stack) -> Stack {
522    coroutine::yield_now();
523    stack
524}
525
526/// Wait for all strands to complete
527///
528/// # Safety
529/// Always safe to call. Blocks until all spawned strands have completed.
530///
531/// Uses event-driven synchronization via condition variable - no polling overhead.
532#[unsafe(no_mangle)]
533pub unsafe extern "C" fn patch_seq_wait_all_strands() {
534    let mut guard = SHUTDOWN_MUTEX.lock()
535        .expect("wait_all_strands: shutdown mutex poisoned - strand panicked during shutdown synchronization");
536
537    // Wait for all strands to complete
538    // The condition variable will be notified when the last strand exits
539    while ACTIVE_STRANDS.load(Ordering::Acquire) > 0 {
540        guard = SHUTDOWN_CONDVAR
541            .wait(guard)
542            .expect("wait_all_strands: condvar wait failed - strand panicked during shutdown wait");
543    }
544}
545
546// Public re-exports with short names for internal use
547pub use patch_seq_scheduler_init as scheduler_init;
548pub use patch_seq_scheduler_run as scheduler_run;
549pub use patch_seq_scheduler_shutdown as scheduler_shutdown;
550pub use patch_seq_spawn_strand as spawn_strand;
551pub use patch_seq_strand_spawn as strand_spawn;
552pub use patch_seq_wait_all_strands as wait_all_strands;
553pub use patch_seq_yield_strand as yield_strand;
554
555#[cfg(test)]
556mod tests {
557    use super::*;
558    use crate::stack::push;
559    use crate::value::Value;
560    use std::sync::atomic::{AtomicU32, Ordering};
561
562    #[test]
563    fn test_spawn_strand() {
564        unsafe {
565            static COUNTER: AtomicU32 = AtomicU32::new(0);
566
567            extern "C" fn test_entry(_stack: Stack) -> Stack {
568                COUNTER.fetch_add(1, Ordering::SeqCst);
569                std::ptr::null_mut()
570            }
571
572            for _ in 0..100 {
573                spawn_strand(test_entry);
574            }
575
576            std::thread::sleep(std::time::Duration::from_millis(200));
577            assert_eq!(COUNTER.load(Ordering::SeqCst), 100);
578        }
579    }
580
581    #[test]
582    fn test_scheduler_init_idempotent() {
583        unsafe {
584            // Should be safe to call multiple times
585            scheduler_init();
586            scheduler_init();
587            scheduler_init();
588        }
589    }
590
591    #[test]
592    fn test_free_stack_null() {
593        // Freeing null should be a no-op
594        free_stack(std::ptr::null_mut());
595    }
596
597    #[test]
598    fn test_free_stack_valid() {
599        unsafe {
600            // Create a stack, then free it
601            let stack = push(crate::stack::alloc_test_stack(), Value::Int(42));
602            free_stack(stack);
603            // If we get here without crashing, test passed
604        }
605    }
606
607    #[test]
608    fn test_strand_spawn_with_stack() {
609        unsafe {
610            static COUNTER: AtomicU32 = AtomicU32::new(0);
611
612            extern "C" fn test_entry(stack: Stack) -> Stack {
613                COUNTER.fetch_add(1, Ordering::SeqCst);
614                // Return the stack as-is (caller will free it)
615                stack
616            }
617
618            let initial_stack = push(crate::stack::alloc_test_stack(), Value::Int(99));
619            strand_spawn(test_entry, initial_stack);
620
621            std::thread::sleep(std::time::Duration::from_millis(200));
622            assert_eq!(COUNTER.load(Ordering::SeqCst), 1);
623        }
624    }
625
626    #[test]
627    fn test_scheduler_shutdown() {
628        unsafe {
629            scheduler_init();
630            scheduler_shutdown();
631            // Should not crash
632        }
633    }
634
635    #[test]
636    fn test_many_strands_stress() {
637        unsafe {
638            static COUNTER: AtomicU32 = AtomicU32::new(0);
639
640            extern "C" fn increment(_stack: Stack) -> Stack {
641                COUNTER.fetch_add(1, Ordering::SeqCst);
642                std::ptr::null_mut()
643            }
644
645            // Reset counter for this test
646            COUNTER.store(0, Ordering::SeqCst);
647
648            // Spawn many strands to stress test synchronization
649            for _ in 0..1000 {
650                strand_spawn(increment, std::ptr::null_mut());
651            }
652
653            // Wait for all to complete
654            wait_all_strands();
655
656            // Verify all strands executed
657            assert_eq!(COUNTER.load(Ordering::SeqCst), 1000);
658        }
659    }
660
661    #[test]
662    fn test_strand_ids_are_unique() {
663        unsafe {
664            use std::collections::HashSet;
665
666            extern "C" fn noop(_stack: Stack) -> Stack {
667                std::ptr::null_mut()
668            }
669
670            // Spawn strands and collect their IDs
671            let mut ids = Vec::new();
672            for _ in 0..100 {
673                let id = strand_spawn(noop, std::ptr::null_mut());
674                ids.push(id);
675            }
676
677            // Wait for completion
678            wait_all_strands();
679
680            // Verify all IDs are unique
681            let unique_ids: HashSet<_> = ids.iter().collect();
682            assert_eq!(unique_ids.len(), 100, "All strand IDs should be unique");
683
684            // Verify all IDs are positive
685            assert!(
686                ids.iter().all(|&id| id > 0),
687                "All strand IDs should be positive"
688            );
689        }
690    }
691
692    #[test]
693    fn test_arena_reset_with_strands() {
694        unsafe {
695            use crate::arena;
696            use crate::seqstring::arena_string;
697
698            extern "C" fn create_temp_strings(stack: Stack) -> Stack {
699                // Create many temporary arena strings (simulating request parsing)
700                for i in 0..100 {
701                    let temp = arena_string(&format!("temporary string {}", i));
702                    // Use the string temporarily
703                    assert!(!temp.as_str().is_empty());
704                    // String is dropped, but memory stays in arena
705                }
706
707                // Arena should have allocated memory
708                let stats = arena::arena_stats();
709                assert!(stats.allocated_bytes > 0, "Arena should have allocations");
710
711                stack // Return empty stack
712            }
713
714            // Reset arena before test
715            arena::arena_reset();
716
717            // Spawn strand that creates many temp strings
718            strand_spawn(create_temp_strings, std::ptr::null_mut());
719
720            // Wait for strand to complete (which calls free_stack -> arena_reset)
721            wait_all_strands();
722
723            // After strand exits, arena should be reset
724            let stats_after = arena::arena_stats();
725            assert_eq!(
726                stats_after.allocated_bytes, 0,
727                "Arena should be reset after strand exits"
728            );
729        }
730    }
731
732    #[test]
733    fn test_arena_with_channel_send() {
734        unsafe {
735            use crate::channel::{close_channel, make_channel, receive, send};
736            use crate::stack::{pop, push};
737            use crate::value::Value;
738            use std::sync::Arc;
739            use std::sync::atomic::{AtomicI64, AtomicU32, Ordering};
740
741            static RECEIVED_COUNT: AtomicU32 = AtomicU32::new(0);
742            static CHANNEL_PTR: AtomicI64 = AtomicI64::new(0);
743
744            // Create channel
745            let stack = crate::stack::alloc_test_stack();
746            let stack = make_channel(stack);
747            let (stack, chan_val) = pop(stack);
748            let channel = match chan_val {
749                Value::Channel(ch) => ch,
750                _ => panic!("Expected Channel"),
751            };
752
753            // Store channel pointer for strands
754            let ch_ptr = Arc::as_ptr(&channel) as i64;
755            CHANNEL_PTR.store(ch_ptr, Ordering::Release);
756
757            // Keep Arc alive
758            std::mem::forget(channel.clone());
759            std::mem::forget(channel.clone());
760
761            // Sender strand: creates arena string, sends through channel
762            extern "C" fn sender(_stack: Stack) -> Stack {
763                use crate::seqstring::arena_string;
764                use crate::value::ChannelData;
765                use std::sync::Arc;
766
767                unsafe {
768                    let ch_ptr = CHANNEL_PTR.load(Ordering::Acquire) as *const ChannelData;
769                    let channel = Arc::from_raw(ch_ptr);
770                    let channel_clone = Arc::clone(&channel);
771                    std::mem::forget(channel); // Don't drop
772
773                    // Create arena string
774                    let msg = arena_string("Hello from sender!");
775
776                    // Push string and channel for send
777                    let stack = push(crate::stack::alloc_test_stack(), Value::String(msg));
778                    let stack = push(stack, Value::Channel(channel_clone));
779
780                    // Send (will clone to global)
781                    send(stack)
782                }
783            }
784
785            // Receiver strand: receives string from channel
786            extern "C" fn receiver(_stack: Stack) -> Stack {
787                use crate::value::ChannelData;
788                use std::sync::Arc;
789                use std::sync::atomic::Ordering;
790
791                unsafe {
792                    let ch_ptr = CHANNEL_PTR.load(Ordering::Acquire) as *const ChannelData;
793                    let channel = Arc::from_raw(ch_ptr);
794                    let channel_clone = Arc::clone(&channel);
795                    std::mem::forget(channel); // Don't drop
796
797                    // Push channel for receive
798                    let stack = push(
799                        crate::stack::alloc_test_stack(),
800                        Value::Channel(channel_clone),
801                    );
802
803                    // Receive message
804                    let stack = receive(stack);
805
806                    // Pop and verify message
807                    let (_stack, msg_val) = pop(stack);
808                    match msg_val {
809                        Value::String(s) => {
810                            assert_eq!(s.as_str(), "Hello from sender!");
811                            RECEIVED_COUNT.fetch_add(1, Ordering::SeqCst);
812                        }
813                        _ => panic!("Expected String"),
814                    }
815
816                    std::ptr::null_mut()
817                }
818            }
819
820            // Spawn sender and receiver
821            spawn_strand(sender);
822            spawn_strand(receiver);
823
824            // Wait for both strands
825            wait_all_strands();
826
827            // Verify message was received
828            assert_eq!(
829                RECEIVED_COUNT.load(Ordering::SeqCst),
830                1,
831                "Receiver should have received message"
832            );
833
834            // Clean up channel
835            let stack = push(stack, Value::Channel(channel));
836            close_channel(stack);
837        }
838    }
839
840    #[test]
841    fn test_no_memory_leak_over_many_iterations() {
842        // PR #11 feedback: Verify 10K+ strand iterations don't cause memory growth
843        unsafe {
844            use crate::arena;
845            use crate::seqstring::arena_string;
846
847            extern "C" fn allocate_strings_and_exit(stack: Stack) -> Stack {
848                // Simulate request processing: many temp allocations
849                for i in 0..50 {
850                    let temp = arena_string(&format!("request header {}", i));
851                    assert!(!temp.as_str().is_empty());
852                    // Strings dropped here but arena memory stays allocated
853                }
854                stack
855            }
856
857            // Run many iterations to detect leaks
858            let iterations = 10_000;
859
860            for i in 0..iterations {
861                // Reset arena before each iteration to start fresh
862                arena::arena_reset();
863
864                // Spawn strand, let it allocate strings, then exit
865                strand_spawn(allocate_strings_and_exit, std::ptr::null_mut());
866
867                // Wait for completion (triggers arena reset)
868                wait_all_strands();
869
870                // Every 1000 iterations, verify arena is actually reset
871                if i % 1000 == 0 {
872                    let stats = arena::arena_stats();
873                    assert_eq!(
874                        stats.allocated_bytes, 0,
875                        "Arena not reset after iteration {} (leaked {} bytes)",
876                        i, stats.allocated_bytes
877                    );
878                }
879            }
880
881            // Final verification: arena should be empty
882            let final_stats = arena::arena_stats();
883            assert_eq!(
884                final_stats.allocated_bytes, 0,
885                "Arena leaked memory after {} iterations ({} bytes)",
886                iterations, final_stats.allocated_bytes
887            );
888
889            println!(
890                "✓ Memory leak test passed: {} iterations with no growth",
891                iterations
892            );
893        }
894    }
895
896    #[test]
897    fn test_parse_stack_size_valid() {
898        assert_eq!(parse_stack_size(Some("2097152".to_string())), 2097152);
899        assert_eq!(parse_stack_size(Some("1".to_string())), 1);
900        assert_eq!(parse_stack_size(Some("999999999".to_string())), 999999999);
901    }
902
903    #[test]
904    fn test_parse_stack_size_none() {
905        assert_eq!(parse_stack_size(None), DEFAULT_STACK_SIZE);
906    }
907
908    #[test]
909    fn test_parse_stack_size_zero() {
910        // Zero should fall back to default (with warning printed to stderr)
911        assert_eq!(parse_stack_size(Some("0".to_string())), DEFAULT_STACK_SIZE);
912    }
913
914    #[test]
915    fn test_parse_stack_size_invalid() {
916        // Non-numeric should fall back to default (with warning printed to stderr)
917        assert_eq!(
918            parse_stack_size(Some("invalid".to_string())),
919            DEFAULT_STACK_SIZE
920        );
921        assert_eq!(
922            parse_stack_size(Some("-100".to_string())),
923            DEFAULT_STACK_SIZE
924        );
925        assert_eq!(parse_stack_size(Some("".to_string())), DEFAULT_STACK_SIZE);
926        assert_eq!(
927            parse_stack_size(Some("1.5".to_string())),
928            DEFAULT_STACK_SIZE
929        );
930    }
931
932    #[test]
933    #[cfg(feature = "diagnostics")]
934    fn test_strand_registry_basic() {
935        let registry = StrandRegistry::new(10);
936
937        // Register some strands
938        assert_eq!(registry.register(1), Some(0)); // First slot
939        assert_eq!(registry.register(2), Some(1)); // Second slot
940        assert_eq!(registry.register(3), Some(2)); // Third slot
941
942        // Verify active strands
943        let active: Vec<_> = registry.active_strands().collect();
944        assert_eq!(active.len(), 3);
945
946        // Unregister one
947        assert!(registry.unregister(2));
948        let active: Vec<_> = registry.active_strands().collect();
949        assert_eq!(active.len(), 2);
950
951        // Unregister non-existent should return false
952        assert!(!registry.unregister(999));
953    }
954
955    #[test]
956    #[cfg(feature = "diagnostics")]
957    fn test_strand_registry_overflow() {
958        let registry = StrandRegistry::new(3); // Small capacity
959
960        // Fill it up
961        assert!(registry.register(1).is_some());
962        assert!(registry.register(2).is_some());
963        assert!(registry.register(3).is_some());
964
965        // Next should overflow
966        assert!(registry.register(4).is_none());
967        assert_eq!(registry.overflow_count.load(Ordering::Relaxed), 1);
968
969        // Another overflow
970        assert!(registry.register(5).is_none());
971        assert_eq!(registry.overflow_count.load(Ordering::Relaxed), 2);
972    }
973
974    #[test]
975    #[cfg(feature = "diagnostics")]
976    fn test_strand_registry_slot_reuse() {
977        let registry = StrandRegistry::new(3);
978
979        // Fill it up
980        registry.register(1);
981        registry.register(2);
982        registry.register(3);
983
984        // Unregister middle one
985        registry.unregister(2);
986
987        // New registration should reuse the slot
988        assert!(registry.register(4).is_some());
989        assert_eq!(registry.active_strands().count(), 3);
990    }
991
992    #[test]
993    #[cfg(feature = "diagnostics")]
994    fn test_strand_registry_concurrent_stress() {
995        use std::sync::Arc;
996        use std::thread;
997
998        let registry = Arc::new(StrandRegistry::new(50)); // Moderate capacity
999
1000        let handles: Vec<_> = (0..100)
1001            .map(|i| {
1002                let reg = Arc::clone(&registry);
1003                thread::spawn(move || {
1004                    let id = (i + 1) as u64;
1005                    // Register
1006                    let _ = reg.register(id);
1007                    // Brief work
1008                    thread::yield_now();
1009                    // Unregister
1010                    reg.unregister(id);
1011                })
1012            })
1013            .collect();
1014
1015        for h in handles {
1016            h.join().unwrap();
1017        }
1018
1019        // All slots should be free after all threads complete
1020        assert_eq!(registry.active_strands().count(), 0);
1021    }
1022
1023    #[test]
1024    fn test_strand_lifecycle_counters() {
1025        unsafe {
1026            // Reset counters for isolation (not perfect but helps)
1027            let initial_spawned = TOTAL_SPAWNED.load(Ordering::Relaxed);
1028            let initial_completed = TOTAL_COMPLETED.load(Ordering::Relaxed);
1029
1030            static COUNTER: AtomicU32 = AtomicU32::new(0);
1031
1032            extern "C" fn simple_work(_stack: Stack) -> Stack {
1033                COUNTER.fetch_add(1, Ordering::SeqCst);
1034                std::ptr::null_mut()
1035            }
1036
1037            COUNTER.store(0, Ordering::SeqCst);
1038
1039            // Spawn some strands
1040            for _ in 0..10 {
1041                strand_spawn(simple_work, std::ptr::null_mut());
1042            }
1043
1044            wait_all_strands();
1045
1046            // Verify counters incremented
1047            let final_spawned = TOTAL_SPAWNED.load(Ordering::Relaxed);
1048            let final_completed = TOTAL_COMPLETED.load(Ordering::Relaxed);
1049
1050            assert!(
1051                final_spawned >= initial_spawned + 10,
1052                "TOTAL_SPAWNED should have increased by at least 10"
1053            );
1054            assert!(
1055                final_completed >= initial_completed + 10,
1056                "TOTAL_COMPLETED should have increased by at least 10"
1057            );
1058            assert_eq!(COUNTER.load(Ordering::SeqCst), 10);
1059        }
1060    }
1061}