Event_POSIX.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // Event_POSIX.h
  3. //
  4. // Library: Foundation
  5. // Package: Threading
  6. // Module: Event
  7. //
  8. // Definition of the EventImpl class for POSIX Threads.
  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_POSIX_INCLUDED
  16. #define Foundation_Event_POSIX_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Exception.h"
  19. #include <pthread.h>
  20. #include <errno.h>
  21. namespace Poco {
  22. class Foundation_API EventImpl
  23. {
  24. protected:
  25. EventImpl(bool autoReset);
  26. ~EventImpl();
  27. void setImpl();
  28. void waitImpl();
  29. bool waitImpl(long milliseconds);
  30. void resetImpl();
  31. private:
  32. bool _auto;
  33. volatile bool _state;
  34. pthread_mutex_t _mutex;
  35. pthread_cond_t _cond;
  36. };
  37. //
  38. // inlines
  39. //
  40. inline void EventImpl::setImpl()
  41. {
  42. if (pthread_mutex_lock(&_mutex))
  43. throw SystemException("cannot signal event (lock)");
  44. _state = true;
  45. if (pthread_cond_broadcast(&_cond))
  46. {
  47. pthread_mutex_unlock(&_mutex);
  48. throw SystemException("cannot signal event");
  49. }
  50. pthread_mutex_unlock(&_mutex);
  51. }
  52. inline void EventImpl::resetImpl()
  53. {
  54. if (pthread_mutex_lock(&_mutex))
  55. throw SystemException("cannot reset event");
  56. _state = false;
  57. pthread_mutex_unlock(&_mutex);
  58. }
  59. } // namespace Poco
  60. #endif // Foundation_Event_POSIX_INCLUDED