1cac7dca0Sopenharmony_ci// Copyright (c) 2023 Huawei Device Co., Ltd.
2cac7dca0Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
3cac7dca0Sopenharmony_ci// you may not use this file except in compliance with the License.
4cac7dca0Sopenharmony_ci// You may obtain a copy of the License at
5cac7dca0Sopenharmony_ci//
6cac7dca0Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
7cac7dca0Sopenharmony_ci//
8cac7dca0Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
9cac7dca0Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
10cac7dca0Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11cac7dca0Sopenharmony_ci// See the License for the specific language governing permissions and
12cac7dca0Sopenharmony_ci// limitations under the License.
13cac7dca0Sopenharmony_ci
14cac7dca0Sopenharmony_ciuse std::collections::HashMap;
15cac7dca0Sopenharmony_ciuse std::mem::MaybeUninit;
16cac7dca0Sopenharmony_ciuse std::sync::Once;
17cac7dca0Sopenharmony_ci
18cac7dca0Sopenharmony_ciuse libc::c_int;
19cac7dca0Sopenharmony_ci
20cac7dca0Sopenharmony_ciuse crate::common::{SigAction, Signal};
21cac7dca0Sopenharmony_ciuse crate::spin_rwlock::SpinningRwLock;
22cac7dca0Sopenharmony_ci
23cac7dca0Sopenharmony_cipub(crate) struct SigMap {
24cac7dca0Sopenharmony_ci    pub(crate) data: SpinningRwLock<HashMap<c_int, Signal>>,
25cac7dca0Sopenharmony_ci    pub(crate) race_old: SpinningRwLock<Option<SigAction>>,
26cac7dca0Sopenharmony_ci}
27cac7dca0Sopenharmony_ci
28cac7dca0Sopenharmony_ciimpl SigMap {
29cac7dca0Sopenharmony_ci    // This function will be called inside a signal handler.
30cac7dca0Sopenharmony_ci    // Although a mutex Once is used, but the mutex will only be locked for once
31cac7dca0Sopenharmony_ci    // when initializing the SignalManager, which is outside of the signal
32cac7dca0Sopenharmony_ci    // handler.
33cac7dca0Sopenharmony_ci    pub(crate) fn get_instance() -> &'static SigMap {
34cac7dca0Sopenharmony_ci        static ONCE: Once = Once::new();
35cac7dca0Sopenharmony_ci        static mut GLOBAL_SIG_MAP: MaybeUninit<SigMap> = MaybeUninit::uninit();
36cac7dca0Sopenharmony_ci
37cac7dca0Sopenharmony_ci        unsafe {
38cac7dca0Sopenharmony_ci            ONCE.call_once(|| {
39cac7dca0Sopenharmony_ci                GLOBAL_SIG_MAP = MaybeUninit::new(SigMap {
40cac7dca0Sopenharmony_ci                    data: SpinningRwLock::new(HashMap::new()),
41cac7dca0Sopenharmony_ci                    race_old: SpinningRwLock::new(None),
42cac7dca0Sopenharmony_ci                });
43cac7dca0Sopenharmony_ci            });
44cac7dca0Sopenharmony_ci            GLOBAL_SIG_MAP.assume_init_ref()
45cac7dca0Sopenharmony_ci        }
46cac7dca0Sopenharmony_ci    }
47cac7dca0Sopenharmony_ci}
48