xdrpp
RFC4506 XDR compiler and message library
Classes | Public Member Functions | Static Public Member Functions | List of all members
xdr::pollset_plus Class Reference

Adds support for signal handlers, asynchonous events, and callbacks injected from other threads to the basic functionality in pollset_light. More...

#include <pollset.h>

Inheritance diagram for xdr::pollset_plus:
Inheritance graph
[legend]
Collaboration diagram for xdr::pollset_plus:
Collaboration graph
[legend]

Public Member Functions

bool pending () const override
 Returns false if no file descriptor callbacks are registered and no timeouts or asynchronous events are pending. More...
 
void wake ()
 Cause PollSet::poll to return if it is sleeping. More...
 
template<typename CB >
void inject_cb (CB &&cb)
 Inject a callback to run immediately. More...
 
template<typename Work , typename CB >
void async (Work &&work, CB &&cb)
 Execute a task asynchonously in another thread, then run callback on the task's result in the main thread. More...
 
void signal_cb (int sig, cb_t cb)
 Add a callback for a particular signal. More...
 
- Public Member Functions inherited from xdr::pollset
 pollset (const pollset &)=delete
 
void poll (int timeout=-1)
 Go through one round of checking all file descriptors. More...
 
void run ()
 Continously poll and only return on exception or when there is no more work to do. More...
 
template<typename CB >
void fd_cb (sock_t s, op_t op, CB &&cb)
 Set a read or write callback on a particular file descriptor. More...
 
void fd_cb (sock_t s, op_t op, std::nullptr_t=nullptr)
 Remove a callback on a file descriptor. More...
 
template<typename CB >
Timeout timeout (std::int64_t ms, CB &&cb)
 Set a callback to run a certain number of milliseconds from now. More...
 
template<typename CB >
Timeout timeout_at (std::int64_t ms, CB &&cb)
 Set a callback to run at a specific time (as returned by PollSet::now_ms()). More...
 
void timeout_cancel (Timeout &t)
 Cancel a pending timeout. More...
 
std::int64_t timeout_time (Timeout t) const
 Returns the absolute time (in milliseconds) at which a timeout will run. More...
 
void timeout_reschedule_at (Timeout &t, std::int64_t ms)
 Reschedule a timeout to run at a specific time. More...
 
void timeout_reschedule (Timeout &t, std::int64_t ms)
 Reschedule a timeout some number of milliseconds in the future.
 

Static Public Member Functions

static void signal_cb (int sig, std::nullptr_t=nullptr)
 Remove any previously added callback for a particular signal. More...
 
- Static Public Member Functions inherited from xdr::pollset
static std::int64_t now_ms ()
 Number of milliseconds since an arbitrary but fixed time, used as the basis of all timeouts. More...
 
static Timeout timeout_null ()
 An invalid timeout, useful for initializing PollSet::Timeout values before a timeout has been scheduled. More...
 

Additional Inherited Members

- Public Types inherited from xdr::pollset
enum  op_t {
  Read = kReadFlag, Write = kWriteFlag, ReadWrite = kReadFlag | kWriteFlag, ReadOnce = kReadFlag | kOnceFlag,
  WriteOnce = kWriteFlag | kOnceFlag
}
 
using cb_t = std::function< void()>
 
- Protected Member Functions inherited from xdr::pollset
std::size_t num_cbs () const
 Number of registered file decriptor and timeout callbacks.
 
- Static Protected Attributes inherited from xdr::pollset
static constexpr int kReadFlag = 0x1
 
static constexpr int kWriteFlag = 0x2
 
static constexpr int kOnceFlag = 0x4
 

Detailed Description

Adds support for signal handlers, asynchonous events, and callbacks injected from other threads to the basic functionality in pollset_light.

Definition at line 182 of file pollset.h.

Member Function Documentation

◆ pending()

bool xdr::pollset_plus::pending ( ) const
overridevirtual

Returns false if no file descriptor callbacks are registered and no timeouts or asynchronous events are pending.

If it returns false, then PollSet::poll will pause forever in the absence of a signal or a call to PollSet::inject_cb in a different thread.

Reimplemented from xdr::pollset.

Definition at line 194 of file pollset.cc.

◆ wake()

void xdr::pollset_plus::wake ( )
inline

Cause PollSet::poll to return if it is sleeping.

Unlike most other methods, wake is safe to call from a signal handler or a different thread.

Definition at line 240 of file pollset.h.

◆ inject_cb()

template<typename CB >
void xdr::pollset_plus::inject_cb ( CB &&  cb)
inline

Inject a callback to run immediately.

Unlike most methods, it is safe to call this function from another thread. Being thread-safe adds extra overhead, so it does not make sense to call this function from the same thread as PollSet::poll. Note that inject_cb acquires a lock and definitely must not be called from a signal handler (or deadlock could ensue).

Definition at line 248 of file pollset.h.

◆ async()

template<typename Work , typename CB >
void xdr::pollset_plus::async ( Work &&  work,
CB &&  cb 
)
inline

Execute a task asynchonously in another thread, then run callback on the task's result in the main thread.

  • work is a task to perform asynchronously in another thread, and must be convertible to std::function<R()> for some type R.
  • cb is the callback that processes the result in the main thread, and must be convertible to std::function<void(R)> for the same type R.

Definition at line 264 of file pollset.h.

◆ signal_cb() [1/2]

void xdr::pollset_plus::signal_cb ( int  sig,
cb_t  cb 
)

Add a callback for a particular signal.

Note that only one callback can be added for a particular signal across all pollset_plus instances in a single process. Hence, calling this function may "steal" a signal from a different pollset_plus (erasing whatever callback the other pollset_plus had for the signal). Such callback stealing is atomic, allowing one to steal a pollset_plus's signals before deleting it with no risk of signals going uncaught.

Definition at line 340 of file pollset.cc.

◆ signal_cb() [2/2]

void xdr::pollset_plus::signal_cb ( int  sig,
std::nullptr_t  = nullptr 
)
static

Remove any previously added callback for a particular signal.

Because signal callbacks are process-wide, this static method will affect whatever pollset_plus currently owns the signal.

Definition at line 401 of file pollset.cc.


The documentation for this class was generated from the following files: