Skip to content

Freeze registries at startup, when everything has been registered #10388

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

Al2Klimov
Copy link
Member

to query them at runtime lock-freely.

@Al2Klimov Al2Klimov added enhancement New feature or request core/quality Improve code, libraries, algorithms, inline docs labels Mar 21, 2025
@cla-bot cla-bot bot added the cla/signed label Mar 21, 2025
Comment on lines +83 to 90
std::shared_lock<std::shared_mutex> ReadLockUnlessFrozen() const
{
bool old_item = false;

if (m_Items.erase(name) > 0)
old_item = true;

m_Items[name] = item;

lock.unlock();

if (old_item)
OnUnregistered(name);
if (m_Frozen.load(std::memory_order_relaxed)) {
return {};
}

OnRegistered(name, item);
return std::shared_lock(m_Mutex);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

*/
void Freeze()
{
std::shared_lock lock (m_Mutex);
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need lock this here? Isn't m_Frozen atomic?

Copy link
Member Author

Choose a reason for hiding this comment

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

Comment on lines +39 to +41
if (m_Frozen) {
BOOST_THROW_EXCEPTION(std::logic_error("Registry is read-only and must not be modified."));
}
Copy link
Member

Choose a reason for hiding this comment

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

You can also perform this check before even locking the mutex. I don't understand this, if you need the mutex for m_Frozen as well, why make it atomic?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

This doesn't answer the questions! And no, you didn’t do exactly the same.

if (m_Frozen) {
BOOST_THROW_EXCEPTION(ScriptError("Namespace is read-only and must not be modified.", debugInfo));
}
std::unique_lock<decltype(m_DataMutex)> dlock (m_DataMutex);

if (m_Frozen) {
BOOST_THROW_EXCEPTION(ScriptError("Namespace is read-only and must not be modified."));
}
std::unique_lock<decltype(m_DataMutex)> dlock (m_DataMutex);

ObjectLock olock(this);
m_Frozen = true;

Copy link
Member Author

@Al2Klimov Al2Klimov Apr 1, 2025

Choose a reason for hiding this comment

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

Actually, it IS the same: Register/Freeze

  1. locks a mutex
  2. operates on m_Frozen

It's critical to synchronize them! Imagine:

  1. Register sees not frozen instance
  2. Freeze freezes it
  3. ReadLockUnlessFrozen doesn't lock it
  4. Register modifies it
  5. 🤯

Copy link
Member

Choose a reason for hiding this comment

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

Actually, it IS the same

No, it's not!

It's critical to synchronize them! Imagine:

I don't get it! It's for a reason atomic and if you can't synchronise those without a mutex why make it atomic then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, it IS the same

No, it's not!

We could argue for centuries...

I don't get it! It's for a reason atomic and if you can't synchronise those without a mutex why make it atomic then?

I can't synchronise write ops without a mutex. m_Frozen is atomic to speed up read ops.

@Al2Klimov Al2Klimov requested a review from julianbrost March 28, 2025 11:12
@julianbrost
Copy link
Contributor

@Al2Klimov I'd suggest you to answer the questions in the previous review first before requesting another review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla/signed core/quality Improve code, libraries, algorithms, inline docs enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants