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

fNetworkActive is not protected by a lock, use an atomic #9131

Merged
merged 1 commit into from Nov 16, 2016

Conversation

jonasschnelli
Copy link
Contributor

Reported by @sipa: #8996 (comment)

@paveljanik
Copy link
Contributor

ACK 079142b

@luke-jr
Copy link
Member

luke-jr commented Nov 12, 2016

Is this enough? Does bool actually behave differently as an atomic if we don't use the atomic-specific functions? I would think using .exchange() would be necessary to ensure the logic is itself atomic as well.

@sipa
Copy link
Member

sipa commented Nov 12, 2016

@luke-jr Yes. An assignment to an std::atomic results in a fully sequentially serialized behaviour. You can use .store(value, memory_order_relaxed) for example if you want weaker consistency (but still atomic) behaviour.

@sipa
Copy link
Member

sipa commented Nov 12, 2016

@theuni Can you comment here? You probably understand CConnman's threading model best.

@theuni
Copy link
Member

theuni commented Nov 12, 2016

Sure, making it atomic should solve the problem at hand.

@sipa I avoided commenting on the network active PR because IMO the model needs to change a bit to make it completely safe. I have some of those changes queued up, but I didn't want to stand in the way of the feature. For now, let's just make this atomic.

Ultimately, CConnman::Start and CConnman::Stop need to be used for network activation. They were designed to function that way, it's just not complete yet. The idea is that the network threads stop when the toggle is set, which would guarantee that p2p is really down. What's missing is the ability to bring it back up with the same config.

@rebroad
Copy link
Contributor

rebroad commented Nov 13, 2016

Slightly OT, but when is it best to use locks vs atomics?

@sipa
Copy link
Member

sipa commented Nov 13, 2016

@rebroad Whenever you can use atomics, atomics. They are many times faster than locks.

@sipa
Copy link
Member

sipa commented Nov 15, 2016

utACK 079142b

@laanwj
Copy link
Member

laanwj commented Nov 16, 2016

utACK 079142b

Slightly OT, but when is it best to use locks vs atomics?

This is not the first time that a question you ask is just a google search away. There is literally tons of CS literature about synchronization primitives.
In any case atomic access 'protects' only one variable, whereas locks can protect multiple from concurrent access. As there is nothing else that needs to remain internally consistent at the same time as fNetworkActive changes, an atomic will do.

@laanwj laanwj merged commit 079142b into bitcoin:master Nov 16, 2016
laanwj added a commit that referenced this pull request Nov 16, 2016
079142b fNetworkActive is not protected by a lock, use an atomic (Jonas Schnelli)
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Dec 21, 2016
codablock pushed a commit to codablock/dash that referenced this pull request Jan 15, 2018
… atomic

079142b fNetworkActive is not protected by a lock, use an atomic (Jonas Schnelli)
andvgal pushed a commit to energicryptocurrency/gen2-energi that referenced this pull request Jan 6, 2019
… atomic

079142b fNetworkActive is not protected by a lock, use an atomic (Jonas Schnelli)
CryptoCentric pushed a commit to absolute-community/absolute that referenced this pull request Feb 24, 2019
… atomic

079142b fNetworkActive is not protected by a lock, use an atomic (Jonas Schnelli)
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants