Fix ThreadCachedInt race condition
authorDan Schatzberg <dschatzberg@fb.com>
Thu, 29 Sep 2016 15:22:50 +0000 (08:22 -0700)
committerFacebook Github Bot <facebook-github-bot-bot@fb.com>
Thu, 29 Sep 2016 15:23:59 +0000 (08:23 -0700)
commit6039ee419aa6cf043e010297b29348267ab5be0d
tree8e118cfe39b7888f6cdae357268c37c0d5153e16
parent546ea578cfbf7b4754669a089109378aa5814331
Fix ThreadCachedInt race condition

Summary:
Acquire a SharedMutex at ThreadExit to ensure that after unlinking the ThreadEntry from
the list, future accessAllThreads() won't miss a destroying thread.

This is quite a dangerous fix as it changes some lock ordering semantics. ThreadLocal
elements are now destroyed while holding a lock, so if the destruction function
acquires a different lock, ordering must be consistent with other
uses of accessAllThreads().

I've made accessAllThreads() an opt-in feature via a template parameter and changed
all existing uses. I've also fixed a few lock ordering issues that arose due to this
change.

Reviewed By: andriigrynenko

Differential Revision: D3931072

fbshipit-source-id: 4d464408713184080079698df453b95873bb1a6c
folly/Singleton.cpp
folly/ThreadCachedInt.h
folly/ThreadLocal.h
folly/detail/ThreadLocalDetail.cpp
folly/detail/ThreadLocalDetail.h
folly/test/ThreadCachedIntTest.cpp