Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1 | // Copyright 2020, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! This module implements the handling of async tasks. |
| 16 | //! The worker thread has a high priority and a low priority queue. Adding a job to either |
| 17 | //! will cause one thread to be spawned if none exists. As a compromise between performance |
| 18 | //! and resource consumption, the thread will linger for about 30 seconds after it has |
| 19 | //! processed all tasks before it terminates. |
| 20 | //! Note that low priority tasks are processed only when the high priority queue is empty. |
| 21 | |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 22 | use std::{any::Any, any::TypeId, time::Duration}; |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 23 | use std::{ |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 24 | collections::{HashMap, VecDeque}, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 25 | sync::Arc, |
| 26 | sync::{Condvar, Mutex, MutexGuard}, |
| 27 | thread, |
| 28 | }; |
| 29 | |
David Drysdale | 2566fb3 | 2024-07-09 14:46:37 +0100 | [diff] [blame] | 30 | #[cfg(test)] |
| 31 | mod tests; |
| 32 | |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 33 | #[derive(Debug, PartialEq, Eq)] |
| 34 | enum State { |
| 35 | Exiting, |
| 36 | Running, |
| 37 | } |
| 38 | |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 39 | /// The Shelf allows async tasks to store state across invocations. |
| 40 | /// Note: Store elves at your own peril ;-). |
| 41 | #[derive(Debug, Default)] |
| 42 | pub struct Shelf(HashMap<TypeId, Box<dyn Any + Send>>); |
| 43 | |
| 44 | impl Shelf { |
| 45 | /// Get a reference to the shelved data of type T. Returns Some if the data exists. |
| 46 | pub fn get_downcast_ref<T: Any + Send>(&self) -> Option<&T> { |
| 47 | self.0.get(&TypeId::of::<T>()).and_then(|v| v.downcast_ref::<T>()) |
| 48 | } |
| 49 | |
| 50 | /// Get a mutable reference to the shelved data of type T. If a T was inserted using put, |
| 51 | /// get_mut, or get_or_put_with. |
| 52 | pub fn get_downcast_mut<T: Any + Send>(&mut self) -> Option<&mut T> { |
| 53 | self.0.get_mut(&TypeId::of::<T>()).and_then(|v| v.downcast_mut::<T>()) |
| 54 | } |
| 55 | |
| 56 | /// Remove the entry of the given type and returns the stored data if it existed. |
| 57 | pub fn remove_downcast_ref<T: Any + Send>(&mut self) -> Option<T> { |
| 58 | self.0.remove(&TypeId::of::<T>()).and_then(|v| v.downcast::<T>().ok().map(|b| *b)) |
| 59 | } |
| 60 | |
| 61 | /// Puts data `v` on the shelf. If there already was an entry of type T it is returned. |
| 62 | pub fn put<T: Any + Send>(&mut self, v: T) -> Option<T> { |
| 63 | self.0 |
| 64 | .insert(TypeId::of::<T>(), Box::new(v) as Box<dyn Any + Send>) |
| 65 | .and_then(|v| v.downcast::<T>().ok().map(|b| *b)) |
| 66 | } |
| 67 | |
| 68 | /// Gets a mutable reference to the entry of the given type and default creates it if necessary. |
| 69 | /// The type must implement Default. |
| 70 | pub fn get_mut<T: Any + Send + Default>(&mut self) -> &mut T { |
| 71 | self.0 |
| 72 | .entry(TypeId::of::<T>()) |
Charisee | 03e0084 | 2023-01-25 01:41:23 +0000 | [diff] [blame] | 73 | .or_insert_with(|| Box::<T>::default() as Box<dyn Any + Send>) |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 74 | .downcast_mut::<T>() |
| 75 | .unwrap() |
| 76 | } |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 77 | |
| 78 | /// Gets a mutable reference to the entry of the given type or creates it using the init |
| 79 | /// function. Init is not executed if the entry already existed. |
| 80 | pub fn get_or_put_with<T: Any + Send, F>(&mut self, init: F) -> &mut T |
| 81 | where |
| 82 | F: FnOnce() -> T, |
| 83 | { |
| 84 | self.0 |
| 85 | .entry(TypeId::of::<T>()) |
| 86 | .or_insert_with(|| Box::new(init()) as Box<dyn Any + Send>) |
| 87 | .downcast_mut::<T>() |
| 88 | .unwrap() |
| 89 | } |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 92 | struct AsyncTaskState { |
| 93 | state: State, |
| 94 | thread: Option<thread::JoinHandle<()>>, |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 95 | timeout: Duration, |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 96 | hi_prio_req: VecDeque<Box<dyn FnOnce(&mut Shelf) + Send>>, |
| 97 | lo_prio_req: VecDeque<Box<dyn FnOnce(&mut Shelf) + Send>>, |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 98 | idle_fns: Vec<Arc<dyn Fn(&mut Shelf) + Send + Sync>>, |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 99 | /// The store allows tasks to store state across invocations. It is passed to each invocation |
| 100 | /// of each task. Tasks need to cooperate on the ids they use for storing state. |
| 101 | shelf: Option<Shelf>, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /// AsyncTask spawns one worker thread on demand to process jobs inserted into |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 105 | /// a low and a high priority work queue. The queues are processed FIFO, and low |
| 106 | /// priority queue is processed if the high priority queue is empty. |
| 107 | /// Note: Because there is only one worker thread at a time for a given AsyncTask instance, |
| 108 | /// all scheduled requests are guaranteed to be serialized with respect to one another. |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 109 | pub struct AsyncTask { |
| 110 | state: Arc<(Condvar, Mutex<AsyncTaskState>)>, |
| 111 | } |
| 112 | |
| 113 | impl Default for AsyncTask { |
| 114 | fn default() -> Self { |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 115 | Self::new(Duration::from_secs(30)) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl AsyncTask { |
| 120 | /// Construct an [`AsyncTask`] with a specific timeout value. |
| 121 | pub fn new(timeout: Duration) -> Self { |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 122 | Self { |
| 123 | state: Arc::new(( |
| 124 | Condvar::new(), |
| 125 | Mutex::new(AsyncTaskState { |
| 126 | state: State::Exiting, |
| 127 | thread: None, |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 128 | timeout, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 129 | hi_prio_req: VecDeque::new(), |
| 130 | lo_prio_req: VecDeque::new(), |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 131 | idle_fns: Vec::new(), |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 132 | shelf: None, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 133 | }), |
| 134 | )), |
| 135 | } |
| 136 | } |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 137 | |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 138 | /// Adds a one-off job to the high priority queue. High priority jobs are |
| 139 | /// completed before low priority jobs and can also overtake low priority |
| 140 | /// jobs. But they cannot preempt them. |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 141 | pub fn queue_hi<F>(&self, f: F) |
| 142 | where |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 143 | F: for<'r> FnOnce(&'r mut Shelf) + Send + 'static, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 144 | { |
| 145 | self.queue(f, true) |
| 146 | } |
| 147 | |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 148 | /// Adds a one-off job to the low priority queue. Low priority jobs are |
| 149 | /// completed after high priority. And they are not executed as long as high |
| 150 | /// priority jobs are present. Jobs always run to completion and are never |
| 151 | /// preempted by high priority jobs. |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 152 | pub fn queue_lo<F>(&self, f: F) |
| 153 | where |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 154 | F: FnOnce(&mut Shelf) + Send + 'static, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 155 | { |
| 156 | self.queue(f, false) |
| 157 | } |
| 158 | |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 159 | /// Adds an idle callback. This will be invoked whenever the worker becomes |
| 160 | /// idle (all high and low priority jobs have been performed). |
| 161 | pub fn add_idle<F>(&self, f: F) |
| 162 | where |
| 163 | F: Fn(&mut Shelf) + Send + Sync + 'static, |
| 164 | { |
| 165 | let (ref _condvar, ref state) = *self.state; |
| 166 | let mut state = state.lock().unwrap(); |
| 167 | state.idle_fns.push(Arc::new(f)); |
| 168 | } |
| 169 | |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 170 | fn queue<F>(&self, f: F, hi_prio: bool) |
| 171 | where |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 172 | F: for<'r> FnOnce(&'r mut Shelf) + Send + 'static, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 173 | { |
| 174 | let (ref condvar, ref state) = *self.state; |
| 175 | let mut state = state.lock().unwrap(); |
Janis Danisevskis | 796db6a | 2021-05-06 10:53:31 -0700 | [diff] [blame] | 176 | |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 177 | if hi_prio { |
| 178 | state.hi_prio_req.push_back(Box::new(f)); |
| 179 | } else { |
| 180 | state.lo_prio_req.push_back(Box::new(f)); |
| 181 | } |
| 182 | |
| 183 | if state.state != State::Running { |
| 184 | self.spawn_thread(&mut state); |
| 185 | } |
| 186 | drop(state); |
| 187 | condvar.notify_all(); |
| 188 | } |
| 189 | |
| 190 | fn spawn_thread(&self, state: &mut MutexGuard<AsyncTaskState>) { |
| 191 | if let Some(t) = state.thread.take() { |
| 192 | t.join().expect("AsyncTask panicked."); |
| 193 | } |
| 194 | |
| 195 | let cloned_state = self.state.clone(); |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 196 | let timeout_period = state.timeout; |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 197 | |
| 198 | state.thread = Some(thread::spawn(move || { |
| 199 | let (ref condvar, ref state) = *cloned_state; |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 200 | |
| 201 | enum Action { |
| 202 | QueuedFn(Box<dyn FnOnce(&mut Shelf) + Send>), |
| 203 | IdleFns(Vec<Arc<dyn Fn(&mut Shelf) + Send + Sync>>), |
Jeff Vander Stoep | 46bbc61 | 2021-04-09 08:55:21 +0200 | [diff] [blame] | 204 | } |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 205 | let mut done_idle = false; |
| 206 | |
Janis Danisevskis | 40f0e6b | 2021-02-10 15:48:44 -0800 | [diff] [blame] | 207 | // When the worker starts, it takes the shelf and puts it on the stack. |
| 208 | let mut shelf = state.lock().unwrap().shelf.take().unwrap_or_default(); |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 209 | loop { |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 210 | if let Some(action) = { |
| 211 | let state = state.lock().unwrap(); |
| 212 | if !done_idle && state.hi_prio_req.is_empty() && state.lo_prio_req.is_empty() { |
| 213 | // No jobs queued so invoke the idle callbacks. |
| 214 | Some(Action::IdleFns(state.idle_fns.clone())) |
| 215 | } else { |
| 216 | // Wait for either a queued job to arrive or a timeout. |
| 217 | let (mut state, timeout) = condvar |
| 218 | .wait_timeout_while(state, timeout_period, |state| { |
| 219 | state.hi_prio_req.is_empty() && state.lo_prio_req.is_empty() |
| 220 | }) |
| 221 | .unwrap(); |
| 222 | match ( |
| 223 | state.hi_prio_req.pop_front(), |
| 224 | state.lo_prio_req.is_empty(), |
| 225 | timeout.timed_out(), |
| 226 | ) { |
| 227 | (Some(f), _, _) => Some(Action::QueuedFn(f)), |
| 228 | (None, false, _) => { |
| 229 | state.lo_prio_req.pop_front().map(|f| Action::QueuedFn(f)) |
| 230 | } |
| 231 | (None, true, true) => { |
| 232 | // When the worker exits it puts the shelf back into the shared |
| 233 | // state for the next worker to use. So state is preserved not |
| 234 | // only across invocations but also across worker thread shut down. |
| 235 | state.shelf = Some(shelf); |
| 236 | state.state = State::Exiting; |
| 237 | break; |
| 238 | } |
| 239 | (None, true, false) => None, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 240 | } |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 241 | } |
| 242 | } { |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 243 | // Now that the lock has been dropped, perform the action. |
| 244 | match action { |
| 245 | Action::QueuedFn(f) => { |
| 246 | f(&mut shelf); |
| 247 | done_idle = false; |
| 248 | } |
| 249 | Action::IdleFns(idle_fns) => { |
| 250 | for idle_fn in idle_fns { |
| 251 | idle_fn(&mut shelf); |
| 252 | } |
| 253 | done_idle = true; |
| 254 | } |
| 255 | } |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | })); |
| 259 | state.state = State::Running; |
| 260 | } |
| 261 | } |