Event.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Event.h
  3. //
  4. // Library: Foundation
  5. // Package: Threading
  6. // Module: Event
  7. //
  8. // Definition of the Event class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_Event_INCLUDED
  16. #define Foundation_Event_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Exception.h"
  19. #if defined(POCO_OS_FAMILY_WINDOWS)
  20. #include "Poco/Event_WIN32.h"
  21. #elif defined(POCO_VXWORKS)
  22. #include "Poco/Event_VX.h"
  23. #else
  24. #include "Poco/Event_POSIX.h"
  25. #endif
  26. namespace Poco {
  27. class Foundation_API Event: private EventImpl
  28. /// An Event is a synchronization object that
  29. /// allows one thread to signal one or more
  30. /// other threads that a certain event
  31. /// has happened.
  32. /// Usually, one thread signals an event,
  33. /// while one or more other threads wait
  34. /// for an event to become signalled.
  35. {
  36. public:
  37. Event(bool autoReset = true);
  38. /// Creates the event. If autoReset is true,
  39. /// the event is automatically reset after
  40. /// a wait() successfully returns.
  41. ~Event();
  42. /// Destroys the event.
  43. void set();
  44. /// Signals the event. If autoReset is true,
  45. /// only one thread waiting for the event
  46. /// can resume execution.
  47. /// If autoReset is false, all waiting threads
  48. /// can resume execution.
  49. void wait();
  50. /// Waits for the event to become signalled.
  51. void wait(long milliseconds);
  52. /// Waits for the event to become signalled.
  53. /// Throws a TimeoutException if the event
  54. /// does not become signalled within the specified
  55. /// time interval.
  56. bool tryWait(long milliseconds);
  57. /// Waits for the event to become signalled.
  58. /// Returns true if the event
  59. /// became signalled within the specified
  60. /// time interval, false otherwise.
  61. void reset();
  62. /// Resets the event to unsignalled state.
  63. private:
  64. Event(const Event&);
  65. Event& operator = (const Event&);
  66. };
  67. //
  68. // inlines
  69. //
  70. inline void Event::set()
  71. {
  72. setImpl();
  73. }
  74. inline void Event::wait()
  75. {
  76. waitImpl();
  77. }
  78. inline void Event::wait(long milliseconds)
  79. {
  80. if (!waitImpl(milliseconds))
  81. throw TimeoutException();
  82. }
  83. inline bool Event::tryWait(long milliseconds)
  84. {
  85. return waitImpl(milliseconds);
  86. }
  87. inline void Event::reset()
  88. {
  89. resetImpl();
  90. }
  91. } // namespace Poco
  92. #endif // Foundation_Event_INCLUDED