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

Remove global variable #1913

Merged
merged 3 commits into from
Oct 4, 2021

Conversation

quink-black
Copy link
Contributor

No description provided.

Copy link
Collaborator

@maxsharabayko maxsharabayko left a comment

Choose a reason for hiding this comment

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

CI build failure. Not all places of use were updated.
Please build with -DENABLE_EXPERIMENTAL_BONDING=ON to see more places.

@quink-black quink-black force-pushed the bugfix-global-variable branch 2 times, most recently from 2443cb3 to 473ad03 Compare April 13, 2021 03:12
@quink-black
Copy link
Contributor Author

CI build failure. Not all places of use were updated.
Please build with -DENABLE_EXPERIMENTAL_BONDING=ON to see more places.

Thanks for the review, I missed that part. A new version was pushed with double check by grep 's_UDTUnited'.

srtcore/api.cpp Outdated Show resolved Hide resolved
@maxsharabayko maxsharabayko added [core] Area: Changes in SRT library core Type: Maintenance Work required to maintain or clean up the code labels Apr 13, 2021
@quink-black
Copy link
Contributor Author

Finally CI build success on all platforms.

@ethouris
Copy link
Collaborator

What exactly problem does this solve?

@quink-black
Copy link
Contributor Author

What exactly problem does this solve?

Trying to avoid non-plain-old-data type global variables. The second commit is an example of what kind of problem does non-POD global variable have. And there are other kind of issues like exception handling.

@maxsharabayko maxsharabayko added this to the v1.4.4 milestone Apr 14, 2021
Copy link
Collaborator

@maxsharabayko maxsharabayko left a comment

Choose a reason for hiding this comment

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

@quink-black Could you please update the PR to the latest master (resolve conflicts)?

srtcore/core.cpp Outdated
Comment on lines 232 to 242
static void createInstance()
{
s_UDTUnited = new CUDTUnited();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

How would the CUDTUnited object be destructed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know how to delete the singleton automatically in a portable way.

CUDT::cleanup is a good entry to trigger delete, then another global lock is needed to protect CUDT::startup() and CUDT::cleanup() called by multiple threads.

What's your suggestion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about this:

static CUDTUnited *getInstance()
{
    static CUDTUnited instance;
    return &instance;
}

static void createInstance() {
    getInstance();
}

CUDTUnited* srt::CUDT::uglobal()
{
    pthread_once(&s_UDTUnitedOnce, createInstance);
    return getInstance();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or just do cast and remove the createInstance()

srtcore/core.cpp Outdated
Comment on lines 221 to 232
CUDTUnited* CUDT::uglobal()
{
static CUDTUnited instance;
return &instance;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

To reduce dereferencing, this would be a more convenient function.

Suggested change
CUDTUnited* CUDT::uglobal()
{
static CUDTUnited instance;
return &instance;
}
CUDTUnited& CUDT::uglobal()
{
static CUDTUnited instance;
return instance;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think there is performance difference between pointer and reference. They are compiled to the same machine code.

If the singleton needs to be deleted as the previous review comments, then delete a reference looks odd.

Static local variable has the benefits of lazy initialization and
avoids the initialization order problem of global variables.
@maxsharabayko
Copy link
Collaborator

Hi @quink-black. Sorry for a notable pause in the review.
I think the PR looks good now.

I am only still thinking about srt::CUDT::uglobal() returning a reference instead of a pointer. I agree there would be no performance difference between pointer and reference. The main reason I believe it would be better is that with a reference we always know we don't ever return a NULL object, and we don't need to check it in various places in the code.

If you still believe a pointer is better, then it might be worth marking a function with __attribute__((returns_nonnull)) (placed in srt_attr_defs.h).

@maxsharabayko maxsharabayko modified the milestones: v1.4.4, v1.4.5 Aug 18, 2021
@quink-black
Copy link
Contributor Author

Hi @quink-black. Sorry for a notable pause in the review.
I think the PR looks good now.

I am only still thinking about srt::CUDT::uglobal() returning a reference instead of a pointer. I agree there would be no performance difference between pointer and reference. The main reason I believe it would be better is that with a reference we always know we don't ever return a NULL object, and we don't need to check it in various places in the code.

Good point! I have added a new commit, with a lot of small changes from pointer to reference.

@maxsharabayko maxsharabayko merged commit 5b0811c into Haivision:master Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[core] Area: Changes in SRT library core Type: Maintenance Work required to maintain or clean up the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants