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 Y_ABSL_SYNCHRONIZATION_INTERNAL_SEM_WAITER_H_
  16. #define Y_ABSL_SYNCHRONIZATION_INTERNAL_SEM_WAITER_H_
  17. #include "y_absl/base/config.h"
  18. #ifdef Y_ABSL_HAVE_SEMAPHORE_H
  19. #include <semaphore.h>
  20. #include <atomic>
  21. #include <cstdint>
  22. #include "y_absl/base/internal/thread_identity.h"
  23. #include "y_absl/synchronization/internal/futex.h"
  24. #include "y_absl/synchronization/internal/kernel_timeout.h"
  25. #include "y_absl/synchronization/internal/waiter_base.h"
  26. namespace y_absl {
  27. Y_ABSL_NAMESPACE_BEGIN
  28. namespace synchronization_internal {
  29. #define Y_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. Y_ABSL_NAMESPACE_END
  47. } // namespace y_absl
  48. #endif // Y_ABSL_HAVE_SEMAPHORE_H
  49. #endif // Y_ABSL_SYNCHRONIZATION_INTERNAL_SEM_WAITER_H_