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
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Worker thread abstraction

use std::panic;
use std::thread;
use std::thread::JoinHandle;
use std::thread::Thread;

use crate::Error;
use crate::Event;

/// Wrapper object for creating a worker thread that can be stopped by signaling an event.
pub struct WorkerThread<T: Send + 'static> {
    worker: Option<(Event, JoinHandle<T>)>,
}

impl<T: Send + 'static> WorkerThread<T> {
    /// Starts a worker thread named `thread_name` running the `thread_func` function.
    ///
    /// The `thread_func` implementation must monitor the provided `Event` and return from the
    /// thread when it is signaled.
    ///
    /// Call [`stop()`](Self::stop) to stop the thread.
    pub fn start<F>(thread_name: impl Into<String>, thread_func: F) -> Self
    where
        F: FnOnce(Event) -> T + Send + 'static,
    {
        let stop_event = Event::new().expect("Event::new() failed");
        let thread_stop_event = stop_event.try_clone().expect("Event::try_clone() failed");

        let thread_handle = thread::Builder::new()
            .name(thread_name.into())
            .spawn(move || thread_func(thread_stop_event))
            .expect("thread spawn failed");

        WorkerThread {
            worker: Some((stop_event, thread_handle)),
        }
    }

    /// Stops the worker thread.
    ///
    /// Returns the value returned by the function running in the thread.
    pub fn stop(mut self) -> T {
        // The only time the internal `Option` should be `None` is in a `drop` after `stop`, so this
        // `expect()` should never fail.
        self.stop_internal().expect("invalid worker state")
    }

    // `stop_internal` accepts a reference so it can be called from `drop`.
    #[doc(hidden)]
    fn stop_internal(&mut self) -> Option<T> {
        self.worker.take().map(|(stop_event, thread_handle)| {
            // There is nothing the caller can do to handle `stop_event.signal()` failure, and we
            // don't want to leave the thread running, so panic in that case.
            stop_event
                .signal()
                .expect("WorkerThread stop event signal failed");

            match thread_handle.join() {
                Ok(v) => v,
                Err(e) => panic::resume_unwind(e),
            }
        })
    }

    /// Signal thread's stop event. Unlike stop, the function doesn't wait
    /// on joining the thread.
    /// The function can be called multiple times.
    /// Calling `stop` or `drop` will internally signal the stop event again
    /// and join the thread.
    pub fn signal(&mut self) -> Result<(), Error> {
        if let Some((event, _)) = &mut self.worker {
            event.signal()
        } else {
            Ok(())
        }
    }

    /// Returns a handle to the running thread.
    pub fn thread(&self) -> &Thread {
        // The only time the internal `Option` should be `None` is in a `drop` after `stop`, so this
        // `unwrap()` should never fail.
        self.worker.as_ref().unwrap().1.thread()
    }
}

impl<T: Send + 'static> Drop for WorkerThread<T> {
    /// Stops the thread if the `WorkerThread` is dropped without calling [`stop()`](Self::stop).
    fn drop(&mut self) {
        let _ = self.stop_internal();
    }
}