@@ -183,10 +183,51 @@ pub struct Mutex<T: ?Sized> {
183
183
184
184
// these are the only places where `T: Send` matters; all other
185
185
// functionality works fine on a single thread.
186
+
187
+ /// SAFETY
188
+ ///
189
+ /// The `Send` and `Sync` implementations for `Mutex` ensure that it is safe to
190
+ /// share instances of `Mutex` between threads when the protected data is also
191
+ /// thread-safe. The following explains the safety guarantees:
192
+ ///
193
+ /// - `Send` is implemented for `Mutex<T>` if and only if `T` is also `Send`.
194
+ /// This guarantees that `Mutex<T>` can be safely transferred between threads,
195
+ /// and `T` can be sent across thread boundaries. This is crucial for allowing
196
+ /// safe access to the protected data from multiple threads.
197
+ ///
198
+ /// - `Sync` is implemented for `Mutex<T>` if and only if `T` is both `Send` and
199
+ /// `Sync`. This ensures that `Mutex<T>` can be safely shared between threads
200
+ /// without requiring further synchronization, assuming `T` can be sent across
201
+ /// thread boundaries. It guarantees that multiple threads can safely access the
202
+ /// protected data concurrently without data races.
203
+ ///
204
+ /// It's important to note that `Mutex` can be `Sync` even if its inner type `T`
205
+ /// is not `Sync` itself. This is because `Mutex` provides a safe interface for
206
+ /// accessing `T` through locking mechanisms, ensuring that only one thread can
207
+ /// access `T` at a time.
208
+ ///
186
209
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
187
210
unsafe impl < T : ?Sized + Send > Send for Mutex < T > { }
211
+
212
+ /// SAFETY
213
+ ///
214
+ /// The `Send` and `Sync` implementations for `MutexGuard` ensure that it is
215
+ /// safe to share instances of `MutexGuard` between threads when the protected
216
+ /// data is also thread-safe. The following explains the safety guarantees:
217
+ ///
218
+ /// - `MutexGuard` is not `Send` because it represents exclusive access to the
219
+ /// data protected by `Mutex`, and sending it to another thread could lead to
220
+ /// data races or other unsafe behavior, violating the mutual exclusion property
221
+ /// provided by `Mutex`.
222
+ ///
223
+ /// - `Sync` is implemented for `MutexGuard` if and only if `T` is `Send`. This
224
+ /// ensures that `MutexGuard` can be safely shared between threads if the
225
+ /// protected data can be sent across thread boundaries. It guarantees that
226
+ /// multiple threads can safely access the protected data concurrently without
227
+ /// data races.
228
+ ///
188
229
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
189
- unsafe impl < T : ?Sized + Send > Sync for Mutex < T > { }
230
+ unsafe impl < T : ?Sized + Send > Sync for MutexGuard < T > { }
190
231
191
232
/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
192
233
/// dropped (falls out of scope), the lock will be unlocked.
0 commit comments