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 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
// Copyright 2018 Mike Hommey // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! # Flexible Locks //! //! This crate aims at providing generic, flexible implementations of locking //! primitives. For now, it only provides `Mutex` types (i.e. no `RwLock`, etc.), //! without [poisoning], and without `try_lock`. Support for those can be //! added in the future if there is interest (patches welcome). Poisoning is not //! necessary with panic=abort. //! //! [poisoning]: https://doc.rust-lang.org/std/sync/struct.Mutex.html#poisoning //! //! The provided types allow flexibility in layout and locking implementation. //! See the [`Mutex`], [`MutexWrap`] and [`RawOsMutex`] documentation for more //! details. //! //! # Features //! //! The `parking_lot` feature can be enabled, providing a [`RawMutex`] //! implementation for `parking_log::Mutex<()>`. #![no_std] #![deny(missing_docs)] #[cfg(any(feature = "std", test))] extern crate std; #[cfg(any(feature = "std", test))] use std::prelude::v1::*; #[cfg(windows)] extern crate winapi; #[cfg(unix)] extern crate libc; #[macro_use] extern crate flexible_locks_derive; #[cfg(feature = "allocator_api")] extern crate allocator_api; #[cfg(feature = "parking_lot")] extern crate parking_lot; // Public for the *_new macros. #[doc(hidden)] pub use core::cell::UnsafeCell; use core::marker::PhantomData; use core::ops::{Deref, DerefMut}; use core::ptr; // For #[derive(MutexProtected)] mod flexible_locks { pub use super::MutexProtected; } mod os; pub use os::*; /// A trait for raw mutual exclusion primitives. pub trait RawMutex: Send + Sync { /// Initialize the raw mutex. /// /// Because initializing an instance may involve moving its location /// when doing e.g. `Box::new(Foo::new())`, because some types /// of raw mutex primitives need to be initialized at their final, /// permanent location (e.g. [`CRITICAL_SECTION`]), or because some /// may not be statically initialized to an entirely satisfying state /// (e.g. [`pthread_mutex_t`], see [issue #33770]), this method is called /// from [`Mutex::new`] to finish the raw mutex initialization. /// /// [`CRITICAL_SECTION`]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682530(v=vs.85).aspx /// [`pthread_mutex_t`]: http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_mutex_init.html /// [issue #33770]: https://github.com/rust-lang/rust/issues/33770 unsafe fn init(&mut self) {} /// Destroy the raw mutex. /// /// In some cases, raw mutex primitives need to be cleaned up after /// use, so this method is called when a [`Mutex`] is dropped. unsafe fn destroy(&self) {} /// Acquire the raw mutex. unsafe fn lock(&self); /// Release the raw mutex. unsafe fn unlock(&self); } /// Wrapping a `RawMutex` in a `Box` is just as good a valid `RawMutex`. /// /// Ideally, any type that derefs to a `RawMutex` would be good too, but /// without specialization, this would prevent implementing `RawMutex` on /// your own mutex types. // // Ideally, this would be: // `impl<T: RawMutex, U: DerefMut<Target = T> + Send + Sync> RawMutex for U` #[cfg(any(feature = "std", test))] impl<T: RawMutex> RawMutex for Box<T> { unsafe fn init(&mut self) { self.as_mut().init() } unsafe fn lock(&self) { self.as_ref().lock() } unsafe fn unlock(&self) { self.as_ref().unlock() } unsafe fn destroy(&self) { self.as_ref().destroy() } } /// A trait describing types that can be wrapped in a [`Mutex`]. /// /// It is not recommended to implement this trait manually. Instead, use the /// `flexible-locks_derive` crate that provides a custom /// `#[derive(MutexProtected)]`. /// /// When using that custom derive, the `#[mutex]` attribute is used to /// indicate the data field containing the raw mutex type. /// /// # Examples /// /// ``` /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::{Mutex, RawMutex}; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::CRITICAL_SECTION as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// #[derive(MutexProtected)] /// struct Data { /// a: usize, /// #[mutex] /// mutex: RawOsMutex, /// b: usize, /// } /// # fn main() {} /// ``` pub trait MutexProtected { /// Raw mutex pritimive used to protect the data type. type MutexType: RawMutex; /// Data type that [`Mutex::lock`] will give access to. /// /// `[derive(MutexProtected)]` makes this `Self` when the type is large, /// but when there are only two fields in the struct (whichever their /// order is), it will set the `DataType` to the type of the non-mutex /// field. /// /// # Examples /// /// ``` /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::{Mutex, RawMutex}; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// #[derive(MutexProtected, Default)] /// struct Data { /// a: usize, /// b: usize, /// #[mutex] /// mutex: RawOsMutex, /// } /// /// #[derive(MutexProtected, Default)] /// struct Data2 { /// a: usize, /// #[mutex] /// mutex: RawOsMutex, /// } /// /// #[derive(MutexProtected, Default)] /// struct Data3 { /// #[mutex] /// mutex: RawOsMutex, /// a: usize, /// } /// /// fn main() { /// let mut mutex = Mutex::new(Data::default()); /// mutex.lock().a = 10; /// let mut mutex = Mutex::new(Data2::default()); /// *mutex.lock() = 10; /// let mut mutex = Mutex::new(Data3::default()); /// *mutex.lock() = 10; /// } /// ``` /// /// `MutexWrap<RawOsMutex, usize>` should be preferred to `Mutex<Data2>` /// and `Mutex<Data3>`. type DataType: ?Sized; /// Return an immutable reference to the raw mutex. fn get_mutex(&self) -> &Self::MutexType; /// Return a mutable reference to the raw mutex. fn get_mutex_mut(&mut self) -> &mut Self::MutexType; /// Return an immutable reference to the data. fn get_data(&self) -> &Self::DataType; /// Return a mutable reference to the data. fn get_data_mut(&mut self) -> &mut Self::DataType; /// Consumes the wrapping type, returning the underlying data. fn into_data(self) -> Self::DataType where Self::DataType: Sized; } // FIXME: specialization should allow to just use (M, T) directly, // and to turn MutexWrap into a type alias. #[doc(hidden)] #[derive(MutexProtected)] pub struct MutexWrapper<M: RawMutex, T: ?Sized>(#[mutex] pub M, pub T); impl<M: RawMutex + Default, T> From<T> for MutexWrapper<M, T> { fn from(t: T) -> Self { MutexWrapper(Default::default(), t) } } /// A mutual exclusion primitive useful for protecting shared data /// /// This mutex will block threads waiting for the lock to become available. The /// mutex can be statically initialized via the [`mutex_new`] macro, or created /// via a [`new`] constructor. Each mutex has a type parameter which represents /// the data that it is protecting. The data can only be accessed through the /// RAII guards returned from [`lock`], which guarantees that the data is only /// ever accessed when the mutex is locked. /// /// [`new`]: #method.new /// [`lock`]: #method.lock /// /// # Differences from [`std::sync::Mutex`] /// /// - No poisoning. /// - No `try_lock`. /// - The underlying raw mutex primitive can be of any kind, within a `Box` or /// not, as long as the [`RawMutex`] trait is implemented. Choose carefully. /// - The raw mutex primitive can be embedded anywhere in the data type. See /// the [`MutexWrap`] type for a variant that looks more like /// [`std::sync::Mutex`] but still allows to use a specific raw mutex /// primitive. /// - With care, this can allow to share data through FFI and contend on the /// same locks. See the `ffi-example` directory. /// /// [`std::sync::Mutex`]: https://doc.rust-lang.org/std/sync/struct.Mutex.html /// [`RawMutex`]: trait.RawMutex.html /// /// # Examples /// /// ``` /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::{Mutex, RawMutex}; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// use std::sync::Arc; /// use std::thread; /// use std::sync::mpsc::channel; /// /// #[derive(MutexProtected, Default)] /// struct Data { /// a: usize, /// b: usize, /// #[mutex] /// mutex: RawOsMutex, /// } /// /// const N: usize = 10; /// /// fn main() { /// // Spawn a few threads to increment a shared variable (non-atomically), /// // and let the main thread know once all increments are done. /// // /// // Here we're using an Arc to share memory among threads, and the data /// // inside the Arc is protected with a mutex. /// let data = Arc::new(Mutex::new(Data::default())); /// /// let (tx, rx) = channel(); /// for _ in 0..N { /// let (data, tx) = (data.clone(), tx.clone()); /// thread::spawn(move || { /// // The shared state can only be accessed once the lock is held. /// // Our non-atomic increment is safe because we're the only thread /// // which can access the shared state when the lock is held. /// let mut data = data.lock(); /// data.a += 1; /// if data.a == N { /// tx.send(()).unwrap(); /// } /// // the lock is unlocked here when `data` goes out of scope. /// }); /// } /// /// rx.recv().unwrap(); /// } /// ``` /// /// Please note that `#[derive(MutexProtected)]` treats structs containing only /// two fields including the raw mutex differently, such that the data handed /// by [`Mutex::lock`] is the non-mutex field, rather than the whole data. /// In that case, it is preferable to use [`MutexWrap`] instead. /// See [`MutexProtected::DataType`]. pub struct Mutex<T: MutexProtected + ?Sized> { #[doc(hidden)] pub __wrapper: UnsafeCell<T>, } unsafe impl<T: MutexProtected + ?Sized + Send> Send for Mutex<T> {} unsafe impl<T: MutexProtected + ?Sized + Send> Sync for Mutex<T> {} /// Statically initializes a [`Mutex`] or a [`MutexWrap`]. /// /// This skips the [`RawMutex::init`] method, so please be careful when using /// this, and ensure the statically initialized raw mutex is properly usable. /// /// For non-static initialization, it is recommended to use [`Mutex::new`] or /// [`MutexWrap::new`]. /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::{Mutex, MutexWrap, RawMutex}; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// #[derive(MutexProtected)] /// struct Data { /// a: usize, /// b: usize, /// #[mutex] /// mutex: RawOsMutex, /// } /// /// static DATA: Mutex<Data> = mutex_new!(Data { /// a: 2, /// b: 1, /// #[cfg(windows)] /// mutex: srwlock_new!(), /// #[cfg(unix)] /// mutex: pthread_mutex_new!(), /// }); /// /// struct Data2 { /// a: usize, /// b: usize, /// } /// /// #[cfg(windows)] /// macro_rules! raw_os_mutex { /// () => { srwlock_new!() }; /// } /// #[cfg(unix)] /// macro_rules! raw_os_mutex { /// () => { pthread_mutex_new!() }; /// } /// /// static DATA2: MutexWrap<RawOsMutex, Data2> = mutex_new!( /// raw_os_mutex!(), /// Data2 { a: 2, b: 1 } /// ); /// # fn main() {} /// ``` #[macro_export] macro_rules! mutex_new { ($m:expr, $d:expr) => { $crate::MutexWrap { __inner: mutex_new!($crate::MutexWrapper($m, $d)), } }; ($e:expr) => { $crate::Mutex { __wrapper: $crate::UnsafeCell::new($e), } }; } impl<T: MutexProtected> Mutex<T> where T::DataType: Sized, { /// Creates a new mutex in an unlocked state ready for use. /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::{Mutex, RawMutex}; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// #[derive(MutexProtected)] /// struct Data { /// a: usize, /// b: usize, /// #[mutex] /// mutex: RawOsMutex, /// } /// /// fn main() { /// let mutex = Mutex::new(Data { /// a: 2, /// b: 1, /// mutex: Default::default(), /// }); /// } /// ``` pub fn new(t: T) -> Self { let m = mutex_new!(t); unsafe { let wrapper: &mut T = &mut *m.__wrapper.get(); wrapper.get_mutex_mut().init(); } m } /// Consumes this mutex, returning the underlying data. /// /// When the data type contains the raw mutex, which happens with /// `#[derive(MutexProtected)]`, the returned data obviously still /// contains it. It is however in a destroyed state and may not be /// reused. /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::{Mutex, RawMutex}; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// #[derive(MutexProtected)] /// struct Data { /// a: usize, /// b: usize, /// #[mutex] /// mutex: RawOsMutex, /// } /// /// fn main() { /// let mutex = Mutex::new(Data { /// a: 2, /// b: 1, /// mutex: Default::default(), /// }); /// let data = mutex.into_inner(); /// assert_eq!(data.a, 2); /// assert_eq!(data.b, 1); /// } /// ``` pub fn into_inner(self) -> T::DataType { unsafe { let wrapper = ptr::read(&self.__wrapper); core::mem::forget(self); wrapper.into_inner().into_data() } } } impl<T: MutexProtected> From<T> for Mutex<T> where T::DataType: Sized, { fn from(t: T) -> Self { Mutex::<T>::new(t) } } impl<T: MutexProtected<DataType = T> + Sized + Default> Default for Mutex<T> { fn default() -> Self { Mutex::<T>::new(Default::default()) } } impl<T: MutexProtected + ?Sized> Mutex<T> { /// 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 depends on the underlying raw mutex implementation. /// /// # Examples /// /// ``` /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::{Mutex, RawMutex}; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// use std::sync::Arc; /// use std::thread; /// /// #[derive(MutexProtected, Default)] /// struct Data { /// a: usize, /// b: usize, /// #[mutex] /// mutex: RawOsMutex, /// } /// /// fn main() { /// let mutex = Arc::new(Mutex::new(Data::default())); /// let c_mutex = mutex.clone(); /// /// thread::spawn(move || { /// c_mutex.lock().a = 10; /// }).join().expect("thread::spawn failed"); /// assert_eq!(mutex.lock().a, 10); /// } /// ``` pub fn lock(&self) -> MutexGuard<T> { unsafe { let wrapper = &mut *self.__wrapper.get(); wrapper.get_mutex().lock(); MutexGuard::new(wrapper) } } /// 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. /// /// # Examples /// /// ``` /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::{Mutex, RawMutex}; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// #[derive(MutexProtected, Default)] /// struct Data { /// a: usize, /// b: usize, /// #[mutex] /// mutex: RawOsMutex, /// } /// /// fn main() { /// let mut mutex = Mutex::new(Data::default()); /// mutex.get_mut().a = 10; /// assert_eq!(mutex.lock().a, 10); /// } /// ``` pub fn get_mut(&mut self) -> &mut T::DataType { let wrapper = unsafe { &mut *self.__wrapper.get() }; T::get_data_mut(wrapper) } } impl<T: MutexProtected + ?Sized> Drop for Mutex<T> { fn drop(&mut self) { unsafe { let wrapper: &T = &*self.__wrapper.get(); wrapper.get_mutex().destroy(); } } } /// A mutual exclusion primitive useful for protecting shared data /// /// This mutex will block threads waiting for the lock to become available. The /// mutex can be statically initialized via the [`mutex_new`] macro, or created /// via a [`new`] constructor. Each mutex has a raw mutex and a type parameter /// which represents the data that it is protecting. The data can only be /// accessed through the RAII guards returned from [`lock`], which guarantees /// that the data is only ever accessed when the mutex is locked. /// /// [`new`]: #method.new /// [`lock`]: #method.lock /// /// # Differences from [`std::sync::Mutex`] /// /// - No poisoning. /// - No `try_lock`. /// - The underlying raw mutex primitive can be of any kind, within a `Box` or /// not, as long as the [`RawMutex`] trait is implemented. Choose carefully. /// - With care, this can allow to share data through FFI and contend on the /// same locks. See the `ffi-example` directory. /// /// # Examples /// /// ``` /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::MutexWrap; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// use std::sync::Arc; /// use std::thread; /// use std::sync::mpsc::channel; /// /// const N: usize = 10; /// /// fn main() { /// // Spawn a few threads to increment a shared variable (non-atomically), /// // and let the main thread know once all increments are done. /// // /// // Here we're using an Arc to share memory among threads, and the data /// // inside the Arc is protected with a mutex. /// let data = Arc::new(MutexWrap::<RawOsMutex, _>::new(0)); /// /// let (tx, rx) = channel(); /// for _ in 0..N { /// let (data, tx) = (data.clone(), tx.clone()); /// thread::spawn(move || { /// // The shared state can only be accessed once the lock is held. /// // Our non-atomic increment is safe because we're the only thread /// // which can access the shared state when the lock is held. /// let mut data = data.lock(); /// *data += 1; /// if *data == N { /// tx.send(()).unwrap(); /// } /// // the lock is unlocked here when `data` goes out of scope. /// }); /// } /// /// rx.recv().unwrap(); /// } /// ``` pub struct MutexWrap<M: RawMutex, T: ?Sized> { #[doc(hidden)] pub __inner: Mutex<MutexWrapper<M, T>>, } impl<M: RawMutex + Default, T> MutexWrap<M, T> { /// Creates a new mutex in an unlocked state ready for use. /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::MutexWrap; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// fn main() { /// let mutex = MutexWrap::<RawOsMutex, _>::new(0); /// } /// ``` pub fn new(t: T) -> Self { MutexWrap { __inner: Mutex::new(MutexWrapper(Default::default(), t)), } } /// Consumes this mutex, returning the underlying data. /// /// # Examples /// /// ``` /// #[macro_use] /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::MutexWrap; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// fn main() { /// let mutex = MutexWrap::<RawOsMutex, _>::new(0); /// assert_eq!(mutex.into_inner(), 0); /// } /// ``` pub fn into_inner(self) -> T { self.__inner.into_inner() } } impl<M: RawMutex + Default, T> From<T> for MutexWrap<M, T> { fn from(t: T) -> Self { MutexWrap::new(t) } } impl<M: RawMutex + Default, T: Default> Default for MutexWrap<M, T> { fn default() -> Self { MutexWrap::new(Default::default()) } } impl<M: RawMutex, T: ?Sized> MutexWrap<M, T> { /// 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 depends on the underlying raw mutex implementation. /// /// # Examples /// /// ``` /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::MutexWrap; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// use std::sync::Arc; /// use std::thread; /// /// fn main() { /// let mutex = Arc::new(MutexWrap::<RawOsMutex, _>::new(0)); /// let c_mutex = mutex.clone(); /// /// thread::spawn(move || { /// *c_mutex.lock() = 10; /// }).join().expect("thread::spawn failed"); /// assert_eq!(*mutex.lock(), 10); /// } /// ``` pub fn lock(&self) -> MutexGuard<MutexWrapper<M, T>> { self.__inner.lock() } /// 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. /// /// # Examples /// /// ``` /// extern crate flexible_locks; /// #[macro_use] /// extern crate flexible_locks_derive; /// use flexible_locks::MutexWrap; /// /// // Pick your choice of raw mutex; /// #[cfg(windows)] /// use flexible_locks::SRWLOCK as RawOsMutex; /// #[cfg(unix)] /// use flexible_locks::pthread_mutex_t as RawOsMutex; /// /// fn main() { /// let mut mutex = MutexWrap::<RawOsMutex, _>::new(0); /// *mutex.get_mut() = 10; /// assert_eq!(*mutex.lock(), 10); /// } /// ``` pub fn get_mut(&mut self) -> &mut T { self.__inner.get_mut() } } /// An RAII implementation of a "scoped lock" of a mutex. When this structure is /// dropped (falls out of scope), the lock will be unlocked. /// /// The data protected by the mutex can be accessed through this guard via its /// [`Deref`] and [`DerefMut`] implementations. /// /// This structure is created by [`Mutex::lock`] and [`MutexWrap::lock`]. #[must_use] pub struct MutexGuard<'a, T: MutexProtected + ?Sized + 'a> { __wrapper: &'a mut T, marker: PhantomData<*mut ()>, // prevents an automatic impl Send. } unsafe impl<'a, T: MutexProtected + ?Sized + Sync> Sync for MutexGuard<'a, T> {} impl<'a, T: MutexProtected + ?Sized + 'a> MutexGuard<'a, T> { fn new(wrapper: &'a mut T) -> Self { MutexGuard { __wrapper: wrapper, marker: PhantomData, } } } impl<'a, T: MutexProtected + ?Sized + 'a> Drop for MutexGuard<'a, T> { fn drop(&mut self) { unsafe { self.__wrapper.get_mutex().unlock(); } } } impl<'a, T: MutexProtected + ?Sized + 'a> Deref for MutexGuard<'a, T> { type Target = T::DataType; fn deref(&self) -> &T::DataType { T::get_data(&self.__wrapper) } } impl<'a, T: MutexProtected + ?Sized + 'a> DerefMut for MutexGuard<'a, T> { fn deref_mut(&mut self) -> &mut T::DataType { T::get_data_mut(&mut self.__wrapper) } } /// Type alias for `parking_lot::Mutex<()>`. #[cfg(feature = "parking_lot")] pub type ParkingLotMutex = parking_lot::Mutex<()>; #[cfg(feature = "parking_lot")] impl RawMutex for ParkingLotMutex { unsafe fn lock(&self) { self.raw_lock() } unsafe fn unlock(&self) { self.raw_unlock() } } #[cfg(test)] mod tests { use super::*; #[cfg(windows)] type RawOsMutex = SRWLOCK; #[cfg(unix)] type RawOsMutex = pthread_mutex_t; mod base { use super::*; static mut INITIALIZED: usize = 0; static mut LOCKED: usize = 0; static mut UNLOCKED: usize = 0; static mut DESTROYED: usize = 0; #[derive(PartialEq, Debug, Default)] struct FakeMutex; unsafe impl Send for FakeMutex {} unsafe impl Sync for FakeMutex {} impl RawMutex for FakeMutex { unsafe fn init(&mut self) { INITIALIZED += 1; } unsafe fn lock(&self) { LOCKED += 1; } unsafe fn unlock(&self) { UNLOCKED += 1; } unsafe fn destroy(&self) { DESTROYED += 1; } } #[derive(MutexProtected, PartialEq, Debug, Default)] struct WithEmbeddedMutex<M: RawMutex>(usize, #[mutex] M, usize); #[test] fn smoke() { macro_rules! smoke_test { ($m:expr, $drop:expr) => { unsafe { INITIALIZED = 0; LOCKED = 0; UNLOCKED = 0; DESTROYED = 0; } let m = $m; unsafe { assert_eq!(INITIALIZED, 1); assert_eq!(LOCKED, 0); assert_eq!(UNLOCKED, 0); assert_eq!(DESTROYED, 0); } for i in 0..2 { let data = m.lock(); unsafe { assert_eq!(INITIALIZED, 1); assert_eq!(LOCKED, i + 1); assert_eq!(UNLOCKED, i); assert_eq!(DESTROYED, 0); } drop(data); unsafe { assert_eq!(INITIALIZED, 1); assert_eq!(LOCKED, i + 1); assert_eq!(UNLOCKED, i + 1); assert_eq!(DESTROYED, 0); } } $drop(m); unsafe { assert_eq!(INITIALIZED, 1); assert_eq!(LOCKED, 2); assert_eq!(UNLOCKED, 2); assert_eq!(DESTROYED, 1); } }; } smoke_test!(MutexWrap::<FakeMutex, usize>::new(42), drop); smoke_test!( MutexWrap::<FakeMutex, usize>::new(42), |m: MutexWrap<_, _>| assert_eq!(m.into_inner(), 42) ); smoke_test!(MutexWrap::<Box<FakeMutex>, usize>::new(42), drop); smoke_test!( MutexWrap::<Box<FakeMutex>, usize>::new(42), |m: MutexWrap<_, _>| assert_eq!(m.into_inner(), 42) ); smoke_test!(Mutex::new(WithEmbeddedMutex(42, FakeMutex, 42)), drop); smoke_test!( Mutex::new(WithEmbeddedMutex(42, FakeMutex, 42)), |m: Mutex<_>| assert_eq!(m.into_inner(), WithEmbeddedMutex(42, FakeMutex, 42)) ); smoke_test!( Mutex::new(WithEmbeddedMutex(42, Box::new(FakeMutex), 42)), drop ); smoke_test!( Mutex::new(WithEmbeddedMutex(42, Box::new(FakeMutex), 42)), |m: Mutex<_>| assert_eq!( m.into_inner(), WithEmbeddedMutex(42, Box::new(FakeMutex), 42) ) ); } } mod wrap { use super::*; type Mutex<T> = super::MutexWrap<RawOsMutex, T>; include!("tests.rs"); } mod boxed { use super::*; type Mutex<T> = super::MutexWrap<Box<RawOsMutex>, T>; include!("tests.rs"); } #[cfg(feature = "parking_lot")] mod parking_lot { use super::*; type Mutex<T> = super::MutexWrap<ParkingLotMutex, T>; include!("tests.rs"); } #[cfg(windows)] mod critical_section { use super::*; type Mutex<T> = super::MutexWrap<CRITICAL_SECTION, T>; include!("tests.rs"); } #[cfg(any(target_os = "macos", target_os = "ios"))] mod osspinlock { use super::*; type Mutex<T> = super::MutexWrap<OSSpinLock, T>; include!("tests.rs"); } }