low_level_scheduling.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Core interfaces and definitions used by by low-level interfaces such as
  16. // SpinLock.
  17. #ifndef ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_
  18. #define ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_
  19. #include "absl/base/internal/raw_logging.h"
  20. #include "absl/base/internal/scheduling_mode.h"
  21. #include "absl/base/macros.h"
  22. // The following two declarations exist so SchedulingGuard may friend them with
  23. // the appropriate language linkage. These callbacks allow libc internals, such
  24. // as function level statics, to schedule cooperatively when locking.
  25. extern "C" bool __google_disable_rescheduling(void);
  26. extern "C" void __google_enable_rescheduling(bool disable_result);
  27. namespace absl {
  28. ABSL_NAMESPACE_BEGIN
  29. class CondVar;
  30. class Mutex;
  31. namespace synchronization_internal {
  32. int MutexDelay(int32_t c, int mode);
  33. } // namespace synchronization_internal
  34. namespace base_internal {
  35. class SchedulingHelper; // To allow use of SchedulingGuard.
  36. class SpinLock; // To allow use of SchedulingGuard.
  37. // SchedulingGuard
  38. // Provides guard semantics that may be used to disable cooperative rescheduling
  39. // of the calling thread within specific program blocks. This is used to
  40. // protect resources (e.g. low-level SpinLocks or Domain code) that cooperative
  41. // scheduling depends on.
  42. //
  43. // Domain implementations capable of rescheduling in reaction to involuntary
  44. // kernel thread actions (e.g blocking due to a pagefault or syscall) must
  45. // guarantee that an annotated thread is not allowed to (cooperatively)
  46. // reschedule until the annotated region is complete.
  47. //
  48. // It is an error to attempt to use a cooperatively scheduled resource (e.g.
  49. // Mutex) within a rescheduling-disabled region.
  50. //
  51. // All methods are async-signal safe.
  52. class SchedulingGuard {
  53. public:
  54. // Returns true iff the calling thread may be cooperatively rescheduled.
  55. static bool ReschedulingIsAllowed();
  56. SchedulingGuard(const SchedulingGuard&) = delete;
  57. SchedulingGuard& operator=(const SchedulingGuard&) = delete;
  58. private:
  59. // Disable cooperative rescheduling of the calling thread. It may still
  60. // initiate scheduling operations (e.g. wake-ups), however, it may not itself
  61. // reschedule. Nestable. The returned result is opaque, clients should not
  62. // attempt to interpret it.
  63. // REQUIRES: Result must be passed to a pairing EnableScheduling().
  64. static bool DisableRescheduling();
  65. // Marks the end of a rescheduling disabled region, previously started by
  66. // DisableRescheduling().
  67. // REQUIRES: Pairs with innermost call (and result) of DisableRescheduling().
  68. static void EnableRescheduling(bool disable_result);
  69. // A scoped helper for {Disable, Enable}Rescheduling().
  70. // REQUIRES: destructor must run in same thread as constructor.
  71. struct ScopedDisable {
  72. ScopedDisable() { disabled = SchedulingGuard::DisableRescheduling(); }
  73. ~ScopedDisable() { SchedulingGuard::EnableRescheduling(disabled); }
  74. bool disabled;
  75. };
  76. // A scoped helper to enable rescheduling temporarily.
  77. // REQUIRES: destructor must run in same thread as constructor.
  78. class ScopedEnable {
  79. public:
  80. ScopedEnable();
  81. ~ScopedEnable();
  82. private:
  83. int scheduling_disabled_depth_;
  84. };
  85. // Access to SchedulingGuard is explicitly permitted.
  86. friend class absl::CondVar;
  87. friend class absl::Mutex;
  88. friend class SchedulingHelper;
  89. friend class SpinLock;
  90. friend int absl::synchronization_internal::MutexDelay(int32_t c, int mode);
  91. };
  92. //------------------------------------------------------------------------------
  93. // End of public interfaces.
  94. //------------------------------------------------------------------------------
  95. inline bool SchedulingGuard::ReschedulingIsAllowed() {
  96. return false;
  97. }
  98. inline bool SchedulingGuard::DisableRescheduling() {
  99. return false;
  100. }
  101. inline void SchedulingGuard::EnableRescheduling(bool /* disable_result */) {
  102. return;
  103. }
  104. inline SchedulingGuard::ScopedEnable::ScopedEnable()
  105. : scheduling_disabled_depth_(0) {}
  106. inline SchedulingGuard::ScopedEnable::~ScopedEnable() {
  107. ABSL_RAW_CHECK(scheduling_disabled_depth_ == 0, "disable unused warning");
  108. }
  109. } // namespace base_internal
  110. ABSL_NAMESPACE_END
  111. } // namespace absl
  112. #endif // ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_