Semaphore.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Semaphore.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Semaphore.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: Semaphore
  9. //
  10. // Definition of the Semaphore class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Semaphore_INCLUDED
  18. #define Foundation_Semaphore_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Exception.h"
  21. #if defined(POCO_OS_FAMILY_WINDOWS)
  22. #include "Poco/Semaphore_WIN32.h"
  23. #elif defined(POCO_VXWORKS)
  24. #include "Poco/Semaphore_VX.h"
  25. #else
  26. #include "Poco/Semaphore_POSIX.h"
  27. #endif
  28. namespace Poco {
  29. class Foundation_API Semaphore: private SemaphoreImpl
  30. /// A Semaphore is a synchronization object with the following
  31. /// characteristics:
  32. /// A semaphore has a value that is constrained to be a non-negative
  33. /// integer and two atomic operations. The allowable operations are V
  34. /// (here called set()) and P (here called wait()). A V (set()) operation
  35. /// increases the value of the semaphore by one.
  36. /// A P (wait()) operation decreases the value of the semaphore by one,
  37. /// provided that can be done without violating the constraint that the
  38. /// value be non-negative. A P (wait()) operation that is initiated when
  39. /// the value of the semaphore is 0 suspends the calling thread.
  40. /// The calling thread may continue when the value becomes positive again.
  41. {
  42. public:
  43. Semaphore(int n);
  44. Semaphore(int n, int max);
  45. /// Creates the semaphore. The current value
  46. /// of the semaphore is given in n. The
  47. /// maximum value of the semaphore is given
  48. /// in max.
  49. /// If only n is given, it must be greater than
  50. /// zero.
  51. /// If both n and max are given, max must be
  52. /// greater than zero, n must be greater than
  53. /// or equal to zero and less than or equal
  54. /// to max.
  55. ~Semaphore();
  56. /// Destroys the semaphore.
  57. void set();
  58. /// Increments the semaphore's value by one and
  59. /// thus signals the semaphore. Another thread
  60. /// waiting for the semaphore will be able
  61. /// to continue.
  62. void wait();
  63. /// Waits for the semaphore to become signalled.
  64. /// To become signalled, a semaphore's value must
  65. /// be greater than zero.
  66. /// Decrements the semaphore's value by one.
  67. void wait(long milliseconds);
  68. /// Waits for the semaphore to become signalled.
  69. /// To become signalled, a semaphore's value must
  70. /// be greater than zero.
  71. /// Throws a TimeoutException if the semaphore
  72. /// does not become signalled within the specified
  73. /// time interval.
  74. /// Decrements the semaphore's value by one
  75. /// if successful.
  76. bool tryWait(long milliseconds);
  77. /// Waits for the semaphore to become signalled.
  78. /// To become signalled, a semaphore's value must
  79. /// be greater than zero.
  80. /// Returns true if the semaphore
  81. /// became signalled within the specified
  82. /// time interval, false otherwise.
  83. /// Decrements the semaphore's value by one
  84. /// if successful.
  85. private:
  86. Semaphore();
  87. Semaphore(const Semaphore&);
  88. Semaphore& operator = (const Semaphore&);
  89. };
  90. //
  91. // inlines
  92. //
  93. inline void Semaphore::set()
  94. {
  95. setImpl();
  96. }
  97. inline void Semaphore::wait()
  98. {
  99. waitImpl();
  100. }
  101. inline void Semaphore::wait(long milliseconds)
  102. {
  103. if (!waitImpl(milliseconds))
  104. throw TimeoutException();
  105. }
  106. inline bool Semaphore::tryWait(long milliseconds)
  107. {
  108. return waitImpl(milliseconds);
  109. }
  110. } // namespace Poco
  111. #endif // Foundation_Semaphore_INCLUDED