2022-02-01 10:01:43 +11:00
|
|
|
//! An event loop implementation for Linux. APIs on Linux are generally thread safe, so the context
|
2022-02-01 05:45:11 +11:00
|
|
|
//! of a main thread does not exist there. Because of that, this mostly just serves as a way to
|
|
|
|
//! delegate expensive processing to another thread.
|
|
|
|
|
|
|
|
use crossbeam::channel;
|
2022-02-02 01:31:16 +11:00
|
|
|
use std::sync::Weak;
|
2022-02-01 05:45:11 +11:00
|
|
|
use std::thread::{self, JoinHandle, ThreadId};
|
|
|
|
|
|
|
|
use super::{EventLoop, MainThreadExecutor};
|
2022-02-02 01:04:36 +11:00
|
|
|
use crate::nih_log;
|
2022-02-01 05:45:11 +11:00
|
|
|
|
|
|
|
/// See [super::EventLoop].
|
|
|
|
pub(crate) struct LinuxEventLoop<T, E> {
|
|
|
|
/// The thing that ends up executing these tasks. The tasks are usually executed from the worker
|
|
|
|
/// thread, but if the current thread is the main thread then the task cna also be executed
|
|
|
|
/// directly.
|
2022-02-02 01:31:16 +11:00
|
|
|
executor: Weak<E>,
|
2022-02-01 05:45:11 +11:00
|
|
|
|
|
|
|
/// The ID of the main thread. In practice this is the ID of the thread that created this task
|
|
|
|
/// queue.
|
|
|
|
main_thread_id: ThreadId,
|
|
|
|
|
2022-02-05 22:56:03 +11:00
|
|
|
/// A thread that act as our worker thread. When [Self::do_maybe_async()] is called, this thread will be
|
2022-02-01 05:45:11 +11:00
|
|
|
/// woken up to execute the task on the executor. This is wrapped in an `Option` so the thread
|
|
|
|
/// can be taken out of it and joined when this struct gets dropped.
|
|
|
|
worker_thread: Option<JoinHandle<()>>,
|
|
|
|
/// A channel for waking up the worker thread and having it perform one of the tasks from
|
|
|
|
/// [Message].
|
|
|
|
worker_thread_channel: channel::Sender<Message<T>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A message for communicating with the worker thread.
|
|
|
|
enum Message<T> {
|
|
|
|
/// A new task for the event loop to execute.
|
|
|
|
Task(T),
|
|
|
|
/// Shut down the worker thread.
|
|
|
|
Shutdown,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E> EventLoop<T, E> for LinuxEventLoop<T, E>
|
|
|
|
where
|
2022-02-06 02:09:59 +11:00
|
|
|
T: Send + 'static,
|
|
|
|
E: MainThreadExecutor<T> + 'static,
|
2022-02-01 05:45:11 +11:00
|
|
|
{
|
2022-02-02 01:31:16 +11:00
|
|
|
fn new_and_spawn(executor: Weak<E>) -> Self {
|
2022-02-01 05:45:11 +11:00
|
|
|
let (sender, receiver) = channel::bounded(super::TASK_QUEUE_CAPACITY);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
executor: executor.clone(),
|
|
|
|
main_thread_id: thread::current().id(),
|
|
|
|
// With our drop implementation we guarentee that this thread never outlives this struct
|
2022-02-06 02:09:59 +11:00
|
|
|
worker_thread: Some(
|
2022-02-01 05:45:11 +11:00
|
|
|
thread::Builder::new()
|
|
|
|
.name(String::from("worker"))
|
2022-02-06 02:09:59 +11:00
|
|
|
.spawn(move || worker_thread(receiver, executor))
|
|
|
|
.expect("Could not spawn worker thread"),
|
|
|
|
),
|
2022-02-01 05:45:11 +11:00
|
|
|
worker_thread_channel: sender,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_maybe_async(&self, task: T) -> bool {
|
|
|
|
if self.is_main_thread() {
|
2022-02-02 01:31:16 +11:00
|
|
|
match self.executor.upgrade() {
|
|
|
|
Some(e) => {
|
2022-02-02 03:01:05 +11:00
|
|
|
unsafe { e.execute(task) };
|
2022-02-02 01:31:16 +11:00
|
|
|
true
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
nih_log!("The executor doesn't exist but somehow it's still submitting tasks, this shouldn't be possible!");
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 05:45:11 +11:00
|
|
|
} else {
|
|
|
|
self.worker_thread_channel
|
|
|
|
.try_send(Message::Task(task))
|
|
|
|
.is_ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_main_thread(&self) -> bool {
|
|
|
|
thread::current().id() == self.main_thread_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E> Drop for LinuxEventLoop<T, E> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.worker_thread_channel
|
|
|
|
.send(Message::Shutdown)
|
|
|
|
.expect("Failed while sending worker thread shutdown request");
|
|
|
|
if let Some(join_handle) = self.worker_thread.take() {
|
|
|
|
join_handle.join().expect("Worker thread panicked");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The worker thread used in [EventLoop] that executes incmoing tasks on the event loop's executor.
|
2022-02-02 01:31:16 +11:00
|
|
|
fn worker_thread<T, E>(receiver: channel::Receiver<Message<T>>, executor: Weak<E>)
|
2022-02-01 05:45:11 +11:00
|
|
|
where
|
|
|
|
T: Send,
|
|
|
|
E: MainThreadExecutor<T>,
|
|
|
|
{
|
|
|
|
loop {
|
|
|
|
match receiver.recv() {
|
2022-02-02 01:31:16 +11:00
|
|
|
Ok(Message::Task(task)) => match executor.upgrade() {
|
2022-02-02 03:01:05 +11:00
|
|
|
Some(e) => unsafe { e.execute(task) },
|
2022-02-02 01:31:16 +11:00
|
|
|
None => {
|
|
|
|
nih_log!("Received a new task but the executor is no longer alive, shutting down worker");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2022-02-01 05:45:11 +11:00
|
|
|
Ok(Message::Shutdown) => return,
|
|
|
|
Err(err) => {
|
|
|
|
nih_log!(
|
|
|
|
"Worker thread got disconnected unexpectedly, shutting down: {}",
|
|
|
|
err
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|