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
18 #ifndef ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_
19 #define ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_
20
21 #include "absl/base/internal/scheduling_mode.h"
22 #include "absl/base/macros.h"
23
24 // The following two declarations exist so SchedulingGuard may friend them with
25 // the appropriate language linkage. These callbacks allow libc internals, such
26 // as function level statics, to schedule cooperatively when locking.
27 extern "C" bool __google_disable_rescheduling(void);
28 extern "C" void __google_enable_rescheduling(bool disable_result);
29
30 namespace absl {
31 ABSL_NAMESPACE_BEGIN
32 namespace base_internal {
33
34 class SchedulingHelper; // To allow use of SchedulingGuard.
35 class SpinLock; // To allow use of SchedulingGuard.
36
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
57 private:
58 // Disable cooperative rescheduling of the calling thread. It may still
59 // initiate scheduling operations (e.g. wake-ups), however, it may not itself
60 // reschedule. Nestable. The returned result is opaque, clients should not
61 // attempt to interpret it.
62 // REQUIRES: Result must be passed to a pairing EnableScheduling().
63 static bool DisableRescheduling();
64
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
70 // A scoped helper for {Disable, Enable}Rescheduling().
71 // REQUIRES: destructor must run in same thread as constructor.
72 struct ScopedDisable {
ScopedDisableScopedDisable73 ScopedDisable() { disabled = SchedulingGuard::DisableRescheduling(); }
~ScopedDisableScopedDisable74 ~ScopedDisable() { SchedulingGuard::EnableRescheduling(disabled); }
75
76 bool disabled;
77 };
78
79 // Access to SchedulingGuard is explicitly white-listed.
80 friend class SchedulingHelper;
81 friend class SpinLock;
82
83 SchedulingGuard(const SchedulingGuard&) = delete;
84 SchedulingGuard& operator=(const SchedulingGuard&) = delete;
85 };
86
87 //------------------------------------------------------------------------------
88 // End of public interfaces.
89 //------------------------------------------------------------------------------
90
ReschedulingIsAllowed()91 inline bool SchedulingGuard::ReschedulingIsAllowed() {
92 return false;
93 }
94
DisableRescheduling()95 inline bool SchedulingGuard::DisableRescheduling() {
96 return false;
97 }
98
EnableRescheduling(bool)99 inline void SchedulingGuard::EnableRescheduling(bool /* disable_result */) {
100 return;
101 }
102
103 } // namespace base_internal
104 ABSL_NAMESPACE_END
105 } // namespace absl
106
107 #endif // ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_
108