smol-rs / parking Goto Github PK
View Code? Open in Web Editor NEWThread parking and unparking
License: Apache License 2.0
Thread parking and unparking
License: Apache License 2.0
Unparker
is an Arc<Inner>
. If Inner
implements std::task::Wake
, that Arc<Inner>
can be converted into a Waker
without any additional allocations. This would be useful for the implementation of futures_lite::future::block_on
, whose Waker
implementation just calls Unparker::unpark
.
It is unclear from looking through the docs and the readme of this crate what advantages it has over using the standard library std::thread::park()
and std::thread::Thread::unpark()
functions. A cursory glance over the codebase seems to indicate that it uses the same code that the generic thread-parking function uses, which means that it allocates memory for something threads already have built-in. The main advantage from what I can glean is that this algorithm defeats spurious wakeups, which the standard library doesn't guarantee. However, all three implementations already have guards against spurious wakeups.
The caller of Unparker::unpark
briefly has the lock on the Mutex before notifying the condvar. Would it be feasible to pass a Send
value through the Mutex to the unparked thread for it to consume? Perhaps, say, a async_task
runnable? If the threads already unparked or never was to begin with,unpark
can just return back an error containing the value.
In WebAssembly targets, blocking is unimplemented in the standard library except for target_feature = "atomics"
. Therefore blocking cannot happen until that feature is used. But, we can compile conditionally for that feature.
On web browsers, there is an Atomics
API that can be used to block in Web Workers, but not on the main thread. So we could conditionally select that on web (like getrandom
does), and then try to get that API. Then, if we're in a web worker, block, and if we're in the main thread, panic.
We could even have a try_park()
method that allows users to handle the case of if they cannot actually park, because they're on the main thread. This would allow users to handle the case of not parking.
It would be nice to implement this in parking
, that way it can be bubbled up to other primitives in smol
.
See this comment for more info.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.