The first commit moves m_requested_hb_cmpctblocks and m_provides_cmpctblocks from CNodeState to Peer and also makes the two fields atomic.
The second commit moves fPreferredDownload to Peer and makes it atomic. Since m_num_preferred_download_peers is a counter based on this field, it can also be atomic. This also lets us drop cs_main usage in a few places. I found this commit harder to reason about, specifically whether the asserts in FinalizeNode were still safe for m_num_preferred_download_peers if another thread could modify the value before/after the m_num_preferred_download_peers -= peer->fPreferredDownload line and trigger the assert. Afaict, these asserts are still safe because ProcessMessage cannot run for the same peer as the peer being cleaned up in FinalizeNode. I am also happy to drop this second commit or explain my reasoning in more detail.
For reference:
m_requested_hb_cmpctblockscan be accessed from:NewPoWValidBlock(read) (b-msghand, http, ipc? threads)ProcessMessage(write) (b-msghand)SendMessages(read) (b-msghand)
m_provides_cmpctblockscan be accessed from:MaybeSetPeerAsAnnouncingHeaderAndIDs(read) (b-msghand, http, ipc?)HeadersDirectFetchBlocks(read) (b-msghand)ProcessMessage(write) (b-msghand)
fPreferredDownloadcan be accessed from:ProcessMessage(read/write) (b-msghand)SendMessages(read) (b-msghand)FinalizeNode(read) (bitcoind, b-net)
m_num_preferred_download_peerscan be accessed from:ProcessMessage(write) (b-msghand)SendMessages(read) (b-msghand)FinalizeNode(read/write) (bitcoind, b-net)