New zero alloc, no_std compatible implementation#14
Conversation
This implementation relies on the fact that the Thread is cloneable and layout-compatible with a pointer, thus one may just build a waker out of the thread without unnecessary wrapping behind Arcs.
|
Thanks for the PR! @fereidani how much overlap do you think this has with your PR?
Interestingly, I used a very similar approach in this PR, but was unconvinced of the soundness of this approach since
Unfortunately, it is not. Although this happens to work today, on the setup we're using, it is UB and for that reason I wouldn't be happy putting this into a crate used by so many people. That doesn't mean that it's not a potential avenue for exploration, though. |
|
Nice implementation and ideas! |
Thanks a lot for this great crate! I wanted to see if one could make
pollsterwork inno_stdenvironments, since for one of my upcoming async libraries having option to run without standard library is key, and I noticed that there already is a PR getting rid of complex synchronization mechanisms like condition variables in #9.The use of thread_locals to avoid locking was really interesting, but that made pollster even harder to support
no_std, so I took inspiration from that implementation and looked for a way to have the best of all worlds - no dependency onstdoralloc.This implementation relies on the fact that the Thread is cloneable and layout-compatible with a pointer (in fact, it's an Arc), thus one may just build a
Wakerout of the thread without unnecessary wrapping behind Arcs.Is this safe? It depends on you, but my view is that it's safe enough, because:
a) Thread relies on having constant memory location and I see no reason why the layout should change anytime soon.
b) Even if the layout changed, it would not lead to unsafety. Instead, the compiler would refuse to compile the crate with
stdfeature enabled due to sizes inmem::transmutemismatching.But I'm not sure what you think, it's a hack, and it would ideally be nice to work towards support for taking a "thread pointer" and construct a thread handle from that in the standard library, but right now there are no such facilities available, thus it is as good as it gets.