Skip to content

Instantly share code, notes, and snippets.

@yakkomajuri
Created January 17, 2022 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yakkomajuri/7d6786baf84ff9735a68cb3720136fa5 to your computer and use it in GitHub Desktop.
Save yakkomajuri/7d6786baf84ff9735a68cb3720136fa5 to your computer and use it in GitHub Desktop.
function atomicsWaitLoop (port : MessagePort, sharedBuffer : Int32Array) {
if (!useAtomics) return;
// This function is entered either after receiving the startup message, or
// when we are done with a task. In those situations, the *only* thing we
// expect to happen next is a 'message' on `port`.
// That call would come with the overhead of a C++ → JS boundary crossing,
// including async tracking. So, instead, if there is no task currently
// running, we wait for a signal from the parent thread using Atomics.wait(),
// and read the message from the port instead of generating an event,
// in order to avoid that overhead.
// The one catch is that this stops asynchronous operations that are still
// running from proceeding. Generally, tasks should not spawn asynchronous
// operations without waiting for them to finish, though.
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment