blob: 64565ddf5d38f4dac4ec695abe5a0eed6bc030bb [file] [log] [blame]
Alan Stokes8c840442021-11-26 15:54:30 +00001/*
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 Stokes8c840442021-11-26 15:54:30 +000017use crate::instance_starter::CompOsInstance;
18use crate::odrefresh;
Alan Stokes8c840442021-11-26 15:54:30 +000019use android_system_composd::aidl::android::system::composd::{
20 ICompilationTask::ICompilationTask, ICompilationTaskCallback::ICompilationTaskCallback,
21};
22use android_system_composd::binder::{Interface, Result as BinderResult, Strong};
Alan Stokesac9aa1a2021-12-14 11:32:13 +000023use anyhow::Result;
Alan Stokes8c840442021-11-26 15:54:30 +000024use compos_aidl_interface::aidl::com::android::compos::ICompOsService::ICompOsService;
25use log::{error, warn};
Alan Stokes8c840442021-11-26 15:54:30 +000026use std::sync::{Arc, Mutex};
27use std::thread;
28
29#[derive(Clone)]
30pub struct OdrefreshTask {
31 running_task: Arc<Mutex<Option<RunningTask>>>,
32}
33
34impl Interface for OdrefreshTask {}
35
36impl ICompilationTask for OdrefreshTask {
37 fn cancel(&self) -> BinderResult<()> {
38 let task = self.take();
39 // Drop the VM, which should end compilation - and cause our thread to exit
40 drop(task);
41 Ok(())
42 }
43}
44
45impl OdrefreshTask {
46 /// Return the current running task, if any, removing it from this CompilationTask.
47 /// Once removed, meaning the task has ended or been canceled, further calls will always return
48 /// None.
49 fn take(&self) -> Option<RunningTask> {
50 self.running_task.lock().unwrap().take()
51 }
52
53 pub fn start(
54 comp_os: Arc<CompOsInstance>,
Alan Stokesac9aa1a2021-12-14 11:32:13 +000055 target_dir_name: String,
Alan Stokes8c840442021-11-26 15:54:30 +000056 callback: &Strong<dyn ICompilationTaskCallback>,
57 ) -> Result<OdrefreshTask> {
58 let service = comp_os.get_service();
59 let task = RunningTask { comp_os, callback: callback.clone() };
60 let task = OdrefreshTask { running_task: Arc::new(Mutex::new(Some(task))) };
61
Alan Stokesac9aa1a2021-12-14 11:32:13 +000062 task.clone().start_thread(service, target_dir_name);
Alan Stokes8c840442021-11-26 15:54:30 +000063
64 Ok(task)
65 }
66
Alan Stokesac9aa1a2021-12-14 11:32:13 +000067 fn start_thread(self, service: Strong<dyn ICompOsService>, target_dir_name: String) {
Alan Stokes8c840442021-11-26 15:54:30 +000068 thread::spawn(move || {
Alan Stokesac9aa1a2021-12-14 11:32:13 +000069 let exit_code = odrefresh::run_in_vm(service, &target_dir_name);
Alan Stokes8c840442021-11-26 15:54:30 +000070
71 let task = self.take();
72 // We don't do the callback if cancel has already happened.
73 if let Some(task) = task {
74 let result = match exit_code {
75 Ok(odrefresh::ExitCode::CompilationSuccess) => task.callback.onSuccess(),
76 Ok(exit_code) => {
77 error!("Unexpected odrefresh result: {:?}", exit_code);
78 task.callback.onFailure()
79 }
80 Err(e) => {
81 error!("Running odrefresh failed: {:?}", e);
82 task.callback.onFailure()
83 }
84 };
85 if let Err(e) = result {
86 warn!("Failed to deliver callback: {:?}", e);
87 }
88 }
89 });
90 }
91}
92
Alan Stokes8c840442021-11-26 15:54:30 +000093struct RunningTask {
94 callback: Strong<dyn ICompilationTaskCallback>,
95 #[allow(dead_code)] // Keeps the CompOS VM alive
96 comp_os: Arc<CompOsInstance>,
97}