Dart API Referencedart:isolate

dart:isolate library

The dart:isolate library defines APIs to spawn and communicate with isolates.

All code in dart runs in the context of an isolate. Each isolate has its own heap, which means that all values in memory, including globals, are available only to that isolate. The only mechanism available to communicate between isolates is to pass messages. Messages are sent through ports. This library defines ReceivePort to represent the receiver's end of a communication channel, and SendPort to represent the sender's end.

All isolates start with an initial receive port, which is set in the top-level property port. This port is used to establish the first communication between isolates.

Two APIs are available to spawn a new isolate: spawnFunction and spawnUri. spawnFunction creates a new isolate that is using the same source code as the current isolate, spawnUri allows spawning an isolate that was written independently.

There is currently no way to indicate in the API whether the isolates should run on the same or different threads. The underlying system will schedule the isolate were appropriate. In the near future we will add an API to create DOM isolates. These are isolates that share access to the DOM. All DOM isolates will run on the UI thread.

This library is still evolving. New APIs are being added, and some will be deprecated and removed. In particular, the class Isolate will be deprecated as soon the dartvm has an implementation working for spawnFunction and spawnUri, and we have an API to spawn DOM isolates.

Methods

Code ReceivePort get port() #

The initial ReceivePort available by default for this isolate. This ReceivePort is created automatically and it is commonly used to establish the first communication between isolates (see spawnFunction and spawnUri).

ReceivePort get port() => _port;

Code SendPort spawnFunction(void topLevelFunction()) #

Creates and spawns an isolate that shares the same code as the current isolate, but that starts from topLevelFunction. The topLevelFunction argument must be a static top-level function or a static method that takes no arguments. It is illegal to pass a function closure.

When any isolate starts (even the main script of the application), a default ReceivePort is created for it. This port is available from the top-level getter port defined in this library.

spawnFunction returns a SendPort derived from the child isolate's default port.

See comments at the top of this library for more details.

SendPort spawnFunction(void topLevelFunction()) {
  return _spawnFunction(topLevelFunction);
}

Code SendPort spawnUri(String uri) #

Creates and spawns an isolate whose code is available at uri. Like with spawnFunction, the child isolate will have a default ReceivePort, and a this function returns a SendPort derived from it.

See comments at the top of this library for more details.

SendPort spawnUri(String uri) {
  return _spawnUri(uri);
}

Code void startRootIsolate(entry) #

Wrapper that takes the dart entry point and runs it within an isolate. The dart2js compiler will inject a call of the form startRootIsolate(main); when it determines that this wrapping is needed. For single-isolate applications (e.g. hello world), this call is not emitted.

void startRootIsolate(entry) {
  _globalState = new _Manager();

  // Don't start the main loop again, if we are in a worker.
  if (_globalState.isWorker) return;
  final rootContext = new _IsolateContext();
  _globalState.rootContext = rootContext;
  _fillStatics(rootContext);

  // BUG(5151491): Setting currentContext should not be necessary, but
  // because closures passed to the DOM as event handlers do not bind their
  // isolate automatically we try to give them a reasonable context to live in
  // by having a "default" isolate (the first one created).
  _globalState.currentContext = rootContext;

  rootContext.eval(entry);
  _globalState.topEventLoop.run();
}

Classes

Interfaces

Exceptions