You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
59 lines
1.4 KiB
5 years ago
|
---
|
||
|
怎么同步处理消息?
|
||
|
---
|
||
|
|
||
|
```c++
|
||
|
status_t Client::createSurface(const String&& name, ...){
|
||
|
sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(), name, this, w, h, format, flags, handle, gbp);
|
||
|
mFlinger->postMessageSync(msg);
|
||
|
return static_cast<MessageCreateLayer*>(msg.get())->getResult();
|
||
|
}
|
||
|
status_t postMessageSync(const sp<MessageBase>& msg, ...){
|
||
|
mEventQueue.postMessage(msg, reltime);
|
||
|
msg.wait();
|
||
|
}
|
||
|
class MessageCreateLayer:public MessageBase{
|
||
|
status_t getResult() const{return result;}
|
||
|
virtual bool handler(){
|
||
|
result = flinger->createLayer(name, client,w, h, format, ...);
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
```java
|
||
|
public final boolean runWithScissors(final Runnable r, long timeout){
|
||
|
if(Looper.myLooper()==mLooper){
|
||
|
r.run();
|
||
|
return true;
|
||
|
}
|
||
|
BlockingRunnable br = new BlockingRunnable(r);
|
||
|
return br.postAndWait(this, timeout);
|
||
|
}
|
||
|
private static final class BlockingRunnable implements Runnable{
|
||
|
public BlockingRunnable(Runnable task){
|
||
|
mTask = task;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void run(){
|
||
|
mTask.run();
|
||
|
mDone = true;
|
||
|
notifyAll();
|
||
|
}
|
||
|
}
|
||
|
public boolean postAndWait(Handler handler, ...){
|
||
|
if(!handler.post(this)){
|
||
|
return false;
|
||
|
}
|
||
|
while(!mDone){
|
||
|
wait();
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 总结
|
||
|
|
||
|
1. 同步等待消息的处理
|
||
|
2. Binder 调用统一切换工作线程
|