IlmThreadSemaphore.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_ILM_THREAD_SEMAPHORE_H
  6. #define INCLUDED_ILM_THREAD_SEMAPHORE_H
  7. //-----------------------------------------------------------------------------
  8. //
  9. // class Semaphore -- a wrapper class for
  10. // system-dependent counting semaphores
  11. //
  12. //-----------------------------------------------------------------------------
  13. #include "IlmThreadExport.h"
  14. #include "IlmThreadConfig.h"
  15. #include "IlmThreadNamespace.h"
  16. //
  17. // Decipher the platform-specific threading support.
  18. // Set the ILMTHREAD_SEMAPHORE_* defines to indicate the corresponding
  19. // implementation of the Semaphore class. Only one of these should be
  20. // defined.
  21. //
  22. #if ILMTHREAD_THREADING_ENABLED
  23. # if ILMTHREAD_HAVE_POSIX_SEMAPHORES
  24. # include <semaphore.h>
  25. # define ILMTHREAD_SEMAPHORE_POSIX 1
  26. # elif defined(__APPLE__)
  27. # include <AvailabilityMacros.h>
  28. # if MAC_OS_X_VERSION_MIN_REQUIRED > 1050 && !defined(__ppc__)
  29. # include <dispatch/dispatch.h>
  30. # define ILMTHREAD_SEMAPHORE_OSX 1
  31. # else
  32. # include <condition_variable>
  33. # include <mutex>
  34. # define ILMTHREAD_SEMAPHORE_OTHER 1
  35. # endif
  36. # elif (defined(_WIN32) || defined(_WIN64))
  37. # ifdef NOMINMAX
  38. # undef NOMINMAX
  39. # endif
  40. # define NOMINMAX
  41. # include <windows.h>
  42. # define ILMTHREAD_SEMAPHORE_WINDOWS 1
  43. # else
  44. # include <condition_variable>
  45. # include <mutex>
  46. # define ILMTHREAD_SEMAPHORE_OTHER 1
  47. # endif
  48. #else
  49. # define ILMTHREAD_SEMAPHORE_DISABLED 1
  50. #endif
  51. ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER
  52. class ILMTHREAD_EXPORT_TYPE Semaphore
  53. {
  54. public:
  55. ILMTHREAD_EXPORT Semaphore (unsigned int value = 0);
  56. ILMTHREAD_EXPORT virtual ~Semaphore();
  57. ILMTHREAD_EXPORT void wait();
  58. ILMTHREAD_EXPORT bool tryWait();
  59. ILMTHREAD_EXPORT void post();
  60. ILMTHREAD_EXPORT int value() const;
  61. private:
  62. #if ILMTHREAD_SEMAPHORE_POSIX
  63. mutable sem_t _semaphore;
  64. #elif ILMTHREAD_SEMAPHORE_OSX
  65. mutable dispatch_semaphore_t _semaphore;
  66. #elif ILMTHREAD_SEMAPHORE_WINDOWS
  67. mutable HANDLE _semaphore;
  68. #elif ILMTHREAD_SEMAPHORE_OTHER
  69. //
  70. // If the platform has threads but no semaphores,
  71. // then we implement them ourselves using condition variables
  72. //
  73. struct sema_t
  74. {
  75. unsigned int count;
  76. unsigned long numWaiting;
  77. std::mutex mutex;
  78. std::condition_variable nonZero;
  79. };
  80. mutable sema_t _semaphore;
  81. #endif
  82. void operator = (const Semaphore& s) = delete;
  83. Semaphore (const Semaphore& s) = delete;
  84. void operator = (Semaphore&& s) = delete;
  85. Semaphore (Semaphore&& s) = delete;
  86. };
  87. ILMTHREAD_INTERNAL_NAMESPACE_HEADER_EXIT
  88. #endif // INCLUDED_ILM_THREAD_SEMAPHORE_H