Skip to content

Rework SyncToAsync to avoid missed notifications - #15621

Closed
tlively wants to merge 2 commits into
emscripten-core:threadutil2from
tlively:threadutil2
Closed

tlively wants to merge 2 commits into
emscripten-core:threadutil2from
tlively:threadutil2

Conversation

@tlively

@tlively tlively commented Nov 24, 2021 •

Copy link
Copy Markdown
Member
  • Wait for the worker to start up in the constructor to avoid it missing
    messages sent before it starts.

  • Never release the lock in the worker except to wait for work. That prevents
    multiple invokers from trampling each other's work.

  • Wait in invoke for the worker thread to be ready to accept work to handle
    the case where multiple invokers race to submit work.

  • Maintain a count of finished work to avoid situations where an invoker whose
    work is finished is preempted by other invokers, so it wouldn't be able to
    tell that its work is finished.

 - Wait for the worker to startup in the constructor to avoid it missing
   messages sent before it starts.

 - Never release the lock in the worker except to wait for work. That prevents
   multiple invokers from trampling each other's work.

 - Wait in `invoke` for the worker thread to be ready to accept work to handle
   the case where multiple invokers race to submit work.

 - Maintain a count of finished work to avoid situations where an invoker whose
   work is finished is preempted by other invokers, so it wouldn't be able to
   tell that its work is finished.
// stack, and also running any remaining code the caller had, like
// destructors). TODO: add an option to do a synchronous call here in some
// cases, which would avoid the time delay caused by a browser setTimeout.
emscripten_async_call(threadIter, arg, 0);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Holding locks during calls to emscripten_async_call(threadIter, ... is necessary to avoid missing work, but it also seems like a potentially large performance problem since it has to wait for threadIter to be rescheduled and called before the lock is released. Having an invokeSync method that does not rely on a callback for rescheduling could be more efficient because synchronous work could be performed in a loop without returning to the event loop.

Also, for the case of async work, it's unfortunate that multiple invoking threads cannot have async work in flight at once. This could be fixed by having a separate "state mailbox" for each invoking thread, but I'm not sure that's worth doing right now.

void SyncToAsync::invoke(std::function<void(Callback)> newWork) {
// Use the invokeMutex to prevent more than one invoke being in flight at a
// time, so that this is usable from multiple threads safely.
std::lock_guard<std::mutex> invokeLock(invokeMutex);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand your design yet, as wasn't it simpler to have a separate mutex for invokers? That avoids some of the corner cases with a single mutex that can be grabbed by either another invoker or the worker thread, that it looks like your new code handles using workCount etc.

That is, with the invokerMutex we always have exactly a single invoker "in action" at a time. Other attempted invokes simply wait for when they can grab invokerMutex. Then the other mutex is purely for communication between the single active invoker and the worker.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point that the separate invoker mutex avoided the need for workCount. On the other hand, I think that once we have actual async backends we will want to allow multiple async work items to be in flight at once (e.g. to issue separate async reads to separate files simultaneously), which will require multiple async invokers to be waiting at once. I think there is probably some value in having a design now that is similar to what we will need in the future.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting thought. What kind of idea did you have in mind for async work items here?

(For wasmfs I hadn't even thought of that as read/write are sync. But I guess for non-wasmfs stuff it could make sense?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example IDB reads and writes are async and OPFS opens are async (although I don't know the particular details there). If two threads are each trying to open a new OPFS backed file, they shouldn't have to wait for each other even if they proxy those opens to the same background worker thread.

The previous solution ensured that the worker always either held the lock or was
waiting on the condition variable to prevent invokers from overwriting each
other's submitted work. However, invokers waiting for the state to return to
`Waiting` before submitting their work already protects against this failure
mode. As a result, we can simplify the locking strategy to only hold locks when
state needs to be accessed or modified. We won't miss important wake up
notifications because we are always atomically checking the state before
waiting; if we miss a notification, we won't end up waiting at all.

As a result of this change:

 - We no longer need to wait for the worker thread to start up in the constructor.
 - We no longer need `workerLock`

Also make some unrelated fixes:

 - Remove redundant check for `state != Waiting` before waiting in `invoke`.
 - Better handling of overflow of `workCount`
@tlively

tlively commented Nov 24, 2021

Copy link
Copy Markdown
Member Author

I just pushed another big change (hopefully the last one). This makes the locking strategy closer to what the original code was doing and should reduce lock contention compared to my previous version. Copying the commit message here:

The previous solution ensured that the worker always either held the lock or was
waiting on the condition variable to prevent invokers from overwriting each
other's submitted work. However, invokers waiting for the state to return to
Waiting before submitting their work already protects against this failure
mode. As a result, we can simplify the locking strategy to only hold locks when
state needs to be accessed or modified. We won't miss important wake up
notifications because we are always atomically checking the state before
waiting; if we miss a notification, we won't end up waiting at all.

As a result of this change:

  • We no longer need to wait for the worker thread to start up in the constructor.
  • We no longer need workerLock

Also make some unrelated fixes:

  • Remove redundant check for state != Waiting before waiting in invoke.
  • Better handling of overflow of workCount

@tlively

tlively commented Mar 14, 2022

Copy link
Copy Markdown
Member Author

Closing in favor of #16469

@tlively tlively closed this Mar 14, 2022
@tlively
tlively deleted the threadutil2 branch February 5, 2024 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants