1 of 13

Worker / Worklet Internals

nhiroki@chromium.org

April 19, 2018

PUBLIC

2 of 13

Workers / Worklets Family

  • Workers
    • In-Process Worker
      • Dedicated Worker
    • Out-of-Process Worker
      • Shared Worker
      • Service Worker
  • Worklets
    • Main-Thread Worklet
      • Paint Worklet (shipped in M65, xidachen@) => will be off-main-thread (crbug)
      • Layout Worklet (under development, ikilpatrick@)
    • Off-Main-Thread Worklet
      • Audio Worklet (shipped in M66, hongchan@)
      • Animation Worklet (under development, flackr@)

3 of 13

Worker Process Model

Renderer Process

Document

Dedicated

Worker

Renderer Process

Document

Renderer Process

Shared/ServiceWorker

Browser Process

Document

Backend

In-Process Worker

1 Worker - 1 Document

(Dedicated Worker)

Out-of-Process Worker

1 Worker - multiple Documents

(Shared Worker, Service Worker)

4 of 13

Worklet Process Model

Renderer Process

Document

Worklet

Always in-process

5 of 13

Worker / Worklet Thread Class Layering

WorkerThread

WorkerScheduler

WorkerBackingThread

v8::Isolate

WebThreadImplFor

WorkerScheduler

Worker

base::Thread

NonMainThreadScheduler

blink/renderer/

platform/

scheduler

blink/renderer/

core/workers

base/

WorkerGlobalScope

6 of 13

Worker / Worklet Thread Class Layering

WorkerThread

WorkerScheduler

WorkerBackingThread

v8::Isolate

WebThreadImplFor

WorkerScheduler

WorkerBackingThread

v8::Isolate

WebThreadImplForWorkerScheduler

Worker

Worklet

base::Thread

NonMainThreadScheduler

NonMainThreadScheduler

base::Thread

blink/renderer/

platform/

scheduler

blink/renderer/

core/workers

One backing

thread may host multiple WT/GSs

base/

One backing thread hosts one WT/GS

WorkerGlobalScope

WorkerThread

WorkerScheduler

WorkletGlobalScope

WorkerThread

WorkerScheduler

WorkletGlobalScope

7 of 13

Dedicated Worker start sequence

  • Dedicated workers always start from the main thread
    • japhet@ is now making it possible to start a worker from a worker thread for nested workers

script load

new Worker()

thread

creation

Main

Thread

thread / GS

initialization

script

evaluation

Worker

Thread

8 of 13

Dedicated Worker termination sequence

  • Dedicated workers can be terminated…
    • by Worker::terminate() from the main thread
    • by WorkerGlobalScope::close() from the worker thread
  • Termination sequence always goes through the main thread

worker

termination

terminate()

Main

Thread

prepare for

shutdown

worker shutdown

Worker

Thread

close()

prepare for

shutdown

Stop the scheduler

Destroy the global scope

Stop the scheduler

Destroy the global scope

9 of 13

References

10 of 13

Code locations

  • core/workers
    • Workers / Worklets common implementation + Dedicated Worker + Shared Worker
  • modules/serviceworker/
    • content/{browser,common,renderer}/service_worker
  • modules/csspaint/
  • modules/webaudio/
  • modules/animationworklet/
  • core/layout/custom/

11 of 13

Terminology

  • parent
    • parent document: a document who starts a worker
    • parent worker: a worker who starts a nested wirker
    • parent thread: parent execuion context’s thread

12 of 13

Naming conventions (1)

  • WorkerOrWorklet* prefix
    • Common classes among workers and worklets (e.g., WorkerOrWorkletGlobalScope)
  • Worker* / Worklet* prefix
    • Classes for Workers or Worklets
  • Threaded* prefix
    • Classes for workers and threaded worklets
  • MainThread* prefix
    • Classes for main thread worklets

13 of 13

Naming conventions (2)

  • Thread hopping between the main (parent) thread and a worker thread
    • Main thread side: *MessagingProxy (e.g., DedicatedWorkerMessagingProxy)
    • Worker thread side: *ObjectProxy (e.g., DedicatedWorkerObjectProxy)

Worker

object

Messaging

Proxy

Object

Proxy

Worker

Global

Scope

thread boundary

Main thread

Worker thread