foundation_core 0.0.3

Central crate for all foundation crates for the ewe-platform project
Documentation
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#![allow(clippy::type_complexity)]

use std::sync::{Arc, Mutex};

use crate::valtron::{
    split_thread_count, task::TaskIterator, BackgroundJobRegistry, ExecutionAction, GenericResult,
    TaskReadyResolver, TaskStatusMapper,
};

use crate::synca::{LockSignal, OnSignal};
pub use crate::valtron::multi::threads::*;
use crate::valtron::SharedTaskQueue;

use crate::valtron::executors::background::DEFAULT_BG_YIELD_DURATION;

/// Global registry - stores the `ThreadRegistry` Arc.
/// Uses Mutex<Option> to allow resetting between tests.
static REGISTRY: Mutex<Option<Arc<ThreadRegistry>>> = Mutex::new(None);

/// Global background job registry.
/// Uses Mutex<Option> to allow resetting between tests.
static BG_REGISTRY: Mutex<Option<Arc<BackgroundJobRegistry>>> = Mutex::new(None);

/// Handle for spawning tasks into the shared queue.
///
/// This is the new return type of `get_pool()`. It provides the same
/// spawn API surface as the old `&ThreadPool` by constructing
/// `ThreadPoolTaskBuilder` directly with the shared queue and latch.
#[derive(Clone)]
pub struct LocalPoolHandle {
    shared_tasks: SharedTaskQueue,
    latch: Arc<LockSignal>,
    kill_signal: Arc<OnSignal>,
    yielders: SharedThreadYielders,
}

impl LocalPoolHandle {
    fn new(registry: &Arc<ThreadRegistry>) -> Self {
        Self {
            shared_tasks: registry.shared_tasks(),
            latch: registry.latch(),
            kill_signal: registry.kill_signal(),
            yielders: registry.yielders(),
        }
    }

    /// Create a task builder for scheduling work.
    #[must_use]
    pub fn spawn<Task, Action>(
        &self,
    ) -> ThreadPoolTaskBuilder<
        Task::Ready,
        Task::Pending,
        Task::Spawner,
        Box<dyn TaskStatusMapper<Task::Ready, Task::Pending, Task::Spawner> + Send + 'static>,
        Box<dyn TaskReadyResolver<Task::Spawner, Task::Ready, Task::Pending> + Send + 'static>,
        Task,
    >
    where
        Task::Ready: Send + 'static,
        Task::Pending: Send + 'static,
        Task: TaskIterator<Spawner = Action> + Send + 'static,
        Action: ExecutionAction + Send + 'static,
    {
        ThreadPoolTaskBuilder::new(self.shared_tasks.clone(), self.latch.clone())
            .with_yielders(self.yielders.clone())
    }

    /// Create a task builder with explicit Mapper and Resolver types.
    #[must_use]
    pub fn spawn2<Task, Action, Mapper, Resolver>(
        &self,
    ) -> ThreadPoolTaskBuilder<Task::Ready, Task::Pending, Action, Mapper, Resolver, Task>
    where
        Task::Ready: Send + 'static,
        Task::Pending: Send + 'static,
        Task: TaskIterator<Spawner = Action> + Send + 'static,
        Action: ExecutionAction + Send + 'static,
        Mapper: TaskStatusMapper<Task::Ready, Task::Pending, Action> + Send + 'static,
        Resolver: TaskReadyResolver<Action, Task::Ready, Task::Pending> + Send + 'static,
    {
        ThreadPoolTaskBuilder::new(self.shared_tasks.clone(), self.latch.clone())
            .with_yielders(self.yielders.clone())
    }

    /// Signal all threads to die.
    pub fn kill(&self) {
        self.kill_signal.turn_on();
        self.latch.signal_all();
    }
}

/// [`get_pool`] returns a handle for spawning tasks.
///
/// # Panics
///
/// Panics if the thread pool has not been initialized (ensure to call `block_on` or
/// `initialize_pool` first), or if the internal registry mutex is poisoned.
pub fn get_pool() -> LocalPoolHandle {
    let registry =
        REGISTRY.lock().unwrap().clone().expect(
            "Thread pool not initialized, ensure to call block_on or initialize_pool first",
        );
    LocalPoolHandle::new(&registry)
}

/// [`block_on`] instantiates the thread registry, spawns worker threads,
/// runs the setup function, then blocks until all tasks complete.
///
/// Returns a `PoolGuard` - when dropped, kills all threads and blocks
/// until they're cleaned up. No Ctrl-C handler is registered automatically.
pub fn block_on<F>(seed_from_rng: u64, thread_num: Option<usize>, setup: F) -> PoolGuard
where
    F: FnOnce(LocalPoolHandle),
{
    let guard = initialize_pool(seed_from_rng, thread_num);
    let handle = get_pool();
    setup(handle);
    tracing::debug!("Initialize and call WaitGroup::wait");
    guard.waitgroup().wait();
    guard
}

/// `initialize_pool` creates a `ThreadRegistry` and `BackgroundJobRegistry`,
/// splitting the available threads between them.
///
/// Returns a `PoolGuard` — when dropped, signals all threads to die,
/// waits for them to complete via `WaitGroup`, joins all handles,
/// and clears the global registries to prevent test interference.
///
/// The caller is responsible for signal handling via the `PoolGuard`.
pub fn initialize_pool(seed_for_rng: u64, user_thread_num: Option<usize>) -> PoolGuard {
    let thread_num = match user_thread_num {
        None => get_allocatable_thread_count(),
        // The multi pool splits threads into background + task workers and needs
        // ≥ 2 TASK workers (`ThreadRegistry`), which means ≥ 3 total once the
        // background worker is carved off. Clamp a smaller request up rather than
        // panic — callers (and the `#[valtron]` macros) pass a plain count.
        Some(num) => num.max(3),
    };

    let (task_threads, bg_threads) = split_thread_count(thread_num);

    tracing::debug!(
        "Splitting {thread_num} threads: {task_threads} for tasks, {bg_threads} for background jobs"
    );

    let registry = Arc::new(ThreadRegistry::with_seed_and_threads(
        seed_for_rng,
        task_threads,
    ));

    // Spawn task worker threads
    for _ in 0..task_threads {
        registry
            .spawn_worker()
            .expect("Failed to spawn worker thread");
    }

    // Create background job registry sharing the kill signal
    let bg_registry = BackgroundJobRegistry::new(
        bg_threads,
        registry.kill_signal(),
        DEFAULT_BG_YIELD_DURATION,
    );

    *REGISTRY.lock().unwrap() = Some(registry.clone());
    *BG_REGISTRY.lock().unwrap() = Some(bg_registry.clone());

    PoolGuard::with_bg_registry(registry, bg_registry)
}

/// Submit a blocking closure for execution on the background job pool.
///
/// WHY: Provides managed background thread execution without spawning new OS threads
/// WHAT: Pushes the closure onto the `BackgroundJobRegistry` queue
/// HOW: Acquires the global `BG_REGISTRY` and calls `submit()`
///
/// # Errors
///
/// Returns an error if the background registry is not initialized or the queue is closed.
///
/// # Panics
///
/// Panics if the background job pool has not been initialized.
pub fn run_background_job(job: impl FnOnce() + Send + 'static) -> GenericResult<()> {
    let bg_registry = BG_REGISTRY
        .lock()
        .unwrap()
        .clone()
        .expect("Background job pool not initialized, ensure to call initialize_pool first");
    bg_registry.submit(job)
}

/// [`spawn`] provides a builder which allows you to build out
/// the underlying tasks to be scheduled into the global queue.
///
/// Uses the global registry's shared queue and latch.
#[must_use]
pub fn spawn<Task, Action>() -> ThreadPoolTaskBuilder<
    Task::Ready,
    Task::Pending,
    Task::Spawner,
    Box<dyn TaskStatusMapper<Task::Ready, Task::Pending, Task::Spawner> + Send + 'static>,
    Box<dyn TaskReadyResolver<Task::Spawner, Task::Ready, Task::Pending> + Send + 'static>,
    Task,
>
where
    Task::Ready: Send + 'static,
    Task::Pending: Send + 'static,
    Task: TaskIterator<Spawner = Action> + Send + 'static,
    Action: ExecutionAction + Send + 'static,
{
    get_pool().spawn::<Task, Action>()
}

/// [`spawn2`] provides a builder which allows you to build out
/// the underlying tasks with explicit Mapper and Resolver types.
#[must_use]
pub fn spawn2<Task, Action, Mapper, Resolver>(
) -> ThreadPoolTaskBuilder<Task::Ready, Task::Pending, Action, Mapper, Resolver, Task>
where
    Task::Ready: Send + 'static,
    Task::Pending: Send + 'static,
    Task: TaskIterator<Spawner = Action> + Send + 'static,
    Action: ExecutionAction + Send + 'static,
    Mapper: TaskStatusMapper<Task::Ready, Task::Pending, Action> + Send + 'static,
    Resolver: TaskReadyResolver<Action, Task::Ready, Task::Pending> + Send + 'static,
{
    get_pool().spawn2::<Task, Action, Mapper, Resolver>()
}

#[cfg(test)]
mod multi_threaded_tests {
    use core::time;
    use std::{
        sync::{Arc, Mutex},
        thread,
    };

    use rand::RngCore;
    use serial_test::serial;
    use tracing_test::traced_test;

    use super::{block_on, get_pool, initialize_pool};
    use crate::{
        synca::mpp,
        valtron::{FnReady, NoSpawner, TaskIterator, TaskStatus},
    };

    struct DCounter(usize, Arc<Mutex<Vec<usize>>>);

    impl DCounter {
        pub fn new(val: usize, list: Arc<Mutex<Vec<usize>>>) -> Self {
            Self(val, list)
        }
    }

    impl TaskIterator for DCounter {
        type Pending = ();

        type Ready = usize;

        type Spawner = NoSpawner;

        fn next_status(
            &mut self,
        ) -> Option<crate::valtron::TaskStatus<Self::Ready, Self::Pending, Self::Spawner>> {
            let mut items = self.1.lock().unwrap();
            let item_size = items.len();

            if item_size == self.0 {
                return None;
            }

            items.push(item_size);
            let new_len = items.len();

            Some(crate::valtron::TaskStatus::Ready(new_len))
        }
    }

    struct Counter(usize, Arc<Mutex<Vec<usize>>>, mpp::Sender<()>);

    impl Counter {
        pub fn new(val: usize, list: Arc<Mutex<Vec<usize>>>) -> (Self, mpp::Receiver<()>) {
            let (sender, receiver) = mpp::bounded(10);
            (Counter(val, list, sender), receiver)
        }
    }

    impl TaskIterator for Counter {
        type Pending = ();

        type Ready = usize;

        type Spawner = NoSpawner;

        fn next_status(
            &mut self,
        ) -> Option<crate::valtron::TaskStatus<Self::Ready, Self::Pending, Self::Spawner>> {
            tracing::debug!("Counter Task is running");

            let result = {
                let mut items = self.1.lock().unwrap();
                let item_size = items.len();

                if item_size == self.0 {
                    None // signal "done" — will send after lock release
                } else {
                    items.push(item_size);
                    Some(crate::valtron::TaskStatus::Ready(items.len()))
                }
            }; // lock released here

            match result {
                None => {
                    tracing::debug!("Sending signal with sender");
                    self.2.send(()).expect("send signal");
                    None
                }
                some => some,
            }
        }
    }

    #[derive(Default)]
    struct PanicCounter;

    impl TaskIterator for PanicCounter {
        type Pending = ();

        type Ready = usize;

        type Spawner = NoSpawner;

        fn next_status(
            &mut self,
        ) -> Option<crate::valtron::TaskStatus<Self::Ready, Self::Pending, Self::Spawner>> {
            tracing::debug!("PanicCounter Task is running");
            panic!("Bad stuff");
        }
    }

    mod blocked_on_execution {
        use super::*;

        #[test]
        #[serial]
        #[traced_test]
        fn can_finish_even_when_task_panics() {
            let seed = rand::rng().next_u64();

            let handler_kill = thread::spawn(move || {
                tracing::debug!("Waiting for kill signal");
                thread::sleep(time::Duration::from_secs(5));
                tracing::debug!("Got kill signal");
                get_pool().kill();
                tracing::debug!("Closing thread");
            });

            let (task_sent_sender, task_sent_receiver) = mpp::bounded(1);
            let _guard = block_on(seed, None, |pool| {
                pool.spawn()
                    .with_task(PanicCounter)
                    .with_resolver(Box::new(FnReady::new(|item, _| {
                        tracing::info!("Received next: {item:?}");
                    })))
                    .schedule()
                    .expect("should deliver task");
                task_sent_sender.send(()).expect("deliver message");
            });

            task_sent_receiver
                .recv_timeout(time::Duration::from_secs(2))
                .expect("should have spawned task");

            tracing::info!("Wait for thread to die");
            handler_kill.join().expect("should finish");
            tracing::info!("Wait for thread to die");
        }

        #[test]
        #[serial]
        #[traced_test]
        fn can_queue_and_complete_task() {
            let seed = rand::rng().next_u64();

            let shared_list = Arc::new(Mutex::new(vec![]));
            let (counter, receiver) = Counter::new(5, shared_list.clone());

            let handler_kill = thread::spawn(move || {
                tracing::debug!("Waiting for kill signal");
                receiver
                    .recv_timeout(time::Duration::from_secs(2))
                    .expect("receive signal");
                tracing::debug!("Got kill signal");
                get_pool().kill();
                tracing::debug!("Closing thread");
            });

            let (task_sent_sender, task_sent_receiver) = mpp::bounded(1);
            let _guard = block_on(seed, None, |pool| {
                tracing::debug!("Spawning new task into pool");
                pool.spawn()
                    .with_task(counter)
                    .with_resolver(Box::new(FnReady::new(|item, _| {
                        tracing::info!("Received next: {item:?}");
                    })))
                    .schedule()
                    .expect("should deliver task");
                tracing::debug!("Task spawned");
                task_sent_sender.send(()).expect("deliver message");
            });

            task_sent_receiver
                .recv_timeout(time::Duration::from_secs(2))
                .expect("should have spawned task");

            handler_kill.join().expect("should finish");

            assert_eq!(shared_list.lock().unwrap().clone(), vec![0, 1, 2, 3, 4]);
        }
    }

    mod inline_execution {
        use crate::valtron::NotificationItem;
        use crate::valtron::Stream;

        use super::*;

        #[test]
        #[serial]
        #[traced_test]
        fn can_queue_and_complete_task_with_iterator() {
            let seed = rand::rng().next_u64();

            let shared_list = Arc::new(Mutex::new(Vec::new()));
            let counter = DCounter::new(5, shared_list.clone());

            let _guard = initialize_pool(seed, None);

            let iter = get_pool()
                .spawn()
                .with_task(counter)
                .schedule_iter(std::time::Duration::from_nanos(50))
                .expect("should deliver task");

            let complete: Vec<usize> = iter
                .map(|item| match item {
                    NotificationItem::Ready(inner) => match inner {
                        TaskStatus::Ready(value) => Some(value),
                        _ => None,
                    },
                    NotificationItem::None => None,
                })
                .take_while(|t: &Option<usize>| t.is_some())
                .map(|t: Option<usize>| t.unwrap())
                .collect();

            assert_eq!(complete, vec![1, 2, 3, 4, 5]);
        }

        #[test]
        #[serial]
        #[traced_test]
        fn can_queue_use_stream_iterator_from_task_iterator() {
            let seed = rand::rng().next_u64();

            let shared_list = Arc::new(Mutex::new(Vec::new()));
            let counter = DCounter::new(5, shared_list.clone());

            let _guard = initialize_pool(seed, None);

            let iter = get_pool()
                .spawn()
                .with_task(counter)
                .stream_iter_with_config(
                    std::time::Duration::from_nanos(20), // wait_cycle (used as park_duration)
                    10,                                  // max_turns
                )
                .expect("should deliver task");

            let complete: Vec<usize> = iter
                .map(|item| match item {
                    Stream::Next(value) => Some(value),
                    _ => None,
                })
                .take_while(|t| t.is_some())
                .map(|t| t.unwrap())
                .collect();

            assert_eq!(complete, vec![1, 2, 3, 4, 5]);
        }
    }
}