|
|
|
|
@@ -23,6 +23,8 @@ import kotlinx.coroutines.Runnable
|
|
|
|
|
import java.lang.ref.WeakReference
|
|
|
|
|
import java.util.LinkedList
|
|
|
|
|
import java.util.concurrent.*
|
|
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock
|
|
|
|
|
import kotlin.concurrent.withLock
|
|
|
|
|
import com.mogo.eagle.core.block.runtime.message.Message as Holder
|
|
|
|
|
|
|
|
|
|
@Keep
|
|
|
|
|
@@ -37,14 +39,18 @@ class MainBlockCheck {
|
|
|
|
|
@JvmStatic
|
|
|
|
|
private val tokens = ConcurrentHashMap<Int, WeakReference<Holder>>(128)
|
|
|
|
|
|
|
|
|
|
private val pool by lazy { Executors.newFixedThreadPool(1) }
|
|
|
|
|
private val pool by lazy { Executors.newFixedThreadPool(2) }
|
|
|
|
|
|
|
|
|
|
val asyncHandlers by lazy { CopyOnWriteArraySet<WeakReference<Handler>>() }
|
|
|
|
|
|
|
|
|
|
private val asyncHandlersMap by lazy { ConcurrentHashMap<Int, Boolean>() }
|
|
|
|
|
private val handlersMap by lazy { ConcurrentHashMap<String, Boolean>() }
|
|
|
|
|
|
|
|
|
|
private val whats by lazy { ConcurrentHashMap<Int, LinkedList<WeakReference<Holder>>>() }
|
|
|
|
|
|
|
|
|
|
private val actionPool by lazy { Pools.SimplePool<PoolAction>(500) }
|
|
|
|
|
|
|
|
|
|
private val lock by lazy { ReentrantReadWriteLock() }
|
|
|
|
|
|
|
|
|
|
private data class PoolAction(var type: Int, var what: Int, var holder: Holder?, var elapsedTime: Long = -1, var duration:Long = -1): Runnable {
|
|
|
|
|
|
|
|
|
|
override fun run() {
|
|
|
|
|
@@ -71,7 +77,9 @@ class MainBlockCheck {
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
reset()
|
|
|
|
|
actionPool.release(this)
|
|
|
|
|
lock.writeLock().withLock {
|
|
|
|
|
actionPool.release(this)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -84,9 +92,9 @@ class MainBlockCheck {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val actionPool by lazy { Pools.SynchronizedPool<PoolAction>(500) }
|
|
|
|
|
|
|
|
|
|
private fun getAction(): PoolAction = actionPool.acquire() ?: PoolAction(-1, 0, null)
|
|
|
|
|
|
|
|
|
|
private fun getAction(): PoolAction = lock.readLock().withLock { actionPool.acquire() ?: PoolAction(-1, 0, null) }
|
|
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
|
fun getRecorder(): IMessageRecorder? {
|
|
|
|
|
@@ -126,6 +134,9 @@ class MainBlockCheck {
|
|
|
|
|
h?.dispatchMessage(oldMsg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (t: Throwable) {
|
|
|
|
|
Log.e(TAG, "handler: $h -> ${handlersMap[h.toString()]} -> msg: $enclose", t)
|
|
|
|
|
throw t
|
|
|
|
|
} finally {
|
|
|
|
|
val duration = now() - now
|
|
|
|
|
recycle(enclose, elapsedTime, duration)
|
|
|
|
|
@@ -150,7 +161,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.post(action)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
val msg = Message.obtain(newHandler)
|
|
|
|
|
val what = ObjectHashCodeUtils.getHashCodeIfNeed(newHandler, action)
|
|
|
|
|
@@ -172,7 +183,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.postDelayed(action, delayMillis)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
val msg = Message.obtain(newHandler)
|
|
|
|
|
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
|
|
|
|
|
@@ -194,7 +205,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return HandlerCompat.postDelayed(handler, action, token, delayMillis)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
val msg = Message.obtain(newHandler)
|
|
|
|
|
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
|
|
|
|
|
@@ -221,7 +232,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.postAtFrontOfQueue(action)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
val msg = Message.obtain(newHandler)
|
|
|
|
|
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
|
|
|
|
|
@@ -243,7 +254,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.postAtTime(action, uptimeMillis)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
val msg = Message.obtain(newHandler)
|
|
|
|
|
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
|
|
|
|
|
@@ -266,7 +277,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.postAtTime(action, uptimeMillis)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString() ) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
val msg = Message.obtain(newHandler)
|
|
|
|
|
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
|
|
|
|
|
@@ -294,7 +305,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.sendEmptyMessage(what)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
val msg = Message.obtain(newHandler, what)
|
|
|
|
|
val holder = Holder.acquire(handler, what = what, isAsync = isAsync)
|
|
|
|
|
@@ -314,7 +325,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.sendEmptyMessageAtTime(what, uptimeMillis)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
val msg = Message.obtain(newHandler, what)
|
|
|
|
|
val delta = uptimeMillis - SystemClock.uptimeMillis()
|
|
|
|
|
@@ -324,7 +335,6 @@ class MainBlockCheck {
|
|
|
|
|
if (inserted) {
|
|
|
|
|
insert(what, holder)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return inserted
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -336,7 +346,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.sendEmptyMessageDelayed(what, delayMillis)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
val msg = Message.obtain(newHandler, what)
|
|
|
|
|
val holder = Holder.acquire(handler, what = what, delay = delayMillis, isAsync = isAsync)
|
|
|
|
|
@@ -356,7 +366,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.sendMessage(msg)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
var newMsg = msg
|
|
|
|
|
val action = msg.callback
|
|
|
|
|
@@ -387,7 +397,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.sendMessageAtTime(msg, uptimeMillis)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
var newMsg = msg
|
|
|
|
|
val action = msg.callback
|
|
|
|
|
@@ -419,7 +429,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.sendMessageAtFrontOfQueue(msg)
|
|
|
|
|
}
|
|
|
|
|
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
|
|
|
|
|
val newHandler = getHandler(isAsync)
|
|
|
|
|
var newMsg = msg
|
|
|
|
|
val action = msg.callback
|
|
|
|
|
@@ -502,8 +512,11 @@ class MainBlockCheck {
|
|
|
|
|
return handler.removeCallbacks(action)
|
|
|
|
|
}
|
|
|
|
|
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
|
|
|
|
|
getHandler(asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }).removeMessages(what)
|
|
|
|
|
remove(what)
|
|
|
|
|
try {
|
|
|
|
|
return getHandler(handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }).removeMessages(what)
|
|
|
|
|
} finally {
|
|
|
|
|
remove(what)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
|
@@ -515,7 +528,7 @@ class MainBlockCheck {
|
|
|
|
|
return handler.removeCallbacks(action, token)
|
|
|
|
|
}
|
|
|
|
|
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
|
|
|
|
|
getHandler(asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }).removeMessages(what, if (token != null) { synchronized(
|
|
|
|
|
getHandler(handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }).removeMessages(what, if (token != null) { synchronized(
|
|
|
|
|
tokens
|
|
|
|
|
) { tokens.remove(token.hashCode())?.get() } } else null)
|
|
|
|
|
remove(what)
|
|
|
|
|
@@ -529,7 +542,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.removeMessages(what)
|
|
|
|
|
}
|
|
|
|
|
getHandler(asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }).removeMessages(what)
|
|
|
|
|
getHandler(handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }).removeMessages(what)
|
|
|
|
|
remove(what)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -552,7 +565,7 @@ class MainBlockCheck {
|
|
|
|
|
if (Looper.getMainLooper() != handler.looper) {
|
|
|
|
|
return handler.removeMessages(what, token)
|
|
|
|
|
}
|
|
|
|
|
getHandler(asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }).removeMessages(what, if (token != null) synchronized(
|
|
|
|
|
getHandler(handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }).removeMessages(what, if (token != null) synchronized(
|
|
|
|
|
tokens
|
|
|
|
|
) { tokens.remove(token.hashCode())?.get() } else null)
|
|
|
|
|
remove(what)
|
|
|
|
|
@@ -563,27 +576,27 @@ class MainBlockCheck {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun insert(what: Int, holder: Holder) {
|
|
|
|
|
// val action = getAction()
|
|
|
|
|
// action.type = 0
|
|
|
|
|
// action.what = what
|
|
|
|
|
// action.holder = holder
|
|
|
|
|
// pool.execute(action)
|
|
|
|
|
val action = getAction()
|
|
|
|
|
action.type = 0
|
|
|
|
|
action.what = what
|
|
|
|
|
action.holder = holder
|
|
|
|
|
pool.execute(action)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun remove(what: Int) {
|
|
|
|
|
// val action = getAction()
|
|
|
|
|
// action.type = 1
|
|
|
|
|
// action.what = what
|
|
|
|
|
// pool.execute(action)
|
|
|
|
|
val action = getAction()
|
|
|
|
|
action.type = 1
|
|
|
|
|
action.what = what
|
|
|
|
|
pool.execute(action)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun recycle(holder: Holder, elapsedTime: Long, duration: Long) {
|
|
|
|
|
// val action = getAction()
|
|
|
|
|
// action.type = 2
|
|
|
|
|
// action.holder = holder
|
|
|
|
|
// action.elapsedTime = elapsedTime
|
|
|
|
|
// action.duration = duration
|
|
|
|
|
// pool.execute(action)
|
|
|
|
|
val action = getAction()
|
|
|
|
|
action.type = 2
|
|
|
|
|
action.holder = holder
|
|
|
|
|
action.elapsedTime = elapsedTime
|
|
|
|
|
action.duration = duration
|
|
|
|
|
pool.execute(action)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getHandler(isAsync: Boolean): Handler {
|
|
|
|
|
|