xrcu.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef __XRCU_HPP__
  2. #define __XRCU_HPP__ 1
  3. namespace xrcu
  4. {
  5. // Enter a read-side critical section.
  6. extern void enter_cs ();
  7. // Exit a read-side critical section.
  8. extern void exit_cs ();
  9. // Test if the calling thread is in a read-side critical section.
  10. extern bool in_cs ();
  11. /* Wait until all readers have entered a quiescent state.
  12. * Returns false if a deadlock is detected, true otherwise. */
  13. extern bool sync ();
  14. // Base type for finalizable objects.
  15. struct finalizable
  16. {
  17. finalizable *_Fin_next = nullptr;
  18. virtual void safe_destroy ()
  19. {
  20. delete this;
  21. }
  22. virtual ~finalizable () {}
  23. };
  24. // Add FINP to the list of objects to be finalized after a grace period.
  25. extern void finalize (finalizable *finp);
  26. /* Force destruction of pending finalizable objects.
  27. * Returns true if they were destroyed. */
  28. extern bool flush_finalizers ();
  29. struct cs_guard
  30. {
  31. cs_guard ()
  32. {
  33. enter_cs ();
  34. }
  35. ~cs_guard ()
  36. {
  37. exit_cs ();
  38. }
  39. };
  40. // Miscellaneous functions that don't belong anywhere else.
  41. struct atfork
  42. {
  43. void (*prepare) (void);
  44. void (*parent) (void);
  45. void (*child) (void);
  46. };
  47. // Fetch the `pthread_atfork' callbacks for XRCU.
  48. extern atfork atfork_data ();
  49. // Count the number of trailing zeroes.
  50. #ifdef __GNUC__
  51. inline unsigned int
  52. ctz (unsigned int val)
  53. {
  54. return (__builtin_ctz (val));
  55. }
  56. #else
  57. unsigned int ctz (unsigned int val)
  58. {
  59. unsigned int ret = 0;
  60. val &= ~val + 1; // Isolate the LSB.
  61. ret += !!(val & 0xaaaaaaaau) << 0;
  62. ret += !!(val & 0xccccccccu) << 1;
  63. ret += !!(val & 0xf0f0f0f0u) << 2;
  64. ret += !!(val & 0xff00ff00u) << 3;
  65. ret += !!(val & 0xffff0000u) << 4;
  66. return (ret);
  67. }
  68. #endif
  69. // Generate a pseudo-random number (thread-safe).
  70. extern unsigned int xrand ();
  71. // Get the library version.
  72. extern void library_version (int& major, int& minor);
  73. } // namespace xrcu
  74. #endif