sem_waiter.h 1.7 KB

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