feat[osal]: Add mpp_singleton module

Signed-off-by: Chandler Chen <chandler.chen@rock-chips.com>
Signed-off-by: Hongjin Li <vic.hong@rock-chips.com>
Signed-off-by: Herman Chen <herman.chen@rock-chips.com>
Change-Id: Idfff4d03e1c98e4ad1ddf1553133ba4b0194f7b8
This commit is contained in:
Hongjin Li
2025-03-18 21:01:58 +08:00
committed by Herman Chen
parent f2eb2a2b59
commit 095fdce624
3 changed files with 163 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ set(MPP_DRIVER
)
add_library(osal OBJECT
mpp_singleton.c
mpp_soc.cpp
mpp_platform.cpp
mpp_runtime.cpp

67
osal/inc/mpp_singleton.h Normal file
View File

@@ -0,0 +1,67 @@
/* SPDX-License-Identifier: Apache-2.0 OR MIT */
/*
* Copyright (c) 2024 Rockchip Electronics Co., Ltd.
*/
#ifndef __MPP_SINGLETON_H__
#define __MPP_SINGLETON_H__
#include "rk_type.h"
typedef enum MppSingletonId_e {
MPP_SGLN_BASE = 0,
/* osal base module */
MPP_SGLN_OS_LOG = MPP_SGLN_BASE,
MPP_SGLN_OS_MEM,
MPP_SGLN_OS_ALLOCATOR,
MPP_SGLN_MEM_POOL,
/* hardware platform */
MPP_SGLN_SOC,
MPP_SGLN_PLATFORM,
/* software platform */
MPP_SGLN_RUNTIME,
/* base module */
MPP_SGLN_BUFFER,
MPP_SGLN_META,
MPP_SGLN_FRAME,
MPP_SGLN_PACKET,
/* system module */
MPP_SGLN_KOBJ,
MPP_SGLN_ENC_CFG,
MPP_SGLN_DEC_CFG,
MPP_SGLN_DEC_RC_API,
/* max count for start init process */
MPP_SGLN_MAX_CNT,
} MppSingletonId;
typedef struct MppSingletonInfo_t {
MppSingletonId id;
const char *name;
void (*init)(void);
void (*deinit)(void);
} MppSingletonInfo;
#define MPP_SINGLETON(id, name, init, deinit) \
__attribute__((constructor)) \
static void __mpp_singleton_add(void) { \
MppSingletonInfo info = { \
id, \
name, \
init, \
deinit, \
}; \
mpp_singleton_add(&info); \
}
#ifdef __cplusplus
extern "C" {
#endif
rk_s32 mpp_singleton_add(MppSingletonInfo *info);
#ifdef __cplusplus
}
#endif
#endif /* __MPP_SINGLETON_H__ */

95
osal/mpp_singleton.c Normal file
View File

@@ -0,0 +1,95 @@
/* SPDX-License-Identifier: Apache-2.0 OR MIT */
/*
* Copyright (c) 2024 Rockchip Electronics Co., Ltd.
*/
#define MODULE_TAG "mpp_singleton"
#include <stdio.h>
#include <stdlib.h>
#include "mpp_env.h"
#include "mpp_singleton.h"
#define sgln_dbg(fmt, ...) \
do { \
if (sgln_debug) \
printf(MODULE_TAG ": " fmt, ##__VA_ARGS__); \
} while (0)
static MppSingletonInfo sgln_info[MPP_SGLN_MAX_CNT] = {0};
static rk_u64 sgln_mask = 0;
static rk_u32 sgln_debug = 0;
rk_s32 mpp_singleton_add(MppSingletonInfo *info)
{
mpp_env_get_u32("mpp_sgln_debug", &sgln_debug, 0);
if (!info) {
sgln_dbg("can not add NULL info\n");
return rk_nok;
}
if (info->id >= MPP_SGLN_MAX_CNT) {
sgln_dbg("id %d larger than max %d\n", info->id, MPP_SGLN_MAX_CNT);
return rk_nok;
}
if (sgln_mask & (1 << info->id)) {
sgln_dbg("info %d has been registered\n", info->id);
return rk_nok;
}
sgln_info[info->id] = *info;
sgln_mask |= (1 << info->id);
sgln_dbg("info %d %s registered\n", info->id, info->name);
return rk_ok;
}
static void mpp_singleton_deinit(void)
{
rk_s32 i;
sgln_dbg("deinit enter\n");
/* NOTE: revert deinit order */
for (i = MPP_SGLN_MAX_CNT - 1; i >= 0; i--) {
if (sgln_mask & (1 << i)) {
MppSingletonInfo *info = &sgln_info[i];
if (info->deinit) {
sgln_dbg("info %d %s deinit start\n", info->id, info->name);
info->deinit();
sgln_dbg("info %d %s deinit finish\n", info->id, info->name);
}
}
}
sgln_dbg("deinit leave\n");
}
__attribute__((constructor(65535))) static void mpp_singleton_init(void)
{
rk_s32 i;
sgln_dbg("init enter\n");
/* NOTE: call atexit first to avoid init crash but not deinit */
atexit(mpp_singleton_deinit);
for (i = 0; i < MPP_SGLN_MAX_CNT; i++) {
if (sgln_mask & (1 << i)) {
MppSingletonInfo *info = &sgln_info[i];
if (info->init) {
sgln_dbg("info %d %s init start\n", info->id, info->name);
info->init();
sgln_dbg("info %d %s init finish\n", info->id, info->name);
}
}
}
sgln_dbg("init leave\n");
}