@@ -14,7 +14,8 @@ These include:
1414* Awaitable Types
1515 * ` single_consumer_event `
1616 * ` async_mutex `
17- * ` async_manual_reset_event ` (coming)
17+ * ` async_manual_reset_event `
18+ * ` async_auto_reset_event `
1819* Functions
1920 * ` when_all() ` (coming)
2021* Cancellation
@@ -753,6 +754,156 @@ cppcoro::task<> add_item(std::string value)
753754}
754755```
755756
757+ ## ` async_manual_reset_event `
758+
759+ A manual-reset event is a coroutine/thread-synchronisation primitive that allows one or more threads
760+ to wait until the event is signalled by a thread that calls ` set() ` .
761+
762+ The event is in one of two states; * 'set'* and * 'not set'* .
763+
764+ If the event is in the * 'set'* state when a coroutine awaits the event then the coroutine
765+ continues without suspending. However if the coroutine is in the * 'not set'* state then the
766+ coroutine is suspended until some thread subsequently calls the ` set() ` method.
767+
768+ Any threads that were suspended while waiting for the event to become * 'set'* will be resumed
769+ inside the next call to ` set() ` by some thread.
770+
771+ Note that you must ensure that no coroutines are awaiting a * 'not set'* event when the
772+ event is destructed as they will not be resumed.
773+
774+ Example:
775+ ``` c++
776+ cppcoro::async_manual_reset_event event;
777+ std::string value;
778+
779+ void producer ()
780+ {
781+ value = get_some_string_value();
782+
783+ // Publish a value by setting the event.
784+ event.set();
785+ }
786+
787+ // Can be called many times to create many tasks.
788+ // All consumer tasks will wait until value has been published.
789+ cppcoro::task<> consumer ()
790+ {
791+ // Wait until value has been published by awaiting event.
792+ co_await event;
793+
794+ consume_value (value);
795+ }
796+ ```
797+
798+ API Summary:
799+ ``` c++
800+ namespace cppcoro
801+ {
802+ class async_manual_reset_event_operation;
803+
804+ class async_manual_reset_event
805+ {
806+ public:
807+ async_manual_reset_event(bool initiallySet = false) noexcept;
808+ ~ async_manual_reset_event();
809+
810+ async_manual_reset_event (const async_manual_reset_event&) = delete;
811+ async_manual_reset_event(async_manual_reset_event&&) = delete;
812+ async_manual_reset_event& operator=(const async_manual_reset_event&) = delete;
813+ async_manual_reset_event& operator=(async_manual_reset_event&&) = delete;
814+
815+ // Wait until the event becomes set.
816+ <unspecified> operator co_await() const noexcept;
817+
818+ bool is_set() const noexcept;
819+
820+ void set() noexcept;
821+
822+ void reset() noexcept;
823+
824+ };
825+
826+ class async_manual_reset_event
827+ {
828+ public:
829+ async_manual_reset_event_operation(async_manual_reset_event& event) noexcept;
830+
831+ bool await_ready() const noexcept;
832+ bool await_suspend(std::experimental::coroutine_handle<> awaiter) noexcept;
833+ void await_resume() const noexcept;
834+ };
835+ }
836+ ```
837+
838+ ## `async_auto_reset_event`
839+
840+ An auto-reset event is a coroutine/thread-synchronisation primitive that allows one or more threads
841+ to wait until the event is signalled by a thread by calling `set()`.
842+
843+ Once a coroutine that is awaiting the event is released by either a prior or subsequent call to `set()`
844+ the event is automatically reset back to the 'not set' state.
845+
846+ API Summary:
847+ ```c++
848+ // <cppcoro/async_auto_reset_event.hpp>
849+ namespace cppcoro
850+ {
851+ class async_auto_reset_event_operation;
852+
853+ class async_auto_reset_event
854+ {
855+ public:
856+
857+ async_auto_reset_event(bool initiallySet = false) noexcept;
858+
859+ ~async_auto_reset_event();
860+
861+ async_auto_reset_event(const async_auto_reset_event&) = delete;
862+ async_auto_reset_event(async_auto_reset_event&&) = delete;
863+ async_auto_reset_event& operator=(const async_auto_reset_event&) = delete;
864+ async_auto_reset_event& operator=(async_auto_reset_event&&) = delete;
865+
866+ // Wait for the event to enter the 'set' state.
867+ //
868+ // If the event is already 'set' then the event is set to the 'not set'
869+ // state and the awaiting coroutine continues without suspending.
870+ // Otherwise, the coroutine is suspended and later resumed when some
871+ // thread calls 'set()'.
872+ //
873+ // Note that the coroutine may be resumed inside a call to 'set()'
874+ // or inside another thread's call to 'operator co_await()'.
875+ async_auto_reset_event_operation operator co_await() const noexcept;
876+
877+ // Set the state of the event to 'set'.
878+ //
879+ // If there are pending coroutines awaiting the event then one
880+ // pending coroutine is resumed and the state is immediately
881+ // set back to the 'not set' state.
882+ //
883+ // This operation is a no-op if the event was already 'set'.
884+ void set() noexcept;
885+
886+ // Set the state of the event to 'not-set'.
887+ //
888+ // This is a no-op if the state was already 'not set'.
889+ void reset() noexcept;
890+
891+ };
892+
893+ class async_auto_reset_event_operation
894+ {
895+ public:
896+ explicit async_auto_reset_event_operation(async_auto_reset_event& event) noexcept;
897+ async_auto_reset_event_operation(const async_auto_reset_event_operation& other) noexcept;
898+
899+ bool await_ready() const noexcept;
900+ bool await_suspend(std::experimental::coroutine_handle<> awaiter) noexcept;
901+ void await_resume() const noexcept;
902+
903+ };
904+ }
905+ ```
906+
756907## ` cancellation_token `
757908
758909A ` cancellation_token ` is a value that can be passed to a function that allows the caller to subsequently communicate a request to cancel the operation to that function.
0 commit comments