00001 // Copyright 2017 The Abseil Authors. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // https://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 // 00015 // Core interfaces and definitions used by by low-level interfaces such as 00016 // SpinLock. 00017 00018 #ifndef ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_ 00019 #define ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_ 00020 00021 #include "absl/base/internal/scheduling_mode.h" 00022 #include "absl/base/macros.h" 00023 00024 // The following two declarations exist so SchedulingGuard may friend them with 00025 // the appropriate language linkage. These callbacks allow libc internals, such 00026 // as function level statics, to schedule cooperatively when locking. 00027 extern "C" bool __google_disable_rescheduling(void); 00028 extern "C" void __google_enable_rescheduling(bool disable_result); 00029 00030 namespace absl { 00031 namespace base_internal { 00032 00033 class SchedulingHelper; // To allow use of SchedulingGuard. 00034 class SpinLock; // To allow use of SchedulingGuard. 00035 00036 // SchedulingGuard 00037 // Provides guard semantics that may be used to disable cooperative rescheduling 00038 // of the calling thread within specific program blocks. This is used to 00039 // protect resources (e.g. low-level SpinLocks or Domain code) that cooperative 00040 // scheduling depends on. 00041 // 00042 // Domain implementations capable of rescheduling in reaction to involuntary 00043 // kernel thread actions (e.g blocking due to a pagefault or syscall) must 00044 // guarantee that an annotated thread is not allowed to (cooperatively) 00045 // reschedule until the annotated region is complete. 00046 // 00047 // It is an error to attempt to use a cooperatively scheduled resource (e.g. 00048 // Mutex) within a rescheduling-disabled region. 00049 // 00050 // All methods are async-signal safe. 00051 class SchedulingGuard { 00052 public: 00053 // Returns true iff the calling thread may be cooperatively rescheduled. 00054 static bool ReschedulingIsAllowed(); 00055 00056 private: 00057 // Disable cooperative rescheduling of the calling thread. It may still 00058 // initiate scheduling operations (e.g. wake-ups), however, it may not itself 00059 // reschedule. Nestable. The returned result is opaque, clients should not 00060 // attempt to interpret it. 00061 // REQUIRES: Result must be passed to a pairing EnableScheduling(). 00062 static bool DisableRescheduling(); 00063 00064 // Marks the end of a rescheduling disabled region, previously started by 00065 // DisableRescheduling(). 00066 // REQUIRES: Pairs with innermost call (and result) of DisableRescheduling(). 00067 static void EnableRescheduling(bool disable_result); 00068 00069 // A scoped helper for {Disable, Enable}Rescheduling(). 00070 // REQUIRES: destructor must run in same thread as constructor. 00071 struct ScopedDisable { 00072 ScopedDisable() { disabled = SchedulingGuard::DisableRescheduling(); } 00073 ~ScopedDisable() { SchedulingGuard::EnableRescheduling(disabled); } 00074 00075 bool disabled; 00076 }; 00077 00078 // Access to SchedulingGuard is explicitly white-listed. 00079 friend class SchedulingHelper; 00080 friend class SpinLock; 00081 00082 SchedulingGuard(const SchedulingGuard&) = delete; 00083 SchedulingGuard& operator=(const SchedulingGuard&) = delete; 00084 }; 00085 00086 //------------------------------------------------------------------------------ 00087 // End of public interfaces. 00088 //------------------------------------------------------------------------------ 00089 00090 inline bool SchedulingGuard::ReschedulingIsAllowed() { 00091 return false; 00092 } 00093 00094 inline bool SchedulingGuard::DisableRescheduling() { 00095 return false; 00096 } 00097 00098 inline void SchedulingGuard::EnableRescheduling(bool /* disable_result */) { 00099 return; 00100 } 00101 00102 } // namespace base_internal 00103 } // namespace absl 00104 00105 #endif // ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_