test_reprangelock.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // -*- coding: utf-8 -*-
  2. //
  3. // Copyright 2021-2023 Michael Büsch <m@bues.ch>
  4. //
  5. // Licensed under the Apache License version 2.0
  6. // or the MIT license, at your option.
  7. // SPDX-License-Identifier: Apache-2.0 OR MIT
  8. //
  9. use range_lock::RepVecRangeLock;
  10. use std::sync::{Arc, Barrier};
  11. use std::thread;
  12. #[test]
  13. fn test_rangelock() {
  14. // The data that will simultaneously be accessed from the threads.
  15. let data = vec![10, 11, 12, 13, 20, 21, 22, 23, 30, 31, 32, 33];
  16. // Embed the data in a VecRangeLock
  17. // and clone atomic references to it for the threads.
  18. let data_lock0 = Arc::new(RepVecRangeLock::new(data, 1, 4));
  19. let data_lock1 = Arc::clone(&data_lock0);
  20. let data_lock2 = Arc::clone(&data_lock0);
  21. // Thread barrier, only for demonstration purposes.
  22. let barrier0 = Arc::new(Barrier::new(2));
  23. let barrier1 = Arc::clone(&barrier0);
  24. thread::scope(|s| {
  25. // Spawn first thread.
  26. s.spawn(move || {
  27. {
  28. let mut guard = data_lock0
  29. .try_lock(0)
  30. .expect("T0: Failed to lock offset 0.");
  31. guard[0][0] = 100; // Write to data[0]
  32. guard[1][0] = 200; // Write to data[4]
  33. }
  34. barrier0.wait(); // Synchronize with second thread.
  35. {
  36. let guard = data_lock0
  37. .try_lock(1)
  38. .expect("T0: Failed to lock offset 1.");
  39. assert_eq!(guard[0][0], 1000); // Read from data[1]
  40. assert_eq!(guard[1][0], 2000); // Read from data[5]
  41. }
  42. });
  43. // Spawn second thread.
  44. s.spawn(move || {
  45. {
  46. let mut guard = data_lock1
  47. .try_lock(1)
  48. .expect("T1: Failed to lock offset 1.");
  49. guard[0][0] = 1000; // Write to data[1]
  50. guard[1][0] = 2000; // Write to data[5]
  51. }
  52. barrier1.wait(); // Synchronize with first thread.
  53. {
  54. let guard = data_lock1
  55. .try_lock(0)
  56. .expect("T1: Failed to lock offset 0.");
  57. assert_eq!(guard[0][0], 100); // Read from data[0]
  58. assert_eq!(guard[1][0], 200); // Read from data[5]
  59. }
  60. });
  61. });
  62. // Unwrap the data from the lock.
  63. let data = Arc::try_unwrap(data_lock2)
  64. .expect("Arc unwrap failed")
  65. .into_inner();
  66. // Check the data that has been modified by the threads.
  67. assert_eq!(
  68. data,
  69. vec![100, 1000, 12, 13, 200, 2000, 22, 23, 30, 31, 32, 33]
  70. );
  71. }
  72. #[test]
  73. fn test_conflict() {
  74. let data = vec![10, 11, 12, 13, 20, 21, 22, 23, 30, 31, 32, 33];
  75. let data_lock0 = Arc::new(RepVecRangeLock::new(data, 1, 4));
  76. let data_lock1 = Arc::clone(&data_lock0);
  77. let barrier0 = Arc::new(Barrier::new(2));
  78. let barrier1 = Arc::clone(&barrier0);
  79. thread::scope(|s| {
  80. s.spawn(move || {
  81. let mut _guard = data_lock0.try_lock(0).expect("T0: Failed to offset 0.");
  82. barrier0.wait();
  83. // try_lock() conflict happens in second thread.
  84. barrier0.wait();
  85. });
  86. s.spawn(move || {
  87. barrier1.wait();
  88. // thread0 holds lock offset 0.
  89. assert!(data_lock1.try_lock(0).is_err());
  90. barrier1.wait();
  91. });
  92. });
  93. }
  94. // vim: ts=4 sw=4 expandtab