1
0
Fork 0

Implement Linux event loop using BackgroundThread

This reduces duplication a lot.
This commit is contained in:
Robbert van der Helm 2023-01-06 17:53:07 +01:00
parent 5c9be077e7
commit 28cd25d501

View file

@ -2,11 +2,10 @@
//! 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;
use std::sync::{Arc, Weak};
use std::thread::{self, JoinHandle, ThreadId};
use std::sync::Arc;
use std::thread::{self, ThreadId};
use super::{EventLoop, MainThreadExecutor};
use super::{BackgroundThread, EventLoop, MainThreadExecutor};
use crate::util::permit_alloc;
/// See [`EventLoop`][super::EventLoop].
@ -16,25 +15,13 @@ pub(crate) struct LinuxEventLoop<T, E> {
/// directly.
executor: Arc<E>,
/// The actual background thread. The implementation is shared with the background thread used
/// in other backends.
background_thread: BackgroundThread<T, E>,
/// The ID of the main thread. In practice this is the ID of the thread that created this task
/// queue.
main_thread_id: ThreadId,
/// A thread that act as our worker thread. When [`schedule_gui()`][Self::schedule_gui()] is
/// called, this thread will be 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`].
tasks_sender: 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>
@ -43,19 +30,10 @@ where
E: MainThreadExecutor<T> + 'static,
{
fn new_and_spawn(executor: Arc<E>) -> Self {
let (tasks_sender, tasks_receiver) = channel::bounded(super::TASK_QUEUE_CAPACITY);
Self {
executor: executor.clone(),
background_thread: BackgroundThread::get_or_create(executor),
main_thread_id: thread::current().id(),
// With our drop implementation we guarantee that this thread never outlives this struct
worker_thread: Some(
thread::Builder::new()
.name(String::from("worker"))
.spawn(move || worker_thread(tasks_receiver, Arc::downgrade(&executor)))
.expect("Could not spawn worker thread"),
),
tasks_sender,
}
}
@ -64,14 +42,14 @@ where
self.executor.execute(task, true);
true
} else {
self.tasks_sender.try_send(Message::Task(task)).is_ok()
self.background_thread.schedule(task)
}
}
fn schedule_background(&self, task: T) -> bool {
// This event loop implementation already uses a thread that's completely decoupled from the
// operating system's or the host's main thread, so we don't need _another_ thread here
self.tasks_sender.try_send(Message::Task(task)).is_ok()
self.background_thread.schedule(task)
}
fn is_main_thread(&self) -> bool {
@ -80,45 +58,3 @@ where
permit_alloc(|| thread::current().id() == self.main_thread_id)
}
}
impl<T, E> Drop for LinuxEventLoop<T, E> {
fn drop(&mut self) {
self.tasks_sender
.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 incoming tasks on the event loop's
/// executor.
fn worker_thread<T, E>(tasks_receiver: channel::Receiver<Message<T>>, executor: Weak<E>)
where
T: Send,
E: MainThreadExecutor<T>,
{
loop {
match tasks_receiver.recv() {
Ok(Message::Task(task)) => match executor.upgrade() {
Some(e) => e.execute(task, true),
None => {
nih_trace!(
"Received a new task but the executor is no longer alive, shutting down \
worker"
);
return;
}
},
Ok(Message::Shutdown) => return,
Err(err) => {
nih_trace!(
"Worker thread got disconnected unexpectedly, shutting down: {}",
err
);
return;
}
}
}
}