Hi everyone, One of the challenges of the Quantum DOM project is that we will soon have multiple "main" threads [1]. These will be real OS threads, but only one of them will be allowed to run code at any given time. We will switch between them at well-defined points (currently just the JS interrupt callback). This cooperative scheduling will make it much easier to keep our global state consistent. However, having multiple "main" threads is likely to cause confusion.
To simplify things, we considered trying to make these multiple threads "look" like a single main thread at the API level, but it's really hard to hide something like that. So, instead, we're going to be transitioning to APIs that try to avoid exposing threads at all. This post will summarize that effort. You can find more details in this Google doc: https://docs.google.com/document/d/1MZhF1zB5_dk12WRiq4bpmNZUJWmsIt9OTpFUWAlmMyY/edit?usp=sharing [Note: I'd like this thread (and the Google doc) to be for discussing threading APIs. If you have more general questions about the project, please contact me personally.] The main API change is that we're going to make it a lot harder to get hold of an nsIThread for the main thread. Instead, we want people to work with event targets (nsIEventTarget). The advantage of event targets is that all the main threads will share a single event loop, and therefore a single nsIEventTarget. So code that once expected a single main thread can now expect a single main event target. The functions NS_GetMainThread, NS_GetCurrentThread, and do_Get{Main,Current}Thread will be deprecated. In their place, we'll provide mozilla::Get{Main,Current}ThreadEventTarget. These functions will return an event target instead of a thread. More details: NS_IsMainThread: This function will remain pretty much the same. It will return true on any one of the main threads and false elsewhere. Dispatching runnables: NS_DispatchToMainThread will still work, and you will still be able to dispatch using Get{Main,Current}ThreadEventTarget. >From JS, we want people to use Services.tm.dispatchToMainThread. Thread-safety assertions: Code that used PR_GetCurrentThread for thread safety assertions will be converted to use NS_ASSERT_OWNINGTHREAD, which will allow code from different main threads to touch the same object. PR_GetCurrentThread will be deprecated. If you really want to get the current PRThread*, you can use GetCurrentPhysicalThread, which will return a different value for each main thread. Code that uses NS_GetCurrentThread for thread safety assertions will be converted to use nsIEventTarget::IsOnCurrentThread. The main thread event target will return true from IsOnCurrentThread if you're on any of the main threads. Nested event loop spinning: In the future, we want people to use SpinEventLoopUntil to spin a nested event loop. It will do the right thing when called on any of the main threads. We'll provide a similar facility to JS consumers. Bugs: Bug 1359903 converted some of our PR_GetCurrentThread assertions to use NS_ASSERT_OWNINGTHREAD. Bug 1361561 added GetCurrentPhysicalThread and GetCurrentVirtualThread. These functions both return a PRThread*. The latter one returns the same value for all the main threads. It should only be used for assertion checking. Bug 1361164 will add the Get{Current,Main}ThreadEventTarget functions. Bug 1365096 is a metabug to convert all uses of NS_Get{Current,Main}Thread to use event targets instead. Bug 1365097 is the bug for converting DOM code. [1] https://billmccloskey.wordpress.com/2016/10/27/mozillas-quantum-project/ _______________________________________________ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform