Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:51

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // Copyright (c) Lewis Baker
0003 // Licenced under MIT license. See LICENSE.txt for details.
0004 ///////////////////////////////////////////////////////////////////////////////
0005 #ifndef CPPCORO_CANCELLATION_REGISTRATION_HPP_INCLUDED
0006 #define CPPCORO_CANCELLATION_REGISTRATION_HPP_INCLUDED
0007 
0008 #include <cppcoro/cancellation_token.hpp>
0009 
0010 #include <functional>
0011 #include <utility>
0012 #include <type_traits>
0013 #include <atomic>
0014 #include <cstdint>
0015 
0016 namespace cppcoro
0017 {
0018     namespace detail
0019     {
0020         class cancellation_state;
0021         struct cancellation_registration_list_chunk;
0022         struct cancellation_registration_state;
0023     }
0024 
0025     class cancellation_registration
0026     {
0027     public:
0028 
0029         /// Registers the callback to be executed when cancellation is requested
0030         /// on the cancellation_token.
0031         ///
0032         /// The callback will be executed if cancellation is requested for the
0033         /// specified cancellation token. If cancellation has already been requested
0034         /// then the callback will be executed immediately, before the constructor
0035         /// returns. If cancellation has not yet been requested then the callback
0036         /// will be executed on the first thread to request cancellation inside
0037         /// the call to cancellation_source::request_cancellation().
0038         ///
0039         /// \param token
0040         /// The cancellation token to register the callback with.
0041         ///
0042         /// \param callback
0043         /// The callback to be executed when cancellation is requested on the
0044         /// the cancellation_token. Note that callback must not throw an exception
0045         /// if called when cancellation is requested otherwise std::terminate()
0046         /// will be called.
0047         ///
0048         /// \throw std::bad_alloc
0049         /// If registration failed due to insufficient memory available.
0050         template<
0051             typename FUNC,
0052             typename = std::enable_if_t<std::is_constructible_v<std::function<void()>, FUNC&&>>>
0053         cancellation_registration(cancellation_token token, FUNC&& callback)
0054             : m_callback(std::forward<FUNC>(callback))
0055         {
0056             register_callback(std::move(token));
0057         }
0058 
0059         cancellation_registration(const cancellation_registration& other) = delete;
0060         cancellation_registration& operator=(const cancellation_registration& other) = delete;
0061 
0062         /// Deregisters the callback.
0063         ///
0064         /// After the destructor returns it is guaranteed that the callback
0065         /// will not be subsequently called during a call to request_cancellation()
0066         /// on the cancellation_source.
0067         ///
0068         /// This may block if cancellation has been requested on another thread
0069         /// is it will need to wait until this callback has finished executing
0070         /// before the callback can be destroyed.
0071         ~cancellation_registration();
0072 
0073     private:
0074 
0075         friend class detail::cancellation_state;
0076         friend struct detail::cancellation_registration_state;
0077 
0078         void register_callback(cancellation_token&& token);
0079 
0080         detail::cancellation_state* m_state;
0081         std::function<void()> m_callback;
0082         detail::cancellation_registration_list_chunk* m_chunk;
0083         std::uint32_t m_entryIndex;
0084     };
0085 }
0086 
0087 #endif