xrcu.hpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef __XRCU_TESTS_XRCU__
  2. #define __XRCU_TESTS_XRCU__ 1
  3. #include "../xrcu.hpp"
  4. #include "utils.hpp"
  5. #include <thread>
  6. #include <atomic>
  7. namespace xrcu_test
  8. {
  9. static std::atomic<int> G_CNT;
  10. struct tst_fin : public xrcu::finalizable
  11. {
  12. ~tst_fin ()
  13. {
  14. G_CNT.fetch_add (1);
  15. }
  16. };
  17. void test_xrcu ()
  18. {
  19. xrcu::enter_cs ();
  20. ASSERT (xrcu::in_cs ());
  21. xrcu::exit_cs ();
  22. ASSERT (!xrcu::in_cs ());
  23. xrcu::enter_cs ();
  24. tst_fin *p = new tst_fin ();
  25. finalize (p);
  26. xrcu::flush_finalizers ();
  27. ASSERT (G_CNT.load () == 0);
  28. xrcu::exit_cs ();
  29. ASSERT (G_CNT.load () == 1);
  30. }
  31. static void
  32. mt_xrcu (tst_fin *fp)
  33. {
  34. xrcu::cs_guard g;
  35. finalize (fp);
  36. }
  37. void test_xrcu_mt ()
  38. {
  39. const int NTHREADS = 100;
  40. std::vector<std::thread> thrs;
  41. G_CNT.store (0);
  42. for (int i = 0; i < NTHREADS; ++i)
  43. thrs.push_back (std::thread (mt_xrcu, new tst_fin ()));
  44. for (auto& thr : thrs)
  45. thr.join ();
  46. ASSERT (G_CNT.load () == NTHREADS);
  47. }
  48. test_module xrcu_tests
  49. {
  50. "xrcu",
  51. {
  52. { "API", test_xrcu },
  53. { "API with multiple threads", test_xrcu_mt }
  54. }
  55. };
  56. } // namespace xrcu_test
  57. #endif