-
Notifications
You must be signed in to change notification settings - Fork 7
Questions/Improvement suggestions for scoped thread implementation #29
Comments
First of all, thank you so much for your detailed and thoughtful comments! I think we can discuss several aspects of Regarding 1 and 2: I think we want methods to use Regarding 3: suppose code inside a Regarding 4, Regarding 5, I'd like to hear your opinion in more details! Could you send us a PR? |
While
The problem with generic types is that they can be I've been bitten by this case of UB in
I'm also interested in how you'd rearrange the modules. This is our current module structure: |
@stjepang wow, I didn't know that... Yes, in that case, we should definitely remove calls to |
@jeehoonkang I've only seen it explained here: https://github.com/canndrew/rfcs/blob/uninitialized-uninhabited/text/0000-uninitialized-uninhabited.md#motivation |
@jeehoonkang and @stjepang First off, thanks to both of you for your clarifying comments! Regarding 1) and 2) the argument regarding flexibility through interior mutability makes sense to me, but it should be possible to simple use Regarding 4) I have fiddled around a little and think I might have found a solution does not require any arcane stuff (note that I am not 100% sure yet if this is totally sound, but compiles and apparently works):
This is just an unsafe wrapper around a raw pointer from box that implements Send and Sync in order to smuggle it across the thread boundary of a scoped thread. This is obviously totally unsound in any other context (or rather most) but scoped threads. The Regarding 5) I will post my ideas later, though I should note that this is pretty much a question of personal preference that I prefer to split code into a lot of submodules in order to keep files focused and readable (especially with doc comments I find .rs files become bloated very quickly). |
|
@oliver-giersch Can we close this issue? Is there anything here that we haven't resolved yet? |
As far as I'm concerned thus can be closed. |
As mentioned in #27 I had a few questions about the scoped thread code, which could possibly also be suggestions for improvements, but maybe there are some subtleties involved that I don't fully understand.
The Scope struct uses a somewhat strange custom linked list for storing the boxed destructors/deferred closures. I am not sure why this is, I've tried replacing it with a simple
Vec
as well asstd::collections::LinkedList
and both solutions worked equally well, although I did not do any in-depth testing and panicking destructors might have unexpected side effectsThe linked list is wrapped in a RefCell like so:
dtors: RefCell<Option<DtorChain<'a, ()>>>
.When I tried replacing the
DtorChain
with a regularVec
orLinkedList
I also removed theRefCell
and changed all relevant&Scope
and&self
references to mutable ones and there were no aliasing problems, so I am not sure why theRefCell
wrapping exists.Scope implements the Drop trait. Since
drop_all()
is called manually before the end of the scope function, implementing Drop should not be necessary. Also, the signature for drop_all could be changed tofn drop_all(self)
.The implementation uses a fairly arcane procedure to allocate a
Box
withstd::mem::ManuallyDrop
andstd::mem::unitialized
and casts a raw pointer into anusize
in order to smuggle it across thread boundaries and into the twoScopedJoinHandle
s.I thought there must be a better and more readable/understandable way to express this, but haven't figured one out yet.
The stdlib uses a
Arc<UnsafeCell<Option<T>>
which is smuggled across thread boundaries.Not sure if using an Arc might have a negative impact on performance, but using
Option<T>
instead ofmem::uninitialized()
would be preferable I believe.This is purely my own opinion but I believe the scoped thread module could benefit from a few rearrangements here and there, keeping structs and their impls together (
ScopedJoinHandle
is all over the place for instance) and maybe putting some things into submodules.The text was updated successfully, but these errors were encountered: