• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
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 
16 //! This module is used to Asset service task manager.
17 
18 /// Manages the count.
19 use std::sync::{Arc, Mutex};
20 
21 use ylong_runtime::task::JoinHandle;
22 
23 /// Manager asset tasks execute state.
24 pub struct TaskManager {
25     task_pool: Vec<JoinHandle<()>>,
26 }
27 
28 impl TaskManager {
new() -> Self29     fn new() -> Self {
30         Self { task_pool: vec![] }
31     }
32 
33     /// Get the single instance of TaskManager.
get_instance() -> Arc<Mutex<TaskManager>>34     pub fn get_instance() -> Arc<Mutex<TaskManager>> {
35         static mut INSTANCE: Option<Arc<Mutex<TaskManager>>> = None;
36         unsafe { INSTANCE.get_or_insert_with(|| Arc::new(Mutex::new(TaskManager::new()))).clone() }
37     }
38 
39     /// Push task.
push_task(&mut self, join_handle: JoinHandle<()>)40     pub fn push_task(&mut self, join_handle: JoinHandle<()>) {
41         self.task_pool.push(join_handle);
42     }
43 
44     /// Is task_pool empty.
is_empty(&mut self) -> bool45     pub fn is_empty(&mut self) -> bool {
46         self.task_pool.retain(|handle| !handle.is_finished());
47         self.task_pool.is_empty()
48     }
49 }
50