Event.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file Event.h
  13. @brief The class \ref kNet::Event Event. Provides a mechanism for inter-thread signalling. */
  14. #ifdef WIN32
  15. #include "win32/WS2Include.h"
  16. #endif
  17. namespace kNet
  18. {
  19. enum EventWaitType
  20. {
  21. EventWaitInvalid, ///< This event is uninitialized.
  22. EventWaitDummy, ///< The event to be waited on is a dummy event. Used to keep the index numbers straight, to avoid a O(n) pass through the whole event wait list.
  23. EventWaitSignal, ///< The event to be waited on is not a socket-based event, but an application-triggered signal event.
  24. EventWaitRead, ///< The event to be waited on is a socket read event.
  25. EventWaitWrite ///< The event to be waited on is a socket write event.
  26. };
  27. /// Event is an inter-thread synchronization primitive that can be in one of two states: Set or Cleared.
  28. /** A thread can avoid busy-waiting on a condition by using an Event to represent the triggering of that condition
  29. and calling its \ref kNet::Event::Wait "Wait()" function to observe when the condition occurs.
  30. Network socket read and write availability can be represented as an Event as well. The \ref kNet::EventArray
  31. EventArray class can be used to wait on list of events. This allows a mixture of socket and application events to
  32. be handled using a single select() call.
  33. To create a new event, call \ref kNet::CreateNewEvent CreateNewEvent with the wait type of EventWaitSignal.
  34. The Event class does not follow RAII or data sharing patterns to avoid thread-safety issues. Therefore it is important
  35. to note that:
  36. - The default ctor initializes a "null" event - one that is not initialized.
  37. - The default copy-ctor copies the event. The two events will be the same, i.e. does not matter which of them to wait or signal.
  38. - The default assignment operator also copies the event, but does NOT free up the internal data used by the old event. When
  39. assigning over an Event that is not needed any more, call \ref kNet::Event::Close "Close()" on that event.
  40. - The default destructor of an Event does NOT delete the event. Before letting the last copy of an Event go out of scope,
  41. manually call \ref kNet::Event::Close "Close()" on that Event.
  42. This class represents a WSAEVENT on Windows, and socket or a pipe on unix. */
  43. class Event
  44. {
  45. public:
  46. /// Creates a null event. Call Event::Create to initialize the event.
  47. Event();
  48. // Does not delete the event. Call Event::Close() when you don't need it anymore.
  49. // ~Event();
  50. /// Initializes the event to a new instance. If the Event was previously initialized, does *not* Close
  51. /// the old instance.
  52. void Create(EventWaitType type);
  53. /// Deinitializes the Event. If there exists any copies of this Event, their state will be undefined.
  54. void Close();
  55. /// Returns true if this event is uninitialized.
  56. bool IsNull() const;
  57. /// Returns true if this event is not null.
  58. bool IsValid() const;
  59. /// Clears the event, i.e. sets it to "0".
  60. void Reset();
  61. /// Sets the event, i.e. sets it to "1".
  62. void Set();
  63. /// Returns true if the event is in set state.
  64. bool Test() const;
  65. /// Returns true if the event was set, or got set during the timeout period, or returns false if the event
  66. /// was not set before the timeout occurred.
  67. bool Wait(unsigned long msecs) const;
  68. /// Returns the underlying type of the event, specifying what kind of system object is being represented by this event.
  69. EventWaitType Type() const { return type; }
  70. private:
  71. EventWaitType type;
  72. #ifdef WIN32
  73. public:
  74. WSAEVENT wsaEvent;
  75. /// Wraps the given WSAEVENT into this event.
  76. explicit Event(WSAEVENT wsaEvent, EventWaitType eventType);
  77. #elif defined(KNET_UNIX) || defined(ANDROID)
  78. public:
  79. int fd[2]; // fd[0] is used for reading, fd[1] for writing.
  80. /// Wraps the given socket file descriptor into this event.
  81. explicit Event(int /*SOCKET*/ fd, EventWaitType eventType);
  82. #endif
  83. };
  84. /// Creates and returns a new event.
  85. Event CreateNewEvent(EventWaitType type);
  86. } // ~kNet