Tasks & Microtasks

Robin Röper
Node Gem
2 min readFeb 12, 2021

--

V8 7.4.288(node12.0.0) | Part 2 of Deepdive V8

This chapter takes a closer look at both tasks and microtasks.

Runtime picks up task from queue after timer is expired.

Tasks

As mentioned in the introduction a task is something that is placed on the “Task/Callback queue” and will be picked up by the runtime and placed onto the Execution stack. For example, if a function calls setTimeout that will place the function to be executed on the Task/Callback queue once the timer has expired.

Let’s take a look at the output of a concrete example using Node.js

Note: In Node.js setTimeout is implemented using libuv's uv_timer_start

Microtasks

The introduction has some background information about Microtasks. This chapter is going to look into the internals.

Here is an example of running a Microtask with Googletest:

├── lib
│ └── gtest # Output folder
├── test
│ ├── main.cc
│ ├── microtasks_test.cc
│ └── v8_test_fixture.cc
└── Makefile
Clone with git clone git@github.com:robin-rpr/microtask-example.git

The Isolate class exposes a number of functions related to enqueueing and running microtasks:

Lets take a look at what happens when we enqueue a microtask function:

$ lldb -- ./test/microtask_test
(lldb) br s -f microtask_test.cc -l 33
(lldb) r

Isolate::EnqueueMicrotask can be found in src/api/api.cc The actual call will end up in microtask-queue.cc which can be found in src/execution/microtask-queue.cc

NewCallableTask can be found in src/heap/factory.cc.

Back to Chapter Overview

--

--