Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event: Extract DispatcherBase interface #14446

Merged
merged 3 commits into from
Dec 17, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 58 additions & 51 deletions include/envoy/event/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,67 @@ using PostCb = std::function<void()>;
using PostCbSharedPtr = std::shared_ptr<PostCb>;

/**
* Abstract event dispatching loop.
* Minimal interface to the disptching loop used to create low-level primitives. See Dispatcher
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: sp on dispatching is causing your format failure

* below for the full interface.
*/
class Dispatcher {
class DispatcherBase {
public:
virtual ~Dispatcher() = default;
virtual ~DispatcherBase() = default;

/**
* Creates a file event that will signal when a file is readable or writable. On UNIX systems this
* can be used for any file like interface (files, sockets, etc.).
* @param fd supplies the fd to watch.
* @param cb supplies the callback to fire when the file is ready.
* @param trigger specifies whether to edge or level trigger.
* @param events supplies a logical OR of FileReadyType events that the file event should
* initially listen on.
*/
virtual FileEventPtr createFileEvent(os_fd_t fd, FileReadyCb cb, FileTriggerType trigger,
uint32_t events) PURE;

/**
* Allocates a timer. @see Timer for docs on how to use the timer.
* @param cb supplies the callback to invoke when the timer fires.
*/
virtual Event::TimerPtr createTimer(TimerCb cb) PURE;

/**
* Allocates a schedulable callback. @see SchedulableCallback for docs on how to use the wrapped
* callback.
* @param cb supplies the callback to invoke when the SchedulableCallback is triggered on the
* event loop.
*/
virtual Event::SchedulableCallbackPtr createSchedulableCallback(std::function<void()> cb) PURE;

/**
* Sets a tracked object, which is currently operating in this Dispatcher.
* This should be cleared with another call to setTrackedObject() when the object is done doing
* work. Calling setTrackedObject(nullptr) results in no object being tracked.
*
* This is optimized for performance, to avoid allocation where we do scoped object tracking.
*
* @return The previously tracked object or nullptr if there was none.
*/
virtual const ScopeTrackedObject* setTrackedObject(const ScopeTrackedObject* object) PURE;

/**
* Validates that an operation is thread-safe with respect to this dispatcher; i.e. that the
* current thread of execution is on the same thread upon which the dispatcher loop is running.
*/
virtual bool isThreadSafe() const PURE;

/**
* Returns a recently cached MonotonicTime value.
*/
virtual MonotonicTime approximateMonotonicTime() const PURE;
};

/**
* Abstract event dispatching loop.
*/
class Dispatcher : public DispatcherBase {
public:
/**
* Returns the name that identifies this dispatcher, such as "worker_2" or "main_thread".
* @return const std::string& the name that identifies this dispatcher.
Expand Down Expand Up @@ -136,18 +191,6 @@ class Dispatcher {
createDnsResolver(const std::vector<Network::Address::InstanceConstSharedPtr>& resolvers,
bool use_tcp_for_dns_lookups) PURE;

/**
* Creates a file event that will signal when a file is readable or writable. On UNIX systems this
* can be used for any file like interface (files, sockets, etc.).
* @param fd supplies the fd to watch.
* @param cb supplies the callback to fire when the file is ready.
* @param trigger specifies whether to edge or level trigger.
* @param events supplies a logical OR of FileReadyType events that the file event should
* initially listen on.
*/
virtual FileEventPtr createFileEvent(os_fd_t fd, FileReadyCb cb, FileTriggerType trigger,
uint32_t events) PURE;

/**
* @return Filesystem::WatcherPtr a filesystem watcher owned by the caller.
*/
Expand All @@ -173,20 +216,6 @@ class Dispatcher {
*/
virtual Network::UdpListenerPtr createUdpListener(Network::SocketSharedPtr socket,
Network::UdpListenerCallbacks& cb) PURE;
/**
* Allocates a timer. @see Timer for docs on how to use the timer.
* @param cb supplies the callback to invoke when the timer fires.
*/
virtual Event::TimerPtr createTimer(TimerCb cb) PURE;

/**
* Allocates a schedulable callback. @see SchedulableCallback for docs on how to use the wrapped
* callback.
* @param cb supplies the callback to invoke when the SchedulableCallback is triggered on the
* event loop.
*/
virtual Event::SchedulableCallbackPtr createSchedulableCallback(std::function<void()> cb) PURE;

/**
* Submits an item for deferred delete. @see DeferredDeletable.
*/
Expand Down Expand Up @@ -236,28 +265,6 @@ class Dispatcher {
*/
virtual Buffer::WatermarkFactory& getWatermarkFactory() PURE;

/**
* Sets a tracked object, which is currently operating in this Dispatcher.
* This should be cleared with another call to setTrackedObject() when the object is done doing
* work. Calling setTrackedObject(nullptr) results in no object being tracked.
*
* This is optimized for performance, to avoid allocation where we do scoped object tracking.
*
* @return The previously tracked object or nullptr if there was none.
*/
virtual const ScopeTrackedObject* setTrackedObject(const ScopeTrackedObject* object) PURE;

/**
* Validates that an operation is thread-safe with respect to this dispatcher; i.e. that the
* current thread of execution is on the same thread upon which the dispatcher loop is running.
*/
virtual bool isThreadSafe() const PURE;

/**
* Returns a recently cached MonotonicTime value.
*/
virtual MonotonicTime approximateMonotonicTime() const PURE;

/**
* Updates approximate monotonic time to current value.
*/
Expand Down