Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reference count to metal_common_state #244

Merged
merged 1 commit into from
Jul 19, 2023
Merged

Conversation

xiaoxiang781216
Copy link
Collaborator

to allow to invoke metal_init and metal_finish multiple times

to allow to invoke metal_init and metal_finish multiple times

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
lib/init.c Outdated
@@ -11,24 +11,30 @@ int metal_init(const struct metal_init_params *params)
{
int error = 0;

memset(&_metal, 0, sizeof(_metal));
if (_metal.common.ref_count++ == 0) {
memset(&_metal.common + 1, 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&_metal.common + 1 is probably not good asref_countis an integer.
More than that I wonder if creating a static variable for refcount would not be more better than having this address shift

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but is it good to use global reference count for the local variables at least that it isn't a global variable in libmetal level?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The __metal is a global structure that can be defined by platform. This structure can not be instantiate, in current implementation.
So we have to take into account that the only constraint of this structure is to include a struct metal_common_state, but nothing impose that this have to be the first element of __metal
In this case, to initialize __metal, we have to implement the memset(&_metal, 0, sizeof(_metal));
Far from perfect...

That said here the only variable we want to protect is '_metal.common.ref_count++'
So if it equals to 0, no issue to use memset(&_metal, 0, sizeof(_metal));, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I ajdust the change, please take a look.

Copy link
Contributor

@arnopo arnopo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
thanks!

@arnopo arnopo requested review from edmooring and tnmysh July 12, 2023 06:56
Copy link
Collaborator

@tnmysh tnmysh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants