win32_waiter.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_WIN32_WAITER_H_
  16. #define Y_ABSL_SYNCHRONIZATION_INTERNAL_WIN32_WAITER_H_
  17. #ifdef _WIN32
  18. #include <sdkddkver.h>
  19. #endif
  20. #if defined(_WIN32) && !defined(__MINGW32__) && \
  21. _WIN32_WINNT >= _WIN32_WINNT_VISTA
  22. #include "y_absl/base/config.h"
  23. #include "y_absl/synchronization/internal/kernel_timeout.h"
  24. #include "y_absl/synchronization/internal/waiter_base.h"
  25. namespace y_absl {
  26. Y_ABSL_NAMESPACE_BEGIN
  27. namespace synchronization_internal {
  28. #define Y_ABSL_INTERNAL_HAVE_WIN32_WAITER 1
  29. class Win32Waiter : public WaiterCrtp<Win32Waiter> {
  30. public:
  31. Win32Waiter();
  32. bool Wait(KernelTimeout t);
  33. void Post();
  34. void Poke();
  35. static constexpr char kName[] = "Win32Waiter";
  36. private:
  37. // WinHelper - Used to define utilities for accessing the lock and
  38. // condition variable storage once the types are complete.
  39. class WinHelper;
  40. // REQUIRES: WinHelper::GetLock(this) must be held.
  41. void InternalCondVarPoke();
  42. // We can't include Windows.h in our headers, so we use aligned character
  43. // buffers to define the storage of SRWLOCK and CONDITION_VARIABLE.
  44. // SRW locks and condition variables do not need to be explicitly destroyed.
  45. // https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-initializesrwlock
  46. // https://stackoverflow.com/questions/28975958/why-does-windows-have-no-deleteconditionvariable-function-to-go-together-with
  47. alignas(void*) unsigned char mu_storage_[sizeof(void*)];
  48. alignas(void*) unsigned char cv_storage_[sizeof(void*)];
  49. int waiter_count_;
  50. int wakeup_count_;
  51. };
  52. } // namespace synchronization_internal
  53. Y_ABSL_NAMESPACE_END
  54. } // namespace y_absl
  55. #endif // defined(_WIN32) && !defined(__MINGW32__) &&
  56. // _WIN32_WINNT >= _WIN32_WINNT_VISTA
  57. #endif // Y_ABSL_SYNCHRONIZATION_INTERNAL_WIN32_WAITER_H_