1 /* 2 * Copyright (c) 2024 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 unload handler. 17 18 /// Manages the unload request. 19 use std::sync::{Arc, Mutex}; 20 21 use ylong_runtime::task::JoinHandle; 22 23 pub(crate) struct UnloadHandler { 24 task: Option<JoinHandle<()>>, 25 } 26 27 pub(crate) static DELAYED_UNLOAD_TIME_IN_SEC: i32 = 20; 28 pub(crate) static SEC_TO_MILLISEC: i32 = 1000; 29 30 impl UnloadHandler { new() -> Self31 fn new() -> Self { 32 Self { task: None } 33 } 34 35 /// Get the single instance of UnloadHandler. get_instance() -> Arc<Mutex<UnloadHandler>>36 pub(crate) fn get_instance() -> Arc<Mutex<UnloadHandler>> { 37 static mut INSTANCE: Option<Arc<Mutex<UnloadHandler>>> = None; 38 unsafe { INSTANCE.get_or_insert_with(|| Arc::new(Mutex::new(UnloadHandler::new()))).clone() } 39 } 40 41 /// update task in unload handler update_task(&mut self, new_task: JoinHandle<()>)42 pub(crate) fn update_task(&mut self, new_task: JoinHandle<()>) { 43 if let Some(t) = &self.task { 44 t.cancel(); 45 }; 46 self.task = Some(new_task); 47 } 48 } 49