xrcu.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Declarations for the RCU API.
  2. This file is part of xrcu.
  3. xrcu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef __XRCU_HPP__
  14. #define __XRCU_HPP__ 1
  15. namespace xrcu
  16. {
  17. // Enter a read-side critical section.
  18. extern void enter_cs ();
  19. // Exit a read-side critical section.
  20. extern void exit_cs ();
  21. // Test if the calling thread is in a read-side critical section.
  22. extern bool in_cs ();
  23. /* Wait until all readers have entered a quiescent state.
  24. * Returns false if a deadlock is detected, true otherwise. */
  25. extern bool sync ();
  26. // Base type for finalizable objects.
  27. struct finalizable
  28. {
  29. finalizable *_Fin_next = nullptr;
  30. virtual void safe_destroy ()
  31. {
  32. ::delete this;
  33. }
  34. virtual ~finalizable () {}
  35. };
  36. // Add FINP to the list of objects to be finalized after a grace period.
  37. extern void finalize (finalizable *finp);
  38. /* Force destruction of pending finalizable objects.
  39. * Returns true if they were destroyed. */
  40. extern bool flush_finalizers ();
  41. struct cs_guard
  42. {
  43. cs_guard ()
  44. {
  45. enter_cs ();
  46. }
  47. ~cs_guard ()
  48. {
  49. exit_cs ();
  50. }
  51. };
  52. // Miscellaneous functions that don't belong anywhere else.
  53. struct atfork
  54. {
  55. void (*prepare) ();
  56. void (*parent) ();
  57. void (*child) ();
  58. };
  59. // Fetch the `pthread_atfork' callbacks for XRCU.
  60. extern atfork atfork_data ();
  61. // Count the number of trailing zeroes.
  62. #ifdef __GNUC__
  63. inline unsigned int
  64. ctz (unsigned int val)
  65. {
  66. return (__builtin_ctz (val));
  67. }
  68. #else
  69. inline unsigned int
  70. ctz (unsigned int val)
  71. {
  72. unsigned int ret = 0;
  73. val &= ~val + 1; // Isolate the LSB.
  74. ret += !!(val & 0xaaaaaaaau) << 0;
  75. ret += !!(val & 0xccccccccu) << 1;
  76. ret += !!(val & 0xf0f0f0f0u) << 2;
  77. ret += !!(val & 0xff00ff00u) << 3;
  78. ret += !!(val & 0xffff0000u) << 4;
  79. return (ret);
  80. }
  81. #endif
  82. // Generate a pseudo-random number (thread-safe).
  83. extern unsigned int xrand ();
  84. // Get the library version.
  85. extern void library_version (int& major, int& minor);
  86. } // namespace xrcu
  87. #endif