1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
#![feature(negative_impls)]
#![allow(clippy::comparison_chain)]

use core::cell::UnsafeCell;
use core::ops::{Deref, DerefMut};
use core::sync::atomic::{spin_loop_hint as spin_loop, AtomicU8, AtomicUsize, Ordering::SeqCst};

use std::io::{self, BufRead, Read, Seek, SeekFrom, Write};
use std::sync::{
    LockResult, PoisonError,
    TryLockError::{Poisoned, WouldBlock},
    TryLockResult,
};
use std::thread::panicking;

#[cfg(debug_assertions)]
use std::sync::atomic::Ordering::Relaxed;
#[cfg(debug_assertions)]
use thread_local::ThreadLocal;

// sentinel most significant bit,write lock
const WRITE: usize = usize::MAX - (usize::MAX / 2);
// sentinel pen-most significant bit,poisoned state
const POISON: usize = WRITE / 2;

/// An allocation-free [`RwLock`][`std::sync::RwLock`] made in pure Rust.
pub struct RwLock<T> {
    value: UnsafeCell<T>,
    ref_count: AtomicUsize,
    #[cfg(debug_assertions)]
    local_ref_count: ThreadLocal<AtomicUsize>,
}

unsafe impl<T: Send> Send for RwLock<T> {}
unsafe impl<T: Send> Sync for RwLock<T> {}

impl<T> RwLock<T> {
    /// Creates a new `RwLock` locking the value `x`.
    ///
    /// This function it's constant only without `debug_assertions` due to declare a thread local
    /// storage to avoid acquire the lock twice from the same thread.
    #[inline]
    #[cfg(not(debug_assertions))]
    pub const fn new(x: T) -> Self {
        Self {
            value: UnsafeCell::new(x),
            ref_count: AtomicUsize::new(0),
        }
    }

    /// Creates a new `RwLock` locking the value `x`.
    ///
    /// This function it's constant only without `debug_assertions` due to declare a thread local
    /// storage to avoid acquire the lock twice from the same thread.
    #[inline]
    #[cfg(debug_assertions)]
    pub fn new(x: T) -> Self {
        Self {
            value: UnsafeCell::new(x),
            ref_count: AtomicUsize::new(0),
            local_ref_count: ThreadLocal::new(),
        }
    }

    /// Locks this rwlock with shared read access, blocking the current thread
    /// until it can be acquired.
    ///
    /// The calling thread will be blocked until there are no more writers which
    /// hold the lock. There may be other readers currently inside the lock when
    /// this method returns. This method does not provide any guarantees with
    /// respect to the ordering of whether contentious readers or writers will
    /// acquire the lock first.
    ///
    /// Returns an RAII guard which will release this thread's shared access
    /// once it is dropped.
    ///
    /// # Errors
    ///
    /// This function will return an error if the `RwLock` is poisoned. An `RwLock`
    /// is poisoned whenever a writer panics while holding an exclusive lock.
    /// The failure will occur immediately after the lock has been acquired.
    ///
    /// # Panics
    ///
    /// This function panic when called if the lock is already held by the current thread with `debug_assertions` on.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use std::thread;
    /// use sync_2::RwLock;
    ///
    /// let lock = Arc::new(RwLock::new(1));
    /// let c_lock = lock.clone();
    ///
    /// let n = lock.read().unwrap();
    /// assert_eq!(*n, 1);
    ///
    /// thread::spawn(move || {
    ///     let r = c_lock.read();
    ///     assert!(r.is_ok());
    /// }).join().unwrap();
    /// ```
    #[inline]
    pub fn read(&self) -> LockResult<SharedGuard<'_, T>> {
        #[cfg(debug_assertions)]
        let x = self.local_ref_count.get_or(|| AtomicUsize::new(0));

        #[cfg(debug_assertions)]
        if x.load(Relaxed) >= WRITE {
            panic!("rwlock read lock would result in deadlock")
        }

        #[cfg(debug_assertions)]
        x.fetch_add(1, Relaxed);

        loop {
            match self.try_read() {
                Ok(e) => break Ok(e),
                Err(WouldBlock) => spin_loop(),
                Err(Poisoned(e)) => break Err(e),
            }
        }
    }

    /// Locks this rwlock with exclusive write access, blocking the current
    /// thread until it can be acquired.
    ///
    /// This function will not return while other writers or other readers
    /// currently have access to the lock.
    ///
    /// Returns an RAII guard which will drop the write access of this rwlock
    /// when dropped.
    ///
    /// # Errors
    ///
    /// This function will return an error if the `RwLock` is poisoned. An `RwLock`
    /// is poisoned whenever a writer panics while holding an exclusive lock.
    /// An error will be returned when the lock is acquired.
    ///
    /// # Panics
    ///
    /// This function panic when called if the lock is already held by the current thread with `debug_assertions` on.
    ///
    /// # Examples
    ///
    /// ```
    /// use sync_2::RwLock;
    ///
    /// let lock = RwLock::new(1);
    ///
    /// let mut n = lock.write().unwrap();
    /// *n = 2;
    ///
    /// assert!(lock.try_read().is_err());
    /// ```
    #[inline]
    pub fn write(&self) -> LockResult<UniqueGuard<'_, T>> {
        #[cfg(debug_assertions)]
        let x = self.local_ref_count.get_or(|| AtomicUsize::new(0));

        #[cfg(debug_assertions)]
        if x.load(Relaxed) != 0 {
            panic!("rwlock read lock would result in deadlock")
        }

        #[cfg(debug_assertions)]
        x.fetch_or(WRITE, Relaxed);

        loop {
            match self.try_write() {
                Ok(e) => break Ok(e),
                Err(WouldBlock) => spin_loop(),
                Err(Poisoned(e)) => break Err(e),
            }
        }
    }

    /// Attempts to acquire this rwlock with shared read access.
    ///
    /// If the access could not be granted at this time, then `Err` is returned.
    /// Otherwise, an RAII guard is returned which will release the shared access
    /// when it is dropped.
    ///
    /// This function does not block.
    ///
    /// This function does not provide any guarantees with respect to the ordering
    /// of whether contentious readers or writers will acquire the lock first.
    ///
    /// # Errors
    ///
    /// This function will return an error if the `RwLock` is poisoned. An `RwLock`
    /// is poisoned whenever a writer panics while holding an exclusive lock. An
    /// error will only be returned if the lock would have otherwise been
    /// acquired.
    ///
    /// # Examples
    ///
    /// ```
    /// use sync_2::RwLock;
    ///
    /// let lock = RwLock::new(1);
    ///
    /// match lock.try_read() {
    ///     Ok(n) => assert_eq!(*n, 1),
    ///     Err(_) => unreachable!(),
    /// };
    /// ```
    pub fn try_read(&self) -> TryLockResult<SharedGuard<'_, T>> {
        let x = match self.ref_count.fetch_update(
            SeqCst,
            SeqCst,
            |x| if x < WRITE { Some(x + 1) } else { None },
        ) {
            Ok(x) => x,
            Err(_) => return Err(WouldBlock),
        };

        // WRITE it's greater than poison,once we know our value it's less than WRITE it is ok to not filter bits as in `is_poisoned`
        if x < POISON {
            Ok(SharedGuard { lock: self })
        } else {
            Err(Poisoned(PoisonError::new(SharedGuard { lock: self })))
        }
    }

    /// Attempts to lock this rwlock with exclusive write access.
    ///
    /// If the lock could not be acquired at this time, then `Err` is returned.
    /// Otherwise, an RAII guard is returned which will release the lock when
    /// it is dropped.
    ///
    /// This function does not block.
    ///
    /// This function does not provide any guarantees with respect to the ordering
    /// of whether contentious readers or writers will acquire the lock first.
    ///
    /// # Errors
    ///
    /// This function will return an error if the `RwLock` is poisoned. An `RwLock`
    /// is poisoned whenever a writer panics while holding an exclusive lock. An
    /// error will only be returned if the lock would have otherwise been
    /// acquired.
    ///
    /// # Examples
    ///
    /// ```
    /// use sync_2::RwLock;
    ///
    /// let lock = RwLock::new(1);
    ///
    /// let n = lock.read().unwrap();
    /// assert_eq!(*n, 1);
    ///
    /// println!("{:?}", lock);
    /// assert!(lock.try_write().is_err());
    /// ```
    pub fn try_write(&self) -> TryLockResult<UniqueGuard<'_, T>> {
        let mut clean = true;

        match self.ref_count.fetch_update(
            SeqCst,
            SeqCst,
            |x| {
                if x == 0 || {
                    clean = false;
                    x == POISON
                } {
                    Some(x | WRITE)
                } else {
                    None
                }
            },
        ) {
            Ok(x) => x,
            Err(_) => return Err(WouldBlock),
        };

        if clean {
            Ok(UniqueGuard { lock: self })
        } else {
            Err(Poisoned(PoisonError::new(UniqueGuard { lock: self })))
        }
    }

    /// Determines whether the lock is poisoned.
    ///
    /// If another thread is active, the lock can still become poisoned at any
    /// time. You should not trust a `false` value for program correctness
    /// without additional synchronization.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use std::thread;
    /// use sync_2::RwLock;
    ///
    /// let lock = Arc::new(RwLock::new(0));
    /// let c_lock = lock.clone();
    ///
    /// let _ = thread::spawn(move || {
    ///     let _lock = c_lock.write().unwrap();
    ///     panic!(); // the lock gets poisoned
    /// }).join();
    /// assert_eq!(lock.is_poisoned(), true);
    /// ```
    #[inline]
    pub fn is_poisoned(&self) -> bool {
        (self.ref_count.load(SeqCst) & POISON) == POISON
    }

    /// Returns a mutable reference to the underlying data.
    ///
    /// Since this call borrows the `RwLock` mutably, no actual locking needs to
    /// take place -- the mutable borrow statically guarantees no locks exist.
    ///
    /// # Errors
    ///
    /// This function will return an error if the `RwLock` is poisoned. An `RwLock`
    /// is poisoned whenever a writer panics while holding an exclusive lock. An
    /// error will only be returned if the lock would have otherwise been
    /// acquired.
    ///
    /// # Examples
    ///
    /// ```
    /// use sync_2::RwLock;
    ///
    /// let mut lock = RwLock::new(0);
    /// *lock.get_mut().unwrap() = 10;
    /// assert_eq!(*lock.read().unwrap(), 10);
    /// ```
    #[inline]
    pub fn get_mut(&mut self) -> LockResult<&mut T> {
        let x = unsafe { &mut *self.value.get() };

        if self.is_poisoned() {
            Err(PoisonError::new(x))
        } else {
            Ok(x)
        }
    }

    /// Consumes this `RwLock`, returning the underlying data.
    ///
    /// # Errors
    ///
    /// This function will return an error if the `RwLock` is poisoned. An `RwLock`
    /// is poisoned whenever a writer panics while holding an exclusive lock. An
    /// error will only be returned if the lock would have otherwise been
    /// acquired.
    ///
    /// # Examples
    ///
    /// ```
    /// use sync_2::RwLock;
    ///
    /// let lock = RwLock::new(String::new());
    /// {
    ///     let mut s = lock.write().unwrap();
    ///     *s = "modified".to_owned();
    /// }
    /// assert_eq!(lock.into_inner().unwrap(), "modified");
    /// ```
    #[inline]
    pub fn into_inner(self) -> LockResult<T> {
        if self.is_poisoned() {
            Err(PoisonError::new(self.value.into_inner()))
        } else {
            Ok(self.value.into_inner())
        }
    }
}

