What is cs_main
?
cs_main
is a recursive mutex which is used to ensure that validation is carried out in an atomic way. It guards access to validation specific variables (such as CChainState
and CNode
) or mempool variables (in net_processing
). The lock of cs_main
is in validation.cpp.
Why is it called cs_main
?
cs_main
was the mutex ("critical section") that protected data in Satoshi's original main.cpp file. (main.cpp was entirely removed in PR 9260 in December 2016.) In theory cs_main
could be renamed today as main.cpp has since been refactored into smaller files but every time you rename something in a header file, everyone needs to recompile any file that includes that header.
A long-term goal is to separate out those different bits of data that are protected by cs_main
, so that eventually cs_main
only protects the data inside validation.
These were answered by John Newbery and other participants of the May 12th 2021 Bitcoin Core PR review club.