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
use os_thread_local::ThreadLocal; use std::boxed::Box; use std::cell::RefCell; use std::sync::{ atomic::{AtomicPtr, AtomicUsize}, Arc, Mutex, }; #[cfg(test)] mod tests; pub use std::sync::atomic::Ordering; static THREADID_COUNTER: AtomicUsize = AtomicUsize::new(1); thread_local! { static THREADID: RefCell<usize> = RefCell::new(0); } fn aquire_threadid() -> usize { THREADID.with(|threadid| { let mut value = *threadid.borrow(); if value == 0 { threadid.replace(THREADID_COUNTER.fetch_add(1, Ordering::Relaxed)); value = *threadid.borrow(); } value }) } enum HazardValueImpl<'registry, T: Send + 'registry> { Boxed { ptr: *mut T, registry: Option<&'registry HazardRegistryImpl<T>>, }, Dummy { ptr: *mut T, }, } impl<'registry, T: Send + 'registry> HazardValueImpl<'registry, T> { fn is_dummy(ptr: *mut T) -> bool { let mask = 1_usize; ((ptr as usize) & mask) != 0 } fn leak(self) -> *mut T { let ptr = self.as_ptr(); std::mem::forget(self); ptr } fn as_ptr(&self) -> *mut T { match self { HazardValueImpl::Boxed { ptr, .. } => *ptr, HazardValueImpl::Dummy { ptr } => *ptr, } } } impl<'registry, T: Send + 'registry> Drop for HazardValueImpl<'registry, T> { fn drop(&mut self) { if let HazardValueImpl::Boxed { ptr, registry } = self { let ptr = *ptr; if ptr != std::ptr::null_mut() { if let Some(registry) = registry.take() { registry.delete(ptr); } else { let boxed = unsafe { Box::from_raw(ptr) }; drop(boxed); } } } } } /// encapsulates a boxed Value or a Dummy (like null) /// /// # Examples /// /// ``` /// use peril::HazardValue; /// /// // creating a boxed string value /// let boxed = HazardValue::boxed("test"); /// /// // creating a dummy nullptr /// let dummy = HazardValue::<usize>::dummy(0); /// ``` pub struct HazardValue<'registry, T: Send + 'registry>(HazardValueImpl<'registry, T>); impl<'registry, T: Send + 'registry> HazardValue<'registry, T> { /// create a boxed HazardValue /// /// # Arguments /// /// * `value` - A generic value held within the box /// /// # Examples /// /// ``` /// use peril::HazardValue; /// /// // creating a boxed string value /// let boxed = HazardValue::boxed("test"); /// ``` pub fn boxed(value: T) -> HazardValue<'registry, T> { let boxed = Box::new(value); let ptr = Box::into_raw(boxed); debug_assert!( !HazardValueImpl::is_dummy(ptr), "unexpected low bit set in allocation" ); HazardValue(HazardValueImpl::Boxed { ptr, registry: None, }) } /// create a Dummy HazardValue (like null) /// /// # Arguments /// /// * `value` - the dummy pointer value (usually used for null values) /// /// # Examples /// /// ``` /// use peril::HazardValue; /// /// // creating a dummy nullptr /// let dummy = HazardValue::<usize>::dummy(0); /// ``` pub fn dummy(value: usize) -> HazardValue<'registry, T> { let max = usize::MAX >> 1; assert!(value <= max, "High bit is needed for internal information"); HazardValue(HazardValueImpl::Dummy { ptr: ((value << 1) | 1) as *mut T, }) } fn from_ptr( ptr: *mut T, registry: Option<&'registry HazardRegistryImpl<T>>, ) -> HazardValue<'registry, T> { if HazardValueImpl::is_dummy(ptr) { HazardValue(HazardValueImpl::Dummy { ptr }) } else { HazardValue(HazardValueImpl::Boxed { ptr, registry }) } } /// read the data from a Boxed HazardValue and will return None if the value is a dummy /// /// # Examples /// /// ``` /// use peril::HazardValue; /// /// // creating a boxed string value /// let boxed = HazardValue::boxed("test"); /// assert!(boxed.as_ref().unwrap() == &"test"); /// ``` pub fn as_ref(&self) -> Option<&T> { if let HazardValueImpl::Boxed { ptr, .. } = self.0 { let ptr = unsafe { &*ptr }; Some(ptr) } else { None } } /// check if a value is a Boxed HazardValue /// /// # Examples /// /// ``` /// use peril::HazardValue; /// /// // creating a boxed string value /// let boxed = HazardValue::boxed("test"); /// assert!(boxed.is_boxed()); /// ``` pub fn is_boxed(&self) -> bool { match self.0 { HazardValueImpl::Boxed { .. } => true, HazardValueImpl::Dummy { .. } => false, } } /// check if a value is a Dummy HazardValue /// /// # Examples /// /// ``` /// use peril::HazardValue; /// /// // creating a dummy value /// let dummy = HazardValue::<usize>::dummy(0); /// assert!(dummy.is_dummy()); /// ``` pub fn is_dummy(&self) -> bool { match self.0 { HazardValueImpl::Boxed { .. } => false, HazardValueImpl::Dummy { .. } => true, } } /// get the (pointer) dummy value of HazardValue or return None if the value is not a dummy /// /// # Examples /// /// ``` /// use peril::HazardValue; /// /// // creating a dummy value /// let dummy = HazardValue::<usize>::dummy(1337); /// assert!(dummy.as_dummy().unwrap() == 1337); /// ``` pub fn as_dummy(&self) -> Option<usize> { match self.0 { HazardValueImpl::Boxed { .. } => None, HazardValueImpl::Dummy { ptr } => Some((ptr as usize) >> 1), } } /// modify the data from a Boxed HazardValue or return None if the value is a dummy /// this interface is unsafe because another thread could still read the data while it is modified /// if it is modified shortly after swapping it out while it is still protected on another thread /// /// # Examples /// /// ``` /// use peril::HazardValue; /// /// // creating a boxed value /// let mut boxed = HazardValue::boxed(1); /// *unsafe{ boxed.as_mut().unwrap() } = 2; /// assert!(boxed.as_ref().unwrap() == &2); /// ``` pub unsafe fn as_mut(&mut self) -> Option<&mut T> { if let HazardValueImpl::Boxed { ptr, .. } = self.0 { let ptr = &mut *ptr; Some(ptr) } else { None } } /// modify the data from a Boxed HazardValue or return None if the value is still protected or a dummy /// /// # Examples /// /// ``` /// use peril::HazardValue; /// /// // creating a boxed value /// let mut boxed = HazardValue::boxed(1); /// *boxed.as_mut_safe().unwrap() = 2; /// assert!(boxed.as_ref().unwrap() == &2); /// ``` pub fn as_mut_safe(&mut self) -> Option<&mut T> { if let HazardValueImpl::Boxed { ptr, registry } = self.0 { if let Some(registry) = registry { if registry.scan_one(ptr) { return None; } } let ptr = unsafe { &mut *ptr }; Some(ptr) } else { None } } /// take the data from a Boxed HazardValue or return None if the value is a dummy /// this interface is unsafe because another thread could still read the data while it is taken /// if it is taken shortly after swapping it out while it is still protected on another thread /// /// # Examples /// /// ``` /// use peril::HazardValue; /// /// // creating a boxed value /// let mut boxed = HazardValue::boxed(1); /// let taken = unsafe{ boxed.take().unwrap() }; /// assert!(taken == 1); /// ``` pub unsafe fn take(self) -> Option<T> { if let HazardValueImpl::Boxed { ptr, .. } = self.0 { let boxed = Box::from_raw(ptr); self.0.leak(); Some(*boxed) } else { None } } /// take the data from a Boxed HazardValue or return None if the value is still protected or a dummy /// /// # Examples /// /// ``` /// use peril::HazardValue; /// /// // creating a boxed value /// let mut boxed = HazardValue::boxed(1); /// let taken = boxed.take_safe().unwrap(); /// assert!(taken == 1); /// ``` pub fn take_safe(self) -> Option<T> { if let HazardValueImpl::Boxed { ptr, registry } = self.0 { if let Some(registry) = registry { if registry.scan_one(ptr) { return None; } } let boxed = unsafe { Box::from_raw(ptr) }; self.0.leak(); Some(*boxed) } else { None } } } struct HazardRecordImpl<'registry> { record: &'registry AtomicPtr<()>, } impl<'registry> Drop for HazardRecordImpl<'registry> { fn drop(&mut self) { debug_assert!( self.record.load(Ordering::Relaxed) != std::ptr::null_mut(), "null is reserved to mark free slots" ); self.record.store(std::ptr::null_mut(), Ordering::Relaxed); } } impl<'registry> HazardRecordImpl<'registry> { fn acquire<T>(&mut self, atomic: &AtomicPtr<T>) -> *mut T { let mut ptr = atomic.load(Ordering::Acquire); loop { debug_assert!( ptr != std::ptr::null_mut(), "null is reserved to mark free slots" ); self.record.store(ptr as *mut (), Ordering::Release); let after = atomic.load(Ordering::Acquire); if ptr == after { return ptr; } ptr = after; } } fn release(&mut self) { self.record.store(1 as *mut (), Ordering::Relaxed); } } /// is used outsite of the CAS loop to cache an expensive operation /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed("test"), ®istry); /// let mut record = HazardRecord::default(); /// loop { /// let scope = hp.protect(&mut record); /// //... /// break; /// } /// ``` pub struct HazardRecord<'registry> { record: Option<HazardRecordImpl<'registry>>, } impl<'registry> Default for HazardRecord<'registry> { fn default() -> Self { HazardRecord { record: None } } } /// is the scope where the HazardPointer can be safely read /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer, Ordering}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed("test"), ®istry); /// let mut record = HazardRecord::default(); /// loop { /// let scope = hp.protect(&mut record); /// // ... /// if scope.compare_exchange_weak(HazardValue::dummy(0), Ordering::Relaxed, Ordering::Relaxed).is_ok() /// { /// break; /// } /// } /// ``` pub struct HazardScope<'registry, 'hazard, T: Send + 'registry> { record: &'hazard mut HazardRecordImpl<'registry>, registry: &'registry HazardRegistryImpl<T>, ptr: &'registry HazardPointer<T>, current: *mut T, } impl<'registry, 'hazard, T: Send + 'registry> Drop for HazardScope<'registry, 'hazard, T> { fn drop(&mut self) { self.record.release(); } } impl<'registry, 'hazard, T: Send + 'registry> HazardScope<'registry, 'hazard, T> { /// compare_exchange_weak version of HazardPointer that needs to use a protected HazardScope as the current value (comperator) /// is saved when the potection starts so that the value can be safely read and copied. When the CAS succeeds it will return /// the previous value held by the HazardPointer and when the CAS fails it will return the new value we just provided. /// for more detail see [compare_exchange_weak](std::sync::atomic::AtomicPtr::compare_exchange_weak) /// /// # Arguments /// /// * `new` - the new value the HazardPointer should have when the CAS succeeds (and returned when the CAS fails) /// * `success` - the [Ordering](std::sync::atomic::Ordering) when the CAS succeeds /// * `failure` - the [Ordering](std::sync::atomic::Ordering) when the CAS fails /// * `return` - Result wrapping the old value if the CAS succeeds or the new value if it failed /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer, Ordering}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed("test"), ®istry); /// let mut record = HazardRecord::default(); /// loop { /// let scope = hp.protect(&mut record); /// // ... /// if scope.compare_exchange_weak(HazardValue::dummy(0), Ordering::Relaxed, Ordering::Relaxed).is_ok() /// { /// break; /// } /// } /// ``` pub fn compare_exchange_weak( self, new: HazardValue<'registry, T>, success: Ordering, failure: Ordering, ) -> Result<HazardValue<'registry, T>, HazardValue<'registry, T>> { match self .ptr .atomic .compare_exchange_weak(self.current, new.0.as_ptr(), success, failure) { Ok(old) => { new.0.leak(); self.record.release(); Ok(HazardValue::from_ptr(old, Some(self.registry))) } Err(_) => Err(new), } } /// compare_exchange version of HazardPointer that needs to use a protected HazardScope as the current value (comperator) /// is saved when the potection starts so that the value can be safely read and copied. When the CAS succeeds it will return /// the previous value held by the HazardPointer and when the CAS fails it will return the new value we just provided. /// for more detail see [compare_exchange](std::sync::atomic::AtomicPtr::compare_exchange) /// /// # Arguments /// /// * `new` - the new value the HazardPointer should have when the CAS succeeds (and returned when the CAS fails) /// * `success` - the [Ordering](std::sync::atomic::Ordering) when the CAS succeeds /// * `failure` - the [Ordering](std::sync::atomic::Ordering) when the CAS fails /// * `return` - Result wrapping the old value if the CAS succeeds or the new value if it failed /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer, Ordering}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed("test"), ®istry); /// let mut record = HazardRecord::default(); /// loop { /// let scope = hp.protect(&mut record); /// // ... /// if scope.compare_exchange(HazardValue::dummy(0), Ordering::Relaxed, Ordering::Relaxed).is_ok() /// { /// break; /// } /// } /// ``` pub fn compare_exchange( self, new: HazardValue<'registry, T>, success: Ordering, failure: Ordering, ) -> Result<HazardValue<'registry, T>, HazardValue<'registry, T>> { match self .ptr .atomic .compare_exchange(self.current, new.0.as_ptr(), success, failure) { Ok(old) => { new.0.leak(); self.record.release(); Ok(HazardValue::from_ptr(old, Some(self.registry))) } Err(_) => Err(new), } } /// check if the protected value is a dummy /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer}; /// /// let registry = HazardRegistry::<usize>::default(); /// let hp = HazardPointer::new(HazardValue::dummy(0), ®istry); /// let mut record = HazardRecord::default(); /// loop { /// let scope = hp.protect(&mut record); /// assert!(scope.is_dummy()); /// // ... /// break; /// } /// ``` pub fn is_dummy(&self) -> bool { HazardValueImpl::is_dummy(self.current) } /// read the dummy (pointer) value of a HazardPointer /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer}; /// /// let registry = HazardRegistry::<usize>::default(); /// let hp = HazardPointer::new(HazardValue::dummy(1337), ®istry); /// let mut record = HazardRecord::default(); /// loop { /// let scope = hp.protect(&mut record); /// assert!(scope.as_dummy().unwrap() == 1337); /// // ... /// break; /// } /// ``` pub fn as_dummy(&self) -> Option<usize> { if HazardValueImpl::is_dummy(self.current) { Some((self.current as usize) >> 1) } else { None } } /// read the boxed value of a HazardPointer and returns None if the value is a dummy /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer, Ordering}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed(0), ®istry); /// let mut record = HazardRecord::default(); /// loop { /// let scope = hp.protect(&mut record); /// let new = *(scope.as_ref().unwrap()) + 1; /// match scope.compare_exchange(HazardValue::boxed(new), Ordering::Relaxed, Ordering::Relaxed) /// { /// Ok(old) => /// { /// assert!(old.as_ref().unwrap() == &0); /// break; /// } /// Err(_) => assert!(false), /// } /// } /// ``` pub fn as_ref(&self) -> Option<&T> { if HazardValueImpl::is_dummy(self.current) { None } else { let reference = unsafe { &*self.current }; Some(reference) } } } /// is the an atomic pointer that is safe to read when it is protected /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed("test"), ®istry); /// let mut record = HazardRecord::default(); /// loop { /// let scope = hp.protect(&mut record); /// // ... /// break; /// } /// ``` pub struct HazardPointer<T: Send> { registry: Arc<HazardRegistryImpl<T>>, atomic: AtomicPtr<T>, } impl<T: Send> HazardPointer<T> { /// create a HazardPointer /// /// # Arguments /// /// * `value` - the HazardValue the pointer is initzialized with /// * `registry` - the HazardRegistry the pointer is registered with, usually HazardPointers in the same system share a common Registry /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer, Ordering}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed("test"), ®istry); /// ``` pub fn new(value: HazardValue<T>, registry: &HazardRegistry<T>) -> HazardPointer<T> { HazardPointer { registry: registry.registry.clone(), atomic: AtomicPtr::new(value.0.leak()), } } /// protect the hazardpointer so that it is safe to read and update using a CAS operation /// /// # Arguments /// /// * `record` - the HazardRecord, which caches an expensive operation for multiple itterations of the update loop /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed("test"), ®istry); /// let mut record = HazardRecord::default(); /// loop { /// let scope = hp.protect(&mut record); /// // ... /// break; /// } /// ``` #[must_use] pub fn protect<'registry, 'hazard>( &'registry self, record: &'hazard mut HazardRecord<'registry>, ) -> HazardScope<'registry, 'hazard, T> { if record.record.is_none() { record.record = Some(self.registry.alloc()); } let record = record.record.as_mut().unwrap(); let current = record.acquire(&self.atomic); HazardScope { ptr: self, record, registry: &self.registry, current, } } /// swaps the value of a HazardPointer returning the old value /// /// # Arguments /// /// * `new` - the HazardValue after the swap /// * `order` - the [Ordering](std::sync::atomic::Ordering) during the swap operation /// * `return` - the HazardValue before the swap /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer, Ordering}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed("test"), ®istry); /// let old = hp.swap(HazardValue::boxed("test2"), Ordering::Relaxed); /// assert!(old.as_ref().unwrap() == &"test"); /// ``` pub fn swap<'registry>( &'registry self, new: HazardValue<'registry, T>, order: Ordering, ) -> HazardValue<'registry, T> { let old = self.atomic.swap(new.0.leak(), order); HazardValue::from_ptr(old, Some(&self.registry)) } /// swaps the value of a HazardPointer with a null dummy HazardValue /// /// # Arguments /// /// * `order` - the [Ordering](std::sync::atomic::Ordering) during the swap operation /// * `return` - the HazardValue before the swap /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer, Ordering}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed("test"), ®istry); /// let old = hp.swap_null(Ordering::Relaxed); /// assert!(old.as_ref().unwrap() == &"test"); /// let old = hp.swap_null(Ordering::Relaxed); /// assert!(old.as_dummy().unwrap() == 0); /// ``` pub fn swap_null(&self, order: Ordering) -> HazardValue<'_, T> { //swapping 1 here because it represents the dummy 0 let old = self.atomic.swap(1 as *mut T, order); HazardValue::from_ptr(old, Some(&self.registry)) } /// stores a new value of a HazardPointer and drops the old value, this internally uses a swap operation /// /// # Arguments /// /// * `new` - the HazardValue after the store /// * `order` - the [Ordering](std::sync::atomic::Ordering) during the store operation /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardRecord, HazardPointer, Ordering}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed("test"), ®istry); /// hp.store(HazardValue::dummy(1337), Ordering::Relaxed); /// let old = hp.swap(HazardValue::boxed("test2"), Ordering::Relaxed); /// assert!(old.as_dummy().unwrap() == 1337); /// ``` pub fn store(&self, new: HazardValue<'_, T>, order: Ordering) { let old = self.swap(new, order); drop(old); } } impl<T: Send> Drop for HazardPointer<T> { fn drop(&mut self) { let value = HazardValue::from_ptr(self.atomic.load(Ordering::Relaxed), None); drop(value); } } const HP_CHUNKS: usize = 32; #[repr(align(64))] struct AtomicPointer(AtomicPtr<()>); impl Default for AtomicPointer { fn default() -> Self { AtomicPointer(AtomicPtr::new(std::ptr::null_mut())) } } struct HazardSlots<T: Send> { slots: [AtomicPointer; HP_CHUNKS], next: AtomicPtr<HazardSlots<T>>, mutex: Mutex<()>, } impl<T: Send> Drop for HazardSlots<T> { fn drop(&mut self) { let next = self.next.load(Ordering::Relaxed); if next != std::ptr::null_mut() { let boxed = unsafe { Box::from_raw(next) }; drop(boxed); } self.next = AtomicPtr::default(); } } impl<T: Send> HazardSlots<T> { fn new() -> HazardSlots<T> { HazardSlots { slots: Default::default(), next: AtomicPtr::default(), mutex: Mutex::new(()), } } fn grow<'registry>( &'registry self, tid: usize, registry: &'registry HazardRegistryImpl<T>, ) -> HazardRecordImpl<'registry> { let lock = self.mutex.lock(); if self.next.load(Ordering::Acquire) == std::ptr::null_mut() { let next = Box::new(HazardSlots::new()); next.slots[0].0.store(1 as *mut (), Ordering::Release); let next = Box::into_raw(next); self.next.store(next, Ordering::Release); registry.numslots.fetch_add(HP_CHUNKS, Ordering::Relaxed); let next = unsafe { &*next }; return HazardRecordImpl { record: &next.slots[0].0, }; } drop(lock); let next = self.next.load(Ordering::Relaxed); let next = unsafe { &*next }; next.alloc(tid, registry) } fn alloc<'registry>( &'registry self, tid: usize, registry: &'registry HazardRegistryImpl<T>, ) -> HazardRecordImpl<'registry> { for i in 0..HP_CHUNKS { let index = (tid + i) % HP_CHUNKS; if self.slots[index] .0 .compare_exchange_weak( std::ptr::null_mut(), 1 as *mut (), Ordering::Release, Ordering::Relaxed, ) .is_ok() { return HazardRecordImpl { record: &self.slots[index].0, }; } } let next = self.next.load(Ordering::Acquire); if next != std::ptr::null_mut() { let next = unsafe { &*next }; next.alloc(tid, registry) } else { self.grow(tid, registry) } } } struct DeletedItem<T: Send>(*mut (), Box<T>); struct DeletedList<T: Send>(Vec<DeletedItem<T>>, *const HazardRegistryImpl<T>); impl<T: Send> Drop for DeletedList<T> { fn drop(&mut self) { if self.1 != std::ptr::null() { let registry = unsafe { &*self.1 }; loop { if self.0.is_empty() { break; } let hazards = registry.scan(); self.0.retain(|DeletedItem(ptr, ..)| { hazards.binary_search(&(*ptr as *mut ())).is_ok() }); std::thread::yield_now(); } } } } struct HazardRegistryImpl<T: Send> { slots: HazardSlots<T>, numslots: AtomicUsize, deleted: ThreadLocal<RefCell<DeletedList<T>>>, } impl<T: Send> HazardRegistryImpl<T> { fn alloc(&self) -> HazardRecordImpl { let tid = aquire_threadid(); self.slots.alloc(tid, self) } fn scan(&self) -> Vec<*mut ()> { let mut arr = Vec::new(); let mut slots = &self.slots; loop { for i in 0..HP_CHUNKS { let slot = slots.slots[i].0.load(Ordering::Acquire); if slot != std::ptr::null_mut() { arr.push(slot); } } let next = slots.next.load(Ordering::Acquire); if next == std::ptr::null_mut() { break; } slots = unsafe { &*next }; } arr.sort(); arr } fn scan_one(&self, ptr: *mut T) -> bool { let mut slots = &self.slots; loop { for i in 0..HP_CHUNKS { let slot = slots.slots[i].0.load(Ordering::Acquire); if slot == ptr as *mut () { return true; } } let next = slots.next.load(Ordering::Acquire); if next == std::ptr::null_mut() { break; } slots = unsafe { &*next }; } false } fn delete(&self, raw: *mut T) { self.deleted.with(|arr| { arr.replace_with(|&mut DeletedList(ref mut old, _)| { let boxed = unsafe { Box::from_raw(raw) }; old.push(DeletedItem(raw as *mut (), boxed)); if old.len() >= (5 * self.numslots.load(Ordering::Relaxed)) / 4 { let hazards = self.scan(); old.retain(|DeletedItem(ptr, ..)| { hazards.binary_search(&(*ptr as *mut ())).is_ok() }); } DeletedList(old.split_off(0), self as *const Self) }); }); } } /// is a registry that stores lifetime information for multiple HazardPointers /// /// # Examples /// /// ``` /// use peril::{HazardRegistry, HazardValue, HazardPointer}; /// /// let registry = HazardRegistry::default(); /// let hp = HazardPointer::new(HazardValue::boxed("test"), ®istry); /// ``` pub struct HazardRegistry<T: Send> { registry: Arc<HazardRegistryImpl<T>>, } impl<T: Send> Default for HazardRegistry<T> { fn default() -> Self { HazardRegistry { registry: Arc::new(HazardRegistryImpl { slots: HazardSlots::new(), numslots: AtomicUsize::new(HP_CHUNKS), deleted: ThreadLocal::new(|| { RefCell::new(DeletedList(Vec::new(), std::ptr::null())) }), }), } } }