iOS的WebThread

简介: 当用到UIWebView、UITextField、UITextView等类时,就会看到WebThread线程: 在lldb中搜索 (lldb) image lookup -r -s [wW]ebThread会有超过一百个结果。

当用到UIWebView、UITextField、UITextView等类时,就会看到WebThread线程:


在lldb中搜索

(lldb) image lookup -r -s [wW]ebThread
会有超过一百个结果。

对于普通开发者而言,WebThread最有意义的东西恐怕是UIWebView的私有API

- (void)_setDrawInWebThread:(BOOL)drawInWebThread;
设置YES后,会在WebThread进行渲染的某些操作(layoutTiles),不占用主线程。


Mac上的Safari是多进程程序,排版和渲染等是在独立的进程里。iOS不允许多进程,只有多线程,所以在iOS版的WebCore.framework里会有一些代码来把多进程机制适配成多线程机制,WebThread就是主要产物之一。


iOS的WebCore开源码,6.1版本的下载地址为:http://opensource.apple.com/release/ios-61/

比起webkit.org上的Mac开源码,会多了个wak目录,内有21个文件,其中与WebCoreThread相关的有6个。常见的被调用函数有这么一些:

// The lock is automatically freed at the bottom of the runloop. No need to unlock.
// Note that calling this function may hang your UI for several seconds. Don't use
// unless you have to.
void WebThreadLock(void);
    
// This is a no-op for compatibility only. It will go away. Please don't use.
void WebThreadUnlock(void);
    
// Please don't use anything below this line unless you know what you are doing. If unsure, ask.
// ---------------------------------------------------------------------------------------------
bool WebThreadIsLocked(void);
bool WebThreadIsLockedOrDisabled(void);
    
void WebThreadLockPushModal(void);
void WebThreadLockPopModal(void);

void WebThreadEnable(void);
bool WebThreadIsEnabled(void);
bool WebThreadIsCurrent(void);
bool WebThreadNotCurrent(void);
    
// These are for <rdar://problem/6817341> Many apps crashing calling -[UIFieldEditor text] in secondary thread
// Don't use them to solve any random problems you might have.
void WebThreadLockFromAnyThread();
void WebThreadLockFromAnyThreadNoLog();
void WebThreadUnlockFromAnyThread();

//
// Release an object on the main thread.
//
@interface NSObject(WebCoreThreadAdditions)
- (void)releaseOnMainThread;
@end

// Register a class for deallocation on the WebThread
void WebCoreObjCDeallocOnWebThread(Class cls);
void WebCoreObjCDeallocWithWebThreadLock(Class cls);

// Asynchronous from main thread to web thread.
void WebThreadCallAPI(NSInvocation *invocation); /* DEPRECATED; use WebThreadRun() in WebCoreThreadRun.h */
void WebThreadAdoptAndRelease(id obj);

// Synchronous from web thread to main thread, or main thread to main thread.
void WebThreadCallDelegate(NSInvocation *invocation);
void WebThreadRunOnMainThread(void (^)(void));

// Asynchronous from web thread to main thread, but synchronous when called on the main thread.
void WebThreadCallDelegateAsync(NSInvocation *invocation);

// Asynchronous from web thread to main thread, but synchronous when called on the main thread.
void WebThreadPostNotification(NSString *name, id object, id userInfo);

// Convenience method for making an NSInvocation object
NSInvocation *WebThreadMakeNSInvocation(id target, SEL selector);

UIKit只工作在main thread,WebCore工作在WebThread,当产生交集时就需要线程间通信,以上函数就是为了线程间消息服务的,最多的操作当然就是加锁了。具体的发消息实现有几种,RunLoop、GCD、performSelector、pthread都会用,也可谓复杂了。依我看应该是新老技术替换的结果,说不定还是新老员工的不同工作结果。

WebThread是以pthread创建的,有全局变量pthread_t webThread来做引用,在不少地方会有
pthread_equal(pthread_self(), webThread)
的判断。

模态机制用的是CFRunLoop。异步消息多用GCD。

在WebView和Clients间通信时,有两个类作为消息中转,_WebSafeForwarder和_WebSafeAsyncForwarder。


让人纠结的是,iOS的实现是在把单线程的Mac上的WebKit.framework改成多线程,但又没法用类似WebKit2.framework的那种多进程方案。WebKit.framework里诸多的WebHTMLView是会直接访问WebCore的,所以iOS上的WebKit.framework里多了好多的锁。为了避免被developer吐槽,这个框架就干脆不公开了,用UIWebView封装好,只有那几个简单的接口来做门面。

目录
相关文章
|
测试技术
西门子S7-200 SMART自由口通信,如何编写发送程序,使用超级终端发送测试
今天我们学习编写西门子S7-200 SMART自由口通信的发送程序,并使用超级终端进行发送功能测试。首先设置参数,打开STEP7 Micro/WIN SMART编程软件,右键单击项目树中的CPU,选择打开,在打开的系统块中选择CPU类型和信号板类型。设置信号板,串口的类型为RS232,地址为2,波特率为9600。
西门子S7-200 SMART自由口通信,如何编写发送程序,使用超级终端发送测试
|
Web App开发 IDE 前端开发
10款最好用的IDEA插件让我们在2022年生活更美好
我知道你在寻找最好的 Intellij 插件。 瞧,你已经来到了最好的地方了。 在 JAVA 开发人员中广泛使用 Intellij 代码开发工具,无论是 Android 还是原生 JAVA。 有这么多 IDE 进入竞争,但拥有如此庞大的插件市场使 Intellij IDEA 成为最牛逼的赢家。 因此,在这里您将了解到一些精选的最佳 Intellij IDEA 插件。
10款最好用的IDEA插件让我们在2022年生活更美好
|
9月前
|
Dart 索引
鸿蒙应用开发从入门到入行 - 篇8:Tabs选项卡页签视图切换
在本篇文章里,您将掌握使用Tabs选项卡做栏目分类,这是未来应用开发中极为常用的组件
295 7
鸿蒙应用开发从入门到入行 - 篇8:Tabs选项卡页签视图切换
|
9月前
|
数据采集 存储 自然语言处理
魔搭社区每周速递(12.22-12.28)
魔搭ModelScope本期社区进展:1039个模型,128个数据集,63个创新应用,6篇内容。
238 4
|
11月前
|
Web App开发 Java iOS开发
webp详解
WebP是一种由谷歌开发的图像文件格式,旨在提供更高效的图像压缩方法,以加快网页加载速度。它支持有损和无损压缩模式,并且在相同的视觉质量下,相比JPEG和PNG等格式,文件大小更小,从而优化了网络传输效率。此外,WebP还支持透明度和动画图像。
|
弹性计算 固态存储 大数据
云服务器价格多少钱一年?2024年阿里云服务器报价清单曝光!
企业或个人采购云服务器可以选择阿里云,作为国内第一云,阿里云服务器性能和稳定性都有保障,只是在大家都眼里都认为阿里云服务器价格可能会比较贵,事实上普惠上云,阿里云服务器价格其实很优惠,尤其是2024年开始,阿里云服务器又降价,大家可以看下2024年阿里云服务器最新优惠价格,上云就上阿里云!
1288 0
|
存储 前端开发 关系型数据库
基于PHP和MySQL数据库实现的学生成绩管理系统
基于PHP和MySQL数据库实现的学生成绩管理系统
1090 0
基于PHP和MySQL数据库实现的学生成绩管理系统
|
存储 弹性计算 安全
阿里云服务器ECS优势、功能、性能测速及应用场景介绍
阿里云服务器怎么样?什么是云服务器ECS?阿里云服务器怎么使用?阿里云服务器优缺点有哪些?阿里云百科分享阿里云服务器ECS优势、功能、性能测速及应用场景介绍
456 0
阿里云服务器ECS优势、功能、性能测速及应用场景介绍
|
Ubuntu Linux C语言
MSYS2介绍
MSYS2介绍
893 0