File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -429,8 +429,17 @@ status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
429429}
430430
431431size_t ProcessState::getThreadPoolMaxTotalThreadCount () const {
432+ // Need to read `mKernelStartedThreads` before `mThreadPoolStarted` (with
433+ // non-relaxed memory ordering) to avoid a race like the following:
434+ //
435+ // thread A: if (mThreadPoolStarted) { // evaluates false
436+ // thread B: mThreadPoolStarted = true;
437+ // thread B: mKernelStartedThreads++;
438+ // thread A: size_t kernelStarted = mKernelStartedThreads;
439+ // thread A: LOG_ALWAYS_FATAL_IF(kernelStarted != 0, ...);
440+ size_t kernelStarted = mKernelStartedThreads ;
441+
432442 if (mThreadPoolStarted ) {
433- size_t kernelStarted = mKernelStartedThreads ;
434443 size_t max = mMaxThreads ;
435444 size_t current = mCurrentThreads ;
436445
@@ -460,7 +469,6 @@ size_t ProcessState::getThreadPoolMaxTotalThreadCount() const {
460469
461470 // must not be initialized or maybe has poll thread setup, we
462471 // currently don't track this in libbinder
463- size_t kernelStarted = mKernelStartedThreads ;
464472 LOG_ALWAYS_FATAL_IF (kernelStarted != 0 , " Expecting 0 kernel started threads but have %zu" ,
465473 kernelStarted);
466474 return mCurrentThreads ;
Original file line number Diff line number Diff line change @@ -188,7 +188,7 @@ class ProcessState : public virtual RefBase {
188188 Vector<handle_entry> mHandleToObject ;
189189
190190 bool mForked ;
191- bool mThreadPoolStarted ;
191+ std::atomic_bool mThreadPoolStarted ;
192192 volatile int32_t mThreadPoolSeq ;
193193
194194 CallRestriction mCallRestriction ;
You can’t perform that action at this time.
0 commit comments