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

[Question] What is purpose of Phantomdata in LockGuard? (api.rs) #667

Closed
dubuduru opened this issue Oct 11, 2022 · 6 comments
Closed

[Question] What is purpose of Phantomdata in LockGuard? (api.rs) #667

dubuduru opened this issue Oct 11, 2022 · 6 comments
Assignees
Labels
lecture question/discussion related to lectures question Further information is requested

Comments

@dubuduru
Copy link

As far I understood, phantomdata is for mimicking as if the type stores a value of type T.
Why do we need this in struct LockGuard? The type T is *const (), which is !Send + !Sync.
At first I thought it is to make LockGuard not Sendable and Syncable, but I'm not sure. Is it right?

@dubuduru dubuduru added the question Further information is requested label Oct 11, 2022
@tomtomjhj
Copy link
Member

At first I thought it is to make LockGuard not Sendable and Syncable, but I'm not sure. Is it right?

Yes. This enforces that the lock not be unlocked by threads other than the thread that acquired the lock.

@dubuduru
Copy link
Author

dubuduru commented Oct 11, 2022

Thanks. Then why do we implement Send or Sync for LockGuard?
In api.rs line 118-119:
unsafe impl<'s, L: RawLock, T: Send> Send for LockGuard<'s, L, T> {}
unsafe impl<'s, L: RawLock, T: Sync> Sync for LockGuard<'s, L, T> {}

@tomtomjhj
Copy link
Member

hmm that's weird. Sync makes sense, but Send not in general. @jeehoonkang

@Lee-Janggun
Copy link
Member

Lee-Janggun commented Oct 11, 2022

Doesn't Send make sense when T is Send? As I understand it, PhantomData is there to prevent LockGuard being derived as general Send and Sync, and the impl is there to make it available for special cases.

@Lee-Janggun
Copy link
Member

Ok, I did some searching, and this is actually a pretty interesting problem.

  • Rust standard: std::sync::MutexGuard is specifically marked as not Send. In Rust, this was decided as the inner pthread used did not allow locks to be sent across threads. Recently, Rust has improved its synchronization primitives to not depend on pthread on most platforms. Now does this suggest MutexGuard may become Send? Unfortunately, (1) the improved implementations may still be restricted to the same limitations (2) some platforms still depends on pthread, and (3) this it is a breaking change, hence this will most likely never become Send.
  • Our situation: Then since we do not depend on any pthread, can we implement Send? The answer I think is yes. In parking_lock, we can see that it is indeed Send, which is possible because they implement their own locks. As we are also implement our own locks, I think having Send for our locks should be Ok.

@dubuduru
Copy link
Author

Thank you so much! Now I see the point.

@Lee-Janggun Lee-Janggun added the lecture question/discussion related to lectures label Oct 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lecture question/discussion related to lectures question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants