Skip to content

Commit 5170b39

Browse files
phoddieMichael Kellner
authored andcommitted
brief notes on SharedWorker implementation
1 parent e792dfa commit 5170b39

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

documentation/base/worker.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Worker
22
Copyright 2018 Moddable Tech, Inc.
33

4-
Revised: January 14, 2017
4+
Revised: March 19, 2018
55

66
**Warning**: These notes are preliminary. Omissions and errors are likely. If you encounter problems, please ask for assistance.
77

@@ -20,13 +20,6 @@ Those familiar with Web Workers are strongly advised to read this document to un
2020

2121
This document contains a standalone description of the `Worker` class implemented in the Moddable SDK, without reference to the Web Worker specification. The [`worker` example](../../examples/base/worker/) is a simple example of using the `Worker` class.
2222

23-
## Scheduling
24-
Depending on the host runtime, workers may be preemptively scheduled (e.g. run in parallel to each other and the main virtual machine, or they may be cooperatively scheduled (e.g. other virtual machines may only run after the current virtual machine yields). Cooperative scheduling is useful for isolating scripts but does not prevent one virtual machine from blocking another.
25-
26-
The Web Worker specification requires assumes that all Workers are preemptively scheduled. On some microcontrollers, preemptive scheduling is impractical (too much memory required) or nearly impossible (not supported by the host RTOS).
27-
28-
Each host of the Moddable SDK runtime decided whether to support multiple virtual machines. If it does, it then decides whether to support preemptive or cooperative scheduling. The ESP8266 runtime is built on a cooperative task model and so implements cooperative scheduling of virtual machines. The ESP32 is built on FreeRTOS, a preemptively scheduled RTOS, an so supports preemptive scheduling. When deciding whether to use multiple virtual machines in a project, check to see what is supported by the host runtime.
29-
3023
## class Worker
3124
Scripts import the `Worker` class to be able to create a new worker.
3225

@@ -132,6 +125,27 @@ The worker `onmessage` property contains a function which receives messages from
132125
trace(msg, "\n");
133126
}
134127

128+
## Shared Workers
129+
The `SharedWorker` class is an API for working with shared virtual machines. The implementation is based on the [Shared Workers](https://html.spec.whatwg.org/multipage/workers.html#shared-workers-introduction) API from the web with some differences, including:
130+
131+
- The `close` function on the message port is not yet implemented, so Shared Workers cannot be terminated
132+
- Workers are always launched from a module, never from a script file.
133+
134+
135+
## class SharedWorker
136+
Scripts import the `SharedWorker` class to be able to connect to a shared worker, creating the shared worker if it is not currently instantiated.
137+
138+
import {SharedWorker} from "worker";
139+
140+
**Note**: Examples and documentation needed.
141+
142+
## Scheduling
143+
Depending on the host runtime, workers may be preemptively scheduled (e.g. run in parallel to each other and the main virtual machine, or they may be cooperatively scheduled (e.g. other virtual machines may only run after the current virtual machine yields). Cooperative scheduling is useful for isolating scripts but does not prevent one virtual machine from blocking another.
144+
145+
The Web Worker specification requires assumes that all Workers are preemptively scheduled. On some microcontrollers, preemptive scheduling is impractical (too much memory required) or nearly impossible (not supported by the host RTOS).
146+
147+
Each host of the Moddable SDK runtime decided whether to support multiple virtual machines. If it does, it then decides whether to support preemptive or cooperative scheduling. The ESP8266 runtime is built on a cooperative task model and so implements cooperative scheduling of virtual machines. The ESP32 is built on FreeRTOS, a preemptively scheduled RTOS, an so supports preemptive scheduling. When deciding whether to use multiple virtual machines in a project, check to see what is supported by the host runtime.
148+
135149
## xsbug support
136150
The debugger for the XS virtual machine, `xsbug`, supports working with multiple virtual machines simultaneously. Each virtual machine appears in a separate tab with the name of the module path used to initialize the worker.
137151

0 commit comments

Comments
 (0)