voidcheckThread(){ if (mThread != Thread.currentThread()) { thrownew CalledFromWrongThreadException( "Only the original thread that created a view hierarchy can touch its views."); } }
privatestaticvoidprepare(boolean quitAllowed){ if (sThreadLocal.get() != null) { thrownew RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); //创建 Looper对象并设置线程私有变量中 }
void Looper::rebuildEpollLocked() { ... // Allocate the new epoll instance and register the wake pipe. mEpollFd.reset(epoll_create1(EPOLL_CLOEXEC)); //1. 创建一个 epoll fd ...
structepoll_eventeventItem;// 构造一个监听事件描述 memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union eventItem.events = EPOLLIN; //监听读入,也就是当管道中有内容时,唤醒去读取 eventItem.data.fd = mWakeEventFd.get(); int result = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, mWakeEventFd.get(), &eventItem); // 2. 将监听的wakefd 加入到 epoll fd 中,并且监听读入事件 ...
Message next(){ ... finallong ptr = mPtr; ... //刚开始迭代时初始化参数 int pendingIdleHandlerCount = -1; // -1 only during first iteration int nextPollTimeoutMillis = 0; //首次默认不等待 for (;;) { ...
synchronized (this) { // Try to retrieve the next message. Return if found. finallong now = SystemClock.uptimeMillis(); Message prevMsg = null; Message msg = mMessages; if (msg != null && msg.target == null) { // Stalled by a barrier. Find the next asynchronous message in the queue. do { prevMsg = msg; msg = msg.next; } while (msg != null && !msg.isAsynchronous()); } if (msg != null) {// 当消息队列不为空时 if (now < msg.when) {//若是延迟消息,则计算还要等待的最大timeout时间 // Next message is not ready. Set a timeout to wake up when it is ready. nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE); } else {//消息队列不为空,且消息执行时间符合,开始获取一条消息 // Got a message. mBlocked = false; //将阻塞变量设为false //基于链表操作,获取消息队列的首条消息 if (prevMsg != null) { prevMsg.next = msg.next; } else { mMessages = msg.next; } msg.next = null; if (DEBUG) Log.v(TAG, "Returning message: " + msg); msg.markInUse(); return msg; //立即 return, next()就直接返回了 } } else {//如果消息队列为空,说明当前无任何消息,设置无限等待标志(-1) // No more messages. nextPollTimeoutMillis = -1; }
// Process the quit message now that all pending messages have been handled. if (mQuitting) { dispose(); returnnull; }
// 逻辑能走到这里,说明消息队列为空,或者首个消息队列还没到时间执行时间,那么就趁着这个完全空闲的执行,执行空闲的操作 // If first time idle, then get the number of idlers to run. // Idle handles only run if the queue is empty or if the first message // in the queue (possibly a barrier) is due to be handled in the future. //若消息队列为空或者首条消息未到执行时间,获取 idlerHandler的大小 if (pendingIdleHandlerCount < 0 && (mMessages == null || now < mMessages.when)) { pendingIdleHandlerCount = mIdleHandlers.size(); } //若此时未有pengdingIdleHanlder的任务,说明没有空闲执行的任务,那就直接等下一条消息到来 if (pendingIdleHandlerCount <= 0) { // No idle handlers to run. Loop and wait some more. mBlocked = true; //阻塞设置为 true continue; // 直接下一轮,实际是直接应用 nextPollTimeoutMillis 来做等待 }
//下面是有pending的空闲任务,则逐一回调执行, if (mPendingIdleHandlers == null) { mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)]; } mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers); }
// Run the idle handlers. // We only ever reach this code block during the first iteration. for (int i = 0; i < pendingIdleHandlerCount; i++) { final IdleHandler idler = mPendingIdleHandlers[i]; mPendingIdleHandlers[i] = null; // release the reference to the handler
if (!keep) { synchronized (this) { mIdleHandlers.remove(idler); } } } // pending 任务做完了,立即设置 空闲任务为0,且等待时间为0,因为可能现在消息队列可能有消息了,直接查看 // Reset the idle handler count to 0 so we do not run them again. pendingIdleHandlerCount = 0;
// While calling an idle handler, a new message could have been delivered // so go back and look again for a pending message without waiting. nextPollTimeoutMillis = 0; } }
booleanenqueueMessage(Message msg, long when){ ...//异常处理 synchronized (this) { ...//异常处理 msg.markInUse(); msg.when = when; Message p = mMessages;//获取当前消息队列 boolean needWake; //是否要唤醒消息队列 if (p == null || when == 0 || when < p.when) { // 当消息队列为空,或者当前消息需要立即执行,该msg为首条消息,且唤醒 // New head, wake up the event queue if blocked. msg.next = p; mMessages = msg; needWake = mBlocked; //设置阻塞唤醒 } else { //正常插入消息到队列的常规case // Inserted within the middle of the queue. Usually we don't have to wake // up the event queue unless there is a barrier at the head of the queue // and the message is the earliest asynchronous message in the queue. needWake = mBlocked && p.target == null && msg.isAsynchronous(); Message prev; for (;;) {// 按照msg执行时间插入到对应的位置 prev = p; p = p.next; if (p == null || when < p.when) { break; } if (needWake && p.isAsynchronous()) { needWake = false; } } msg.next = p; // invariant: p == prev.next prev.next = msg; }
// We can assume mPtr != 0 because mQuitting is false. if (needWake) {// 假如需要唤醒,就立即唤醒 nativeWake(mPtr); } } returntrue; }