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 00016 // PerThreadSem is a low-level synchronization primitive controlling the 00017 // runnability of a single thread, used internally by Mutex and CondVar. 00018 // 00019 // This is NOT a general-purpose synchronization mechanism, and should not be 00020 // used directly by applications. Applications should use Mutex and CondVar. 00021 // 00022 // The semantics of PerThreadSem are the same as that of a counting semaphore. 00023 // Each thread maintains an abstract "count" value associated with its identity. 00024 00025 #ifndef ABSL_SYNCHRONIZATION_INTERNAL_PER_THREAD_SEM_H_ 00026 #define ABSL_SYNCHRONIZATION_INTERNAL_PER_THREAD_SEM_H_ 00027 00028 #include <atomic> 00029 00030 #include "absl/base/internal/thread_identity.h" 00031 #include "absl/synchronization/internal/create_thread_identity.h" 00032 #include "absl/synchronization/internal/kernel_timeout.h" 00033 00034 namespace absl { 00035 00036 class Mutex; 00037 00038 namespace synchronization_internal { 00039 00040 class PerThreadSem { 00041 public: 00042 PerThreadSem() = delete; 00043 PerThreadSem(const PerThreadSem&) = delete; 00044 PerThreadSem& operator=(const PerThreadSem&) = delete; 00045 00046 // Routine invoked periodically (once a second) by a background thread. 00047 // Has no effect on user-visible state. 00048 static void Tick(base_internal::ThreadIdentity* identity); 00049 00050 // --------------------------------------------------------------------------- 00051 // Routines used by autosizing threadpools to detect when threads are 00052 // blocked. Each thread has a counter pointer, initially zero. If non-zero, 00053 // the implementation atomically increments the counter when it blocks on a 00054 // semaphore, a decrements it again when it wakes. This allows a threadpool 00055 // to keep track of how many of its threads are blocked. 00056 // SetThreadBlockedCounter() should be used only by threadpool 00057 // implementations. GetThreadBlockedCounter() should be used by modules that 00058 // block threads; if the pointer returned is non-zero, the location should be 00059 // incremented before the thread blocks, and decremented after it wakes. 00060 static void SetThreadBlockedCounter(std::atomic<int> *counter); 00061 static std::atomic<int> *GetThreadBlockedCounter(); 00062 00063 private: 00064 // Create the PerThreadSem associated with "identity". Initializes count=0. 00065 // REQUIRES: May only be called by ThreadIdentity. 00066 static void Init(base_internal::ThreadIdentity* identity); 00067 00068 // Increments "identity"'s count. 00069 static inline void Post(base_internal::ThreadIdentity* identity); 00070 00071 // Waits until either our count > 0 or t has expired. 00072 // If count > 0, decrements count and returns true. Otherwise returns false. 00073 // !t.has_timeout() => Wait(t) will return true. 00074 static inline bool Wait(KernelTimeout t); 00075 00076 // White-listed callers. 00077 friend class PerThreadSemTest; 00078 friend class absl::Mutex; 00079 friend absl::base_internal::ThreadIdentity* CreateThreadIdentity(); 00080 }; 00081 00082 } // namespace synchronization_internal 00083 } // namespace absl 00084 00085 // In some build configurations we pass --detect-odr-violations to the 00086 // gold linker. This causes it to flag weak symbol overrides as ODR 00087 // violations. Because ODR only applies to C++ and not C, 00088 // --detect-odr-violations ignores symbols not mangled with C++ names. 00089 // By changing our extension points to be extern "C", we dodge this 00090 // check. 00091 extern "C" { 00092 void AbslInternalPerThreadSemPost( 00093 absl::base_internal::ThreadIdentity* identity); 00094 bool AbslInternalPerThreadSemWait( 00095 absl::synchronization_internal::KernelTimeout t); 00096 } // extern "C" 00097 00098 void absl::synchronization_internal::PerThreadSem::Post( 00099 absl::base_internal::ThreadIdentity* identity) { 00100 AbslInternalPerThreadSemPost(identity); 00101 } 00102 00103 bool absl::synchronization_internal::PerThreadSem::Wait( 00104 absl::synchronization_internal::KernelTimeout t) { 00105 return AbslInternalPerThreadSemWait(t); 00106 } 00107 00108 #endif // ABSL_SYNCHRONIZATION_INTERNAL_PER_THREAD_SEM_H_