1 // Copyright 2023 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 16 #ifndef ABSL_SYNCHRONIZATION_INTERNAL_SEM_WAITER_H_ 17 #define ABSL_SYNCHRONIZATION_INTERNAL_SEM_WAITER_H_ 18 19 #include "absl/base/config.h" 20 21 #ifdef ABSL_HAVE_SEMAPHORE_H 22 #include <semaphore.h> 23 24 #include <atomic> 25 #include <cstdint> 26 27 #include "absl/base/internal/thread_identity.h" 28 #include "absl/synchronization/internal/futex.h" 29 #include "absl/synchronization/internal/kernel_timeout.h" 30 #include "absl/synchronization/internal/waiter_base.h" 31 32 namespace absl { 33 ABSL_NAMESPACE_BEGIN 34 namespace synchronization_internal { 35 36 #define ABSL_INTERNAL_HAVE_SEM_WAITER 1 37 38 class SemWaiter : public WaiterCrtp<SemWaiter> { 39 public: 40 SemWaiter(); 41 42 bool Wait(KernelTimeout t); 43 void Post(); 44 void Poke(); 45 46 static constexpr char kName[] = "SemWaiter"; 47 48 private: 49 int TimedWait(KernelTimeout t); 50 51 sem_t sem_; 52 53 // This seems superfluous, but for Poke() we need to cause spurious 54 // wakeups on the semaphore. Hence we can't actually use the 55 // semaphore's count. 56 std::atomic<int> wakeups_; 57 }; 58 59 } // namespace synchronization_internal 60 ABSL_NAMESPACE_END 61 } // namespace absl 62 63 #endif // ABSL_HAVE_SEMAPHORE_H 64 65 #endif // ABSL_SYNCHRONIZATION_INTERNAL_SEM_WAITER_H_ 66