1 // Copyright (C) 2024 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use super::*;
15 
16 extern crate ylong_runtime;
17 
18 use std::future::Future;
19 
20 use crate::errors::IpcStatusCode;
21 
22 /// Use the Ylong `spawn_blocking` pool
23 pub enum Ylong {}
24 
25 impl IpcAsyncPool for Ylong {
spawn<'a, F1, F2, Fut, A, B>(spawn_this: F1, after_handle: F2) -> BoxFuture<'a, IpcResult<B>> where F1: FnOnce() -> A, F2: FnOnce(A) -> Fut, Fut: Future<Output = IpcResult<B>>, F1: Send + 'static, F2: Send + 'a, Fut: Send + 'a, A: Send + 'static, B: Send + 'a,26     fn spawn<'a, F1, F2, Fut, A, B>(spawn_this: F1, after_handle: F2) -> BoxFuture<'a, IpcResult<B>>
27     where
28         F1: FnOnce() -> A,
29         F2: FnOnce(A) -> Fut,
30         Fut: Future<Output = IpcResult<B>>,
31         F1: Send + 'static,
32         F2: Send + 'a,
33         Fut: Send + 'a,
34         A: Send + 'static,
35         B: Send + 'a,
36     {
37         if crate::process::is_handling_transaction() {
38             // We are currently on the thread pool for a binder server, so we should execute
39             // the transaction on the current thread so that the binder kernel
40             // driver is able to apply its deadlock prevention strategy to the
41             // sub-call.
42             //
43             // This shouldn't cause issues with blocking the thread as only one task will
44             // run in a call to `block_on`, so there aren't other tasks to
45             // block.
46             let result = spawn_this();
47             Box::pin(after_handle(result))
48         } else {
49             let handle = Runtime::spawn_blocking(spawn_this);
50             Box::pin(async move {
51                 // The `is_panic` branch is not actually reachable in OH as we compile
52                 // with `panic = abort`.
53                 match handle.await {
54                     Ok(res) => after_handle(res).await,
55                     Err(_) => Err(IpcStatusCode::Failed),
56                 }
57             })
58         }
59     }
60 }
61 
62 /// Wrapper around Ylong runtime types for providing a runtime to a binder
63 /// server.
64 pub struct Runtime;
65 
66 impl IpcAsyncRuntime for Runtime {
spawn<T, R>(task: T) -> JoinHandle<R> where T: Future<Output = R>, T: Send + 'static, R: Send + 'static,67     fn spawn<T, R>(task: T) -> JoinHandle<R>
68     where
69         T: Future<Output = R>,
70         T: Send + 'static,
71         R: Send + 'static,
72     {
73         ylong_runtime::spawn(task)
74     }
75 
spawn_blocking<T, R>(task: T) -> JoinHandle<R> where T: FnOnce() -> R, T: Send + 'static, R: Send + 'static,76     fn spawn_blocking<T, R>(task: T) -> JoinHandle<R>
77     where
78         T: FnOnce() -> R,
79         T: Send + 'static,
80         R: Send + 'static,
81     {
82         ylong_runtime::spawn_blocking(task)
83     }
84 
block_on<F: Future>(future: F) -> F::Output85     fn block_on<F: Future>(future: F) -> F::Output {
86         ylong_runtime::block_on(future)
87     }
88 }
89