Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous thread notification mechanism. #406

Merged
merged 13 commits into from
Jul 21, 2023
Merged

Asynchronous thread notification mechanism. #406

merged 13 commits into from
Jul 21, 2023

Conversation

cheatfate
Copy link
Collaborator

No description provided.

chronos/threadsync.nim Outdated Show resolved Hide resolved
chronos/threadsync.nim Outdated Show resolved Hide resolved
retFuture

when defined(windows):
proc wait*(signal: ThreadSignalPtr) {.async.} =
Copy link
Member

Choose a reason for hiding this comment

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

hm, what are the calling constraints here for fire and wait? ie which threads can they each be called from?

what happens for example if I wait from multiple threads?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

All the cases of calling wait() and fire() are now in tests. You can safely call wait() from multiple threads.
So if you have N threads called wait() on single ThreadSignalPtr instance, single call to fire() will awaken only 1 thread.
And you can call both wait and fire from same thread, you just need to avoid deadlock.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So for any number callers to fire()/fireSync() from single or many threads and for any number of waiters via wait()/waitSync() for single or many thread, every call to fire()/fireSync() will wake up only one waiter via wait()/waitSync(), but ThreadSignal is not a semaphore so there could be only one pending fire() signal until waiter will not receive it.

else:
cint(signal[].rfd)

if checkBusy(checkFd):
Copy link
Member

Choose a reason for hiding this comment

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

is it thread-safe to skip the writing here? if another thread reads just after this check, it might reset the conditions for the "writer" to fire again

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ThreadSignalPtr is supposed to wake only one waiter after one fire. To achieve this we should keep only one fire entity. Its pretty easy to make with eventfd on Linux and with Event on Windows, but sockets are not good at it, so we perform this check to avoid situation when we put 2 events into the sockets.

Copy link
Member

@arnetheduck arnetheduck left a comment

Choose a reason for hiding this comment

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

Tried integrating this both with a channel and with nim-taskpools - works fine 👍

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