This is part of a much larger networking refactor.
After spending a while looking at different approaches for cleaning up the networking code, it's become apparent that some functions/classes are going to have to drop cs_main in favor of using their own fine-grained locks.
Breaking up cs_main would be a monstrous task, and would likely be even harder to review.
As a first step, I've fixed up our locking so that LOCK() and friends play nicely with clang's -Wthread-safety static-analysis option. This will allow us to document locking assumptions while verifying them at the same time. I added https://github.com/bitcoin/bitcoin/commit/a794284e61988a226ea39327449be9906a1b5abd as an example of how this is useful.
My goal is to add the EXCLUSIVE_LOCKS_REQUIRED(cs_main)/GUARDED_BY(cs_main) as needed for functions that need to be broken out of the cs_main lock, that way locking changes can be verified much more easily.
To test:
./configure CXXFLAGS="-O2 -g -Wthread-safety" CXX=clang++ CC=clang
To see an example of a failure in action, comment out LOCK(cs_main); in FinalizeNode(). The result should be something like:
main.cpp:293:5: warning: calling function 'EraseOrphansFor' requires exclusive lock on 'cs_main' [-Wthread-safety-analysis]
EraseOrphansFor(nodeid);