use core::fmt;

impl<T: fmt::Debug> fmt::Debug for RwLock<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.try_read() {
            Ok(guard) => f.debug_struct("RwLock").field("data", &&*guard).finish(),
            Err(Poisoned(err)) => f
                .debug_struct("RwLock")
                .field("data", &&**err.get_ref())
                .finish(),
            Err(WouldBlock) => {
                struct LockedPlaceholder;
                impl fmt::Debug for LockedPlaceholder {
                    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                        f.write_str("<locked>")
                    }
                }

                f.debug_struct("RwLock")
                    .field("data", &LockedPlaceholder)
                    .finish()
            }
        }
    }
}

impl<T: Default> Default for RwLock<T> {
    /// Creates a new `RwLock<T>`, with the `Default` value for T.
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T> From<T> for RwLock<T> {
    /// Creates a new instance of an `RwLock<T>` which is unlocked.
    /// This is equivalent to [`RwLock::new`].
    ///
    /// [`RwLock::new`]: #method.new
    fn from(t: T) -> Self {
        Self::new(t)
    }
}

/// Read-only shared guard created by the methods [`RwLock::read`] and [`RwLock::try_read`].
/// 
/// Leaking this struct would cause deadlock with any further call to [`RwLock::write`].
pub struct SharedGuard<'a, T> {
    lock: &'a RwLock<T>,
}

impl<T: fmt::Debug> fmt::Debug for SharedGuard<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RwLockReadGuard")
            .field("lock", self.lock)
            .finish()
    }
}

impl<T: fmt::Display> fmt::Display for SharedGuard<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        (**self).fmt(f)
    }
}

impl<T: fmt::Debug> fmt::Debug for UniqueGuard<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RwLockWriteGuard")
            .field("lock", self.lock)
            .finish()
    }
}

impl<T: fmt::Display> fmt::Display for UniqueGuard<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        (**self).fmt(f)
    }
}

impl<'a, T> Drop for SharedGuard<'a, T> {
    #[inline]
    fn drop(&mut self) {
        self.lock.ref_count.fetch_sub(1, SeqCst);
        #[cfg(debug_assertions)]
        self.lock
            .local_ref_count
            .get_or(|| AtomicUsize::new(0))
            .fetch_sub(1, Relaxed);
    }
}

impl<'a, T> Deref for SharedGuard<'a, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe { &*self.lock.value.get() }
    }
}

impl<T> !Send for SharedGuard<'_, T> {}
unsafe impl<T: Sync> Sync for SharedGuard<'_, T> {}

impl<T> !Send for UniqueGuard<'_, T> {}
unsafe impl<T: Sync> Sync for UniqueGuard<'_, T> {}

/// Unique guard created by the methods [`RwLock::write`] and [`RwLock::try_write`].
/// 
/// Leaking this struct would cause deadlock with any further call to [`RwLock::write`] or [`RwLock::read`].
pub struct UniqueGuard<'a, T> {
    lock: &'a RwLock<T>,
}

impl<'a, T> Deref for UniqueGuard<'a, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe { &*self.lock.value.get() }
    }
}

impl<'a, T> DerefMut for UniqueGuard<'a, T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *self.lock.value.get() }
    }
}

impl<'a, T> Drop for UniqueGuard<'a, T> {
    #[inline]
    fn drop(&mut self) {
        if panicking() {
            self.lock.ref_count.fetch_or(POISON, SeqCst);
        }
        self.lock.ref_count.fetch_xor(WRITE, SeqCst);
        #[cfg(debug_assertions)]
        self.lock
            .local_ref_count
            .get_or(|| AtomicUsize::new(0))
            .fetch_xor(WRITE, Relaxed);
    }
}

impl<'a, T: Seek> Seek for UniqueGuard<'a, T> {
    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
        (**self).seek(pos)
    }
}

impl<'a, T: Write> Write for UniqueGuard<'a, T> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        (**self).write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        (**self).flush()
    }
}

impl<'a, T: Read> Read for UniqueGuard<'a, T> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        (**self).read(buf)
    }
}

impl<'a, T: BufRead> BufRead for UniqueGuard<'a, T> {
    fn fill_buf(&mut self) -> io::Result<&[u8]> {
        (**self).fill_buf()
    }

    fn consume(&mut self, amt: usize) {
        (**self).consume(amt)
    }
}

const LOCK: u8 = 2;
const POISON_M: u8 = 1;

/// An allocation-free [`Mutex`][`std::sync::Mutex`] made in pure Rust.
pub struct Mutex<T> {
    value: UnsafeCell<T>,
    ref_count: AtomicU8,
    // local ref_count used to detect deadlock whenever you try to acquire a lock that was blocked on the current thread on debug
    #[cfg(debug_assertions)]
    local_ref_count: ThreadLocal<AtomicU8>,
}

unsafe impl<T: Send> Send for Mutex<T> {}
unsafe impl<T: Send> Sync for Mutex<T> {}

impl<T> Mutex<T> {
    /// Creates a new mutex in an unlocked state ready for use.
    ///
    /// This function it's constant only without `debug_assertions` due to declare a thread local
    /// storage to avoid acquire the lock twice from the same thread.
    #[inline]
    #[cfg(not(debug_assertions))]
    pub const fn new(x: T) -> Self {
        Self {
            value: UnsafeCell::new(x),
            ref_count: AtomicU8::new(0),
        }
    }

    /// Creates a new mutex in an unlocked state ready for use.
    ///
    /// This function it's constant only without `debug_assertions` due to declare a thread local
    /// storage to avoid acquire the lock twice from the same thread.
    #[inline]
    #[cfg(debug_assertions)]
    pub fn new(x: T) -> Self {
        Self {
            value: UnsafeCell::new(x),
            ref_count: AtomicU8::new(0),
            local_ref_count: ThreadLocal::new(),
        }
    }

    /// Acquires a mutex, blocking the current thread until it is able to do so.
    ///
    /// This function will block the local thread until it is available to acquire
    /// the mutex. Upon returning, the thread is the only thread with the lock
    /// held. An RAII guard is returned to allow scoped unlock of the lock. When
    /// the guard goes out of scope, the mutex will be unlocked.
    ///
    /// The exact behavior on locking a mutex in the thread which already holds
    /// the lock is left unspecified. However, this function will not return on
    /// the second call (it might panic or deadlock, for example).
    ///
    /// # Errors
    ///
    /// If another user of this mutex panicked while holding the mutex, then
    /// this call will return an error once the mutex is acquired.
    ///
    /// # Panics
    ///
    /// This function might panic when called if the lock is already held by
    /// the current thread only with `debug_assertions`.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use sync_2::Mutex;
    /// use std::thread;
    ///
    /// let mutex = Arc::new(Mutex::new(0));
    /// let c_mutex = mutex.clone();
    ///
    /// thread::spawn(move || {
    ///     *c_mutex.lock().unwrap() = 10;
    /// }).join().expect("thread::spawn failed");
    /// assert_eq!(*mutex.lock().unwrap(), 10);
    /// ```
    #[inline]
    pub fn lock(&self) -> LockResult<MutexGuard<'_, T>> {
        #[cfg(debug_assertions)]
        if self
            .local_ref_count
            .get_or(|| AtomicU8::new(0))
            .fetch_or(LOCK, Relaxed)
            == LOCK
        {
            panic!("mutex lock would result in deadlock")
        }

        loop {
            match self.try_lock() {
                Ok(e) => break Ok(e),
                Err(WouldBlock) => spin_loop(),
                Err(Poisoned(e)) => break Err(e),
            }
        }
    }

    /// Attempts to acquire this lock.
    ///
    /// If the lock could not be acquired at this time, then [`Err`] is returned.
    /// Otherwise, an RAII guard is returned. The lock will be unlocked when the
    /// guard is dropped.
    ///
    /// This function does not block.
    ///
    /// # Errors
    ///
    /// If another user of this mutex panicked while holding the mutex, then
    /// this call will return failure if the mutex would otherwise be
    /// acquired.
    ///
    /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use sync_2::Mutex;
    /// use std::thread;
    ///
    /// let mutex = Arc::new(Mutex::new(0));
    /// let c_mutex = mutex.clone();
    ///
    /// thread::spawn(move || {
    ///     let mut lock = c_mutex.try_lock();
    ///     if let Ok(ref mut mutex) = lock {
    ///         **mutex = 10;
    ///     } else {
    ///         println!("try_lock failed");
    ///     }
    /// }).join().expect("thread::spawn failed");
    /// assert_eq!(*mutex.lock().unwrap(), 10);
    /// ```
    pub fn try_lock(&self) -> TryLockResult<MutexGuard<'_, T>> {
        let x = match self.ref_count.fetch_update(
            SeqCst,
            SeqCst,
            |x| if x < LOCK { Some(x | LOCK) } else { None },
        ) {
            Ok(x) => x,
            Err(_) => return Err(WouldBlock),
        };

        if x < POISON_M {
            Ok(MutexGuard { lock: self })
        } else {
            Err(Poisoned(PoisonError::new(MutexGuard { lock: self })))
        }
    }

    /// Determines whether the mutex is poisoned.
    ///
    /// If another thread is active, the mutex can still become poisoned at any
    /// time. You should not trust a `false` value for program correctness
    /// without additional synchronization.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use sync_2::Mutex;
    /// use std::thread;
    ///
    /// let mutex = Arc::new(Mutex::new(0));
    /// let c_mutex = mutex.clone();
    ///
    /// let _ = thread::spawn(move || {
    ///     let _lock = c_mutex.lock().unwrap();
    ///     panic!(); // the mutex gets poisoned
    /// }).join();
    /// assert_eq!(mutex.is_poisoned(), true);
    /// ```
    #[inline]
    pub fn is_poisoned(&self) -> bool {
        (self.ref_count.load(SeqCst) & POISON_M) == POISON_M
    }

    /// Returns a mutable reference to the underlying data.
    ///
    /// Since this call borrows the `Mutex` mutably, no actual locking needs to
    /// take place -- the mutable borrow statically guarantees no locks exist.
    ///
    /// # Errors
    ///
    /// If another user of this mutex panicked while holding the mutex, then
    /// this call will return an error instead.
    ///
    /// # Examples
    ///
    /// ```
    /// use sync_2::Mutex;
    ///
    /// let mut mutex = Mutex::new(0);
    /// *mutex.get_mut().unwrap() = 10;
    /// assert_eq!(*mutex.lock().unwrap(), 10);
    /// ```
    #[inline]
    pub fn get_mut(&mut self) -> LockResult<&mut T> {
        let x = unsafe { &mut *self.value.get() };

        if self.is_poisoned() {
            Err(PoisonError::new(x))
        } else {
            Ok(x)
        }
    }

    /// Consumes this mutex, returning the underlying data.
    ///
    /// # Errors
    ///
    /// If another user of this mutex panicked while holding the mutex, then
    /// this call will return an error instead.
    ///
    /// # Examples
    ///
    /// ```
    /// use sync_2::Mutex;
    ///
    /// let mutex = Mutex::new(0);
    /// assert_eq!(mutex.into_inner().unwrap(), 0);
    /// ```
    #[inline]
    pub fn into_inner(self) -> LockResult<T> {
        if self.is_poisoned() {
            Err(PoisonError::new(self.value.into_inner()))
        } else {
            Ok(self.value.into_inner())
        }
    }
}

