Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 17 | use crate::instance_starter::CompOsInstance; |
| 18 | use crate::odrefresh; |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 19 | use android_system_composd::aidl::android::system::composd::{ |
| 20 | ICompilationTask::ICompilationTask, ICompilationTaskCallback::ICompilationTaskCallback, |
| 21 | }; |
| 22 | use android_system_composd::binder::{Interface, Result as BinderResult, Strong}; |
Alan Stokes | ac9aa1a | 2021-12-14 11:32:13 +0000 | [diff] [blame] | 23 | use anyhow::Result; |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 24 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::ICompOsService; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame^] | 25 | use compos_common::odrefresh::ExitCode; |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 26 | use log::{error, warn}; |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 27 | use std::sync::{Arc, Mutex}; |
| 28 | use std::thread; |
| 29 | |
| 30 | #[derive(Clone)] |
| 31 | pub struct OdrefreshTask { |
| 32 | running_task: Arc<Mutex<Option<RunningTask>>>, |
| 33 | } |
| 34 | |
| 35 | impl Interface for OdrefreshTask {} |
| 36 | |
| 37 | impl ICompilationTask for OdrefreshTask { |
| 38 | fn cancel(&self) -> BinderResult<()> { |
| 39 | let task = self.take(); |
| 40 | // Drop the VM, which should end compilation - and cause our thread to exit |
| 41 | drop(task); |
| 42 | Ok(()) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl OdrefreshTask { |
| 47 | /// Return the current running task, if any, removing it from this CompilationTask. |
| 48 | /// Once removed, meaning the task has ended or been canceled, further calls will always return |
| 49 | /// None. |
| 50 | fn take(&self) -> Option<RunningTask> { |
| 51 | self.running_task.lock().unwrap().take() |
| 52 | } |
| 53 | |
| 54 | pub fn start( |
| 55 | comp_os: Arc<CompOsInstance>, |
Alan Stokes | ac9aa1a | 2021-12-14 11:32:13 +0000 | [diff] [blame] | 56 | target_dir_name: String, |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 57 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 58 | ) -> Result<OdrefreshTask> { |
| 59 | let service = comp_os.get_service(); |
| 60 | let task = RunningTask { comp_os, callback: callback.clone() }; |
| 61 | let task = OdrefreshTask { running_task: Arc::new(Mutex::new(Some(task))) }; |
| 62 | |
Alan Stokes | ac9aa1a | 2021-12-14 11:32:13 +0000 | [diff] [blame] | 63 | task.clone().start_thread(service, target_dir_name); |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 64 | |
| 65 | Ok(task) |
| 66 | } |
| 67 | |
Alan Stokes | ac9aa1a | 2021-12-14 11:32:13 +0000 | [diff] [blame] | 68 | fn start_thread(self, service: Strong<dyn ICompOsService>, target_dir_name: String) { |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 69 | thread::spawn(move || { |
Alan Stokes | ac9aa1a | 2021-12-14 11:32:13 +0000 | [diff] [blame] | 70 | let exit_code = odrefresh::run_in_vm(service, &target_dir_name); |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 71 | |
| 72 | let task = self.take(); |
| 73 | // We don't do the callback if cancel has already happened. |
| 74 | if let Some(task) = task { |
| 75 | let result = match exit_code { |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame^] | 76 | Ok(ExitCode::CompilationSuccess) => task.callback.onSuccess(), |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 77 | Ok(exit_code) => { |
| 78 | error!("Unexpected odrefresh result: {:?}", exit_code); |
| 79 | task.callback.onFailure() |
| 80 | } |
| 81 | Err(e) => { |
| 82 | error!("Running odrefresh failed: {:?}", e); |
| 83 | task.callback.onFailure() |
| 84 | } |
| 85 | }; |
| 86 | if let Err(e) = result { |
| 87 | warn!("Failed to deliver callback: {:?}", e); |
| 88 | } |
| 89 | } |
| 90 | }); |
| 91 | } |
| 92 | } |
| 93 | |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 94 | struct RunningTask { |
| 95 | callback: Strong<dyn ICompilationTaskCallback>, |
| 96 | #[allow(dead_code)] // Keeps the CompOS VM alive |
| 97 | comp_os: Arc<CompOsInstance>, |
| 98 | } |