1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package androidx.lifecycle
17 
18 import kotlinx.coroutines.Dispatchers
19 import kotlinx.coroutines.runBlocking
20 
21 internal object MainDispatcherChecker {
22     private var isMainDispatcherAvailable: Boolean = true
23     @Volatile private var mainDispatcherThread: Thread? = null
24 
updateMainDispatcherThreadnull25     private fun updateMainDispatcherThread() {
26         try {
27             runBlocking(Dispatchers.Main.immediate) {
28                 mainDispatcherThread = Thread.currentThread()
29             }
30         } catch (_: IllegalStateException) {
31             // No main dispatchers are present in the classpath
32             isMainDispatcherAvailable = false
33         }
34     }
35 
isMainDispatcherThreadnull36     fun isMainDispatcherThread(): Boolean {
37         if (!isMainDispatcherAvailable) {
38             // If we know there's no main dispatcher, assume we're on it.
39             return true
40         }
41 
42         val currentThread = Thread.currentThread()
43         // If the thread has already been retrieved,
44         // we can just check whether we are currently running on the same thread
45         if (currentThread === mainDispatcherThread) {
46             return true
47         }
48 
49         // If the current thread doesn't match the stored main dispatcher thread, is is either:
50         // * The field may not have been initialized yet.
51         // * The Swing Event Dispatch Thread (EDT) may have changed (if applicable).
52         // * We're genuinely not executing on the main thread.
53         // Let's recheck to obtain the most up-to-date dispatcher reference. The recheck can
54         // be time-consuming, but should only occur in less common scenarios.
55         updateMainDispatcherThread()
56 
57         return !isMainDispatcherAvailable || currentThread === mainDispatcherThread
58     }
59 }
60