impl<T> !Send for MutexGuard<'_, T> {}
unsafe impl<T: Sync> Sync for MutexGuard<'_, T> {}

/// Unique guard created by the method [`Mutex::lock`] and [`Mutex::try_lock`].
pub struct MutexGuard<'a, T> {
    lock: &'a Mutex<T>,
}

impl<'a, T> Deref for MutexGuard<'a, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe { &*self.lock.value.get() }
    }
}

impl<'a, T> DerefMut for MutexGuard<'a, T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *self.lock.value.get() }
    }
}

impl<'a, T> Drop for MutexGuard<'a, T> {
    #[inline]
    fn drop(&mut self) {
        if panicking() {
            self.lock.ref_count.fetch_or(POISON_M, SeqCst);
        }
        self.lock.ref_count.fetch_sub(LOCK, SeqCst);
        #[cfg(debug_assertions)]
        self.lock
            .local_ref_count
            .get_or(|| AtomicU8::new(0))
            .fetch_sub(LOCK, Relaxed);
    }
}

impl<'a, T: Seek> Seek for MutexGuard<'a, T> {
    #[inline]
    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
        (**self).seek(pos)
    }
}

impl<'a, T: Write> Write for MutexGuard<'a, T> {
    #[inline]
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        (**self).write(buf)
    }

    #[inline]
    fn flush(&mut self) -> io::Result<()> {
        (**self).flush()
    }
}

impl<'a, T: Read> Read for MutexGuard<'a, T> {
    #[inline]
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        (**self).read(buf)
    }
}

impl<'a, T: BufRead> BufRead for MutexGuard<'a, T> {
    #[inline]
    fn fill_buf(&mut self) -> io::Result<&[u8]> {
        (**self).fill_buf()
    }

    #[inline]
    fn consume(&mut self, amt: usize) {
        (**self).consume(amt)
    }
}

impl<'a, T: AsRef<U>, U> AsRef<U> for MutexGuard<'a, T> {
    fn as_ref(&self) -> &U {
        (**self).as_ref()
    }
}

impl<'a, T: AsMut<U>, U> AsMut<U> for MutexGuard<'a, T> {
    fn as_mut(&mut self) -> &mut U {
        (**self).as_mut()
    }
}