Skip to content

Commit

Permalink
Add reference count to metal_common_state
Browse files Browse the repository at this point in the history
to allow to invoke metal_init and metal_finish multiple times

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
  • Loading branch information
xiaoxiang781216 authored and arnopo committed Jul 19, 2023
1 parent 4976e58 commit 6fb9f45
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ int metal_init(const struct metal_init_params *params)
{
int error = 0;

if (_metal.common.ref_count++ != 0)
return 0;

memset(&_metal, 0, sizeof(_metal));

_metal.common.log_handler = params->log_handler;
Expand All @@ -24,11 +27,15 @@ int metal_init(const struct metal_init_params *params)
if (error)
return error;

++_metal.common.ref_count;
return error;
}

void metal_finish(void)
{
if (--_metal.common.ref_count != 0)
return;

metal_sys_finish();
memset(&_metal, 0, sizeof(_metal));
}
3 changes: 3 additions & 0 deletions lib/sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ struct metal_init_params {
* specific singleton data structure (@see _metal).
*/
struct metal_common_state {
/** reference count to track metal_init/metal_finish. */
int ref_count;

/** Current log level. */
enum metal_log_level log_level;

Expand Down

0 comments on commit 6fb9f45

Please sign in to comment.