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

Updates in synchronization mutex for Shared data #276

Merged
merged 9 commits into from
Jan 24, 2024

Conversation

Naviabheeman
Copy link
Contributor

@Naviabheeman Naviabheeman commented Aug 2, 2023

Changed CCriticalSections to Mutex and RecursiveMutex as in Bitcoin.

Add GUARDED_BY and EXCLUSIVE_LOCK_REQUIRED annotations to some shared data only.
Adding them to all shared data and functions caused lots of build errors on macOS. Clang supports static analysis for thread safety with option -thread-safety-analysis. But this does not seem to be strong enough to identify scope. i.e. when lock is outside a class or a function where the data is accessed. As most of our data and locks are global, and locking is in the outer scope, it causes lots of false positives. Adding more LOCKS in the same scope to pass the compilation warning causes deadlock in our tests.

CCriticalSection cs_totalBytesRecv;
CCriticalSection cs_totalBytesSent;
Mutex cs_totalBytesRecv;
Mutex cs_totalBytesSent;
Copy link
Contributor

Choose a reason for hiding this comment

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

In Bitcoin, RecursiveMutex is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

they have only one mutex. other is changed to atomic

    // Network usage totals
    mutable Mutex m_total_bytes_sent_mutex;
    std::atomic<uint64_t> nTotalBytesRecv{0};
    ```

src/net.h Outdated
CCriticalSection cs_vRecv;
RecursiveMutex cs_vSend;
RecursiveMutex cs_hSocket;
RecursiveMutex cs_vRecv;
Copy link
Contributor

Choose a reason for hiding this comment

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

In Bitcoin, Mutex is used.

std::vector<std::string> vAddedNodes GUARDED_BY(cs_vAddedNodes);
CCriticalSection cs_vAddedNodes;
Mutex cs_vAddedNodes;
Copy link
Contributor

Choose a reason for hiding this comment

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

In Bitcoin, RecursiveMutex is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this remains the same:

mutable Mutex m_added_nodes_mutex;

bool setBannedIsDirty;
bool fAddressesInitialized;
CAddrMan addrman;
std::deque<std::string> vOneShots;
CCriticalSection cs_vOneShots;
Mutex cs_vOneShots;
Copy link
Contributor

Choose a reason for hiding this comment

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

In Bitcoin, RecursiveMutex is used.

Copy link
Contributor Author

@Naviabheeman Naviabheeman Jan 18, 2024

Choose a reason for hiding this comment

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

This is renamed. Bitcoin has reimplemented the peer management layer.

 Mutex m_addr_fetches_mutex;

@@ -530,7 +530,7 @@ struct LocalServiceInfo {
int nPort;
};

extern CCriticalSection cs_mapLocalHost;
extern Mutex cs_mapLocalHost;
Copy link
Contributor

Choose a reason for hiding this comment

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

In Bitcoin, RecursiveMutex is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

extern GlobalMutex g_maplocalhost_mutex;

you mean they say globalmutex which is actually

class GlobalMutex : public Mutex { };

cs_sendProcessing needs to be recursive. otherwise it causes deadlocks
@azuchi azuchi merged commit 0b18f4f into chaintope:master Jan 24, 2024
10 checks passed
@Naviabheeman Naviabheeman deleted the 256_threadDataSynchMutexOnly branch September 17, 2024 10:43
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.

2 participants