revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! Worker pool implementation
//!
//! Uses Condvar for efficient thread signaling instead of busy-polling.
//! Uses BinaryHeap for O(log n) priority queue operations.

use super::{Priority, WorkerConfig, WorkerHandle};
use std::collections::BinaryHeap;
use std::sync::{Arc, Condvar, Mutex};
use std::thread::{self, JoinHandle};

// Use lock utilities for consistent poison handling
use crate::utils::lock as lock_util;

/// Shared state between pool and workers
struct SharedState {
    /// Task queue
    queue: TaskQueue,
    /// Shutdown flag
    shutdown: bool,
}

/// A pool of worker threads for background tasks
pub struct WorkerPool {
    /// Worker threads
    workers: Vec<Worker>,
    /// Shared state (queue + shutdown)
    state: Arc<(Mutex<SharedState>, Condvar)>,
    /// Configuration
    config: WorkerConfig,
}

impl WorkerPool {
    /// Create a new worker pool with default config
    pub fn new(threads: usize) -> Self {
        Self::with_config(WorkerConfig::with_threads(threads))
    }

    /// Create with custom config
    pub fn with_config(config: WorkerConfig) -> Self {
        let shared_state = SharedState {
            queue: TaskQueue::new(config.queue_capacity),
            shutdown: false,
        };
        let state = Arc::new((Mutex::new(shared_state), Condvar::new()));
        let mut workers = Vec::with_capacity(config.threads);

        for id in 0..config.threads {
            workers.push(Worker::new(id, state.clone()));
        }

        Self {
            workers,
            state,
            config,
        }
    }

    /// Spawn a blocking task
    pub fn spawn_blocking<F, T>(&self, f: F) -> WorkerHandle<T>
    where
        F: FnOnce() -> T + Send + 'static,
        T: Send + 'static,
    {
        WorkerHandle::spawn_blocking(f)
    }

    /// Submit a task to the pool's queue
    pub fn submit<F>(&self, f: F) -> bool
    where
        F: FnOnce() + Send + 'static,
    {
        self.submit_with_priority(f, Priority::Normal)
    }

    /// Submit a task with priority
    pub fn submit_with_priority<F>(&self, f: F, priority: Priority) -> bool
    where
        F: FnOnce() + Send + 'static,
    {
        let (lock, cvar) = &*self.state;
        let mut state = lock_util::lock_or_recover(lock);
        if state.shutdown {
            return false;
        }

        // Get and increment sequence number for FIFO ordering
        let seq = state.queue.next_seq;
        state.queue.next_seq = state.queue.next_seq.wrapping_add(1);

        let task = QueuedTask::new(f, priority, seq);
        if state.queue.push(task) {
            // Wake one waiting worker
            cvar.notify_one();
            return true;
        }
        false
    }

    /// Get number of active workers
    pub fn active_workers(&self) -> usize {
        self.workers.iter().filter(|w| w.is_active()).count()
    }

    /// Get queue length
    pub fn queue_len(&self) -> usize {
        let (lock, _) = &*self.state;
        lock_util::lock_or_recover(lock).queue.len()
    }

    /// Get thread count
    pub fn thread_count(&self) -> usize {
        self.config.threads
    }

    /// Shutdown the pool gracefully
    pub fn shutdown(&self) {
        let (lock, cvar) = &*self.state;
        let mut state = lock_util::lock_or_recover(lock);
        state.shutdown = true;
        // Wake all workers so they can exit
        cvar.notify_all();
    }

    /// Check if pool is shutdown
    pub fn is_shutdown(&self) -> bool {
        let (lock, _) = &*self.state;
        lock_util::lock_or_recover(lock).shutdown
    }
}

impl Default for WorkerPool {
    fn default() -> Self {
        Self::with_config(WorkerConfig::default())
    }
}

impl Drop for WorkerPool {
    fn drop(&mut self) {
        self.shutdown();

        // Join all worker threads for clean shutdown
        for worker in &mut self.workers {
            worker.join();
        }
    }
}

/// A single worker thread
pub struct Worker {
    /// Worker ID
    id: usize,
    /// Thread handle for joining on shutdown
    thread: Option<JoinHandle<()>>,
    /// Active flag
    active: Arc<Mutex<bool>>,
}

impl Worker {
    /// Create a new worker
    fn new(id: usize, state: Arc<(Mutex<SharedState>, Condvar)>) -> Self {
        let active = Arc::new(Mutex::new(true));
        let active_clone = active.clone();

        let thread = thread::Builder::new()
            .name(format!("revue-worker-{}", id))
            .spawn(move || {
                let (lock, cvar) = &*state;

                loop {
                    // Wait for a task or shutdown signal
                    let task = {
                        let mut state = lock_util::lock_or_recover(lock);

                        // Wait while queue is empty and not shutting down
                        while state.queue.is_empty() && !state.shutdown {
                            // Condvar::wait can also return poisoned, handle it
                            state = cvar.wait(state).unwrap_or_else(|poisoned| {
                                log_warn!("Condvar wait was poisoned, recovering");
                                poisoned.into_inner()
                            });
                        }

                        // Check if we should exit
                        if state.shutdown && state.queue.is_empty() {
                            break;
                        }

                        // Pop a task from the queue
                        state.queue.pop()
                    };

                    // Execute the task outside the lock
                    if let Some(queued_task) = task {
                        (queued_task.task)();
                    }
                }

                // Mark as inactive
                if let Ok(mut active) = active_clone.lock() {
                    *active = false;
                }
            })
            .ok();

        Self { id, thread, active }
    }

    /// Get worker ID
    pub fn id(&self) -> usize {
        self.id
    }

    /// Check if worker is active
    pub fn is_active(&self) -> bool {
        *lock_util::lock_or_recover(&self.active)
    }

    /// Join the worker thread, waiting for it to finish
    pub fn join(&mut self) {
        if let Some(thread) = self.thread.take() {
            let _ = thread.join();
        }
    }
}

/// Task queue for worker pool
struct TaskQueue {
    /// Tasks waiting to be executed (using BinaryHeap for O(log n) operations)
    tasks: BinaryHeap<QueuedTask>,
    /// Maximum capacity
    capacity: usize,
    /// Next sequence number (for FIFO ordering within same priority)
    next_seq: u64,
}

impl TaskQueue {
    fn new(capacity: usize) -> Self {
        Self {
            tasks: BinaryHeap::with_capacity(capacity),
            capacity,
            next_seq: 0,
        }
    }

    fn len(&self) -> usize {
        self.tasks.len()
    }

    fn is_empty(&self) -> bool {
        self.tasks.is_empty()
    }

    fn push(&mut self, task: QueuedTask) -> bool {
        if self.tasks.len() >= self.capacity {
            return false;
        }

        self.tasks.push(task);
        true
    }

    fn pop(&mut self) -> Option<QueuedTask> {
        self.tasks.pop()
    }
}

/// A queued task with priority and sequence number for FIFO ordering
struct QueuedTask {
    /// Task to execute
    task: Box<dyn FnOnce() + Send + 'static>,
    /// Priority (higher values = higher priority)
    priority: Priority,
    /// Sequence number for FIFO ordering within same priority
    seq: u64,
}

impl QueuedTask {
    fn new<F>(task: F, priority: Priority, seq: u64) -> Self
    where
        F: FnOnce() + Send + 'static,
    {
        Self {
            task: Box::new(task),
            priority,
            seq,
        }
    }
}

// Implement Ord for BinaryHeap
// BinaryHeap is a max-heap, so we want higher priority tasks to compare as "greater"
// For same priority, lower sequence number (earlier) should come first (FIFO)
impl PartialEq for QueuedTask {
    fn eq(&self, other: &Self) -> bool {
        self.priority == other.priority && self.seq == other.seq
    }
}

impl Eq for QueuedTask {}

impl PartialOrd for QueuedTask {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for QueuedTask {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // First compare by priority (higher priority = greater)
        match self.priority.cmp(&other.priority) {
            std::cmp::Ordering::Equal => {
                // For same priority, lower sequence number (earlier) should come first
                // Reverse the seq comparison so lower seq = "greater" in max-heap
                other.seq.cmp(&self.seq)
            }
            other => other,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[test]
    fn test_worker_pool_new() {
        let pool = WorkerPool::new(4);
        assert_eq!(pool.thread_count(), 4);
    }

    #[test]
    fn test_worker_pool_default() {
        let pool = WorkerPool::default();
        assert!(pool.thread_count() >= 1);
    }

    #[test]
    fn test_worker_pool_shutdown() {
        let pool = WorkerPool::new(2);
        assert!(!pool.is_shutdown());
        pool.shutdown();
        assert!(pool.is_shutdown());
    }

    #[test]
    fn test_worker_pool_submit() {
        let pool = WorkerPool::new(2);
        let counter = Arc::new(AtomicUsize::new(0));

        // Submit tasks
        for _ in 0..10 {
            let counter = counter.clone();
            assert!(pool.submit(move || {
                counter.fetch_add(1, Ordering::SeqCst);
            }));
        }

        // Wait for tasks to complete
        thread::sleep(std::time::Duration::from_millis(100));

        assert_eq!(counter.load(Ordering::SeqCst), 10);
        pool.shutdown();
    }

    #[test]
    fn test_worker_pool_submit_after_shutdown() {
        let pool = WorkerPool::new(1);
        pool.shutdown();

        // Should fail to submit after shutdown
        assert!(!pool.submit(|| {}));
    }

    #[test]
    fn test_worker_pool_priority() {
        use std::sync::atomic::{AtomicBool, Ordering};

        let pool = WorkerPool::new(1);
        let order = Arc::new(Mutex::new(Vec::new()));

        // Use a barrier to hold the worker while we queue both tasks
        let barrier = Arc::new(AtomicBool::new(false));
        let barrier_clone = barrier.clone();

        // Submit a blocking task first to hold the worker
        pool.submit(move || {
            while !barrier_clone.load(Ordering::SeqCst) {
                thread::sleep(std::time::Duration::from_millis(1));
            }
        });

        // Give the worker time to pick up the blocking task
        thread::sleep(std::time::Duration::from_millis(10));

        // Now submit low and high priority tasks - they'll queue up
        let order1 = order.clone();
        pool.submit_with_priority(
            move || {
                let mut order = lock_util::lock_or_recover(&order1);
                order.push("low");
            },
            Priority::Low,
        );

        let order2 = order.clone();
        pool.submit_with_priority(
            move || {
                let mut order = lock_util::lock_or_recover(&order2);
                order.push("high");
            },
            Priority::High,
        );

        // Release the barrier - worker will process queued tasks by priority
        barrier.store(true, Ordering::SeqCst);

        // Wait for completion
        thread::sleep(std::time::Duration::from_millis(100));
        pool.shutdown();

        let result = lock_util::lock_or_recover(&order);
        // High priority should be processed first
        assert_eq!(result.len(), 2);
        assert_eq!(result[0], "high");
        assert_eq!(result[1], "low");
    }
}