Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
AICommon::PollLoop Class Reference

A wrapper around epoll that allows for adding, modifying & deleting of source events. More...

#include <PollLoop.h>

Inheritance diagram for AICommon::PollLoop:
Inheritance graph
[legend]
Collaboration diagram for AICommon::PollLoop:
Collaboration graph
[legend]

Classes

struct  tagPollSourceWrapper
 

Public Member Functions

 PollLoop (const std::string &name, int maxSources=512, long deferredTimeInterval=20)
 Constructs a poll loop with the given name and restrictions. More...
 
virtual ~PollLoop ()
 Destructs the poll loop, tears down the thread if the loop is running.
 
virtual bool start (int priority=-1) override
 Starts the poll thread. More...
 
virtual void stop () override
 Stops the poll loop thread. More...
 
virtual bool addSource (const std::shared_ptr< IPollSource > &source, int fd, uint32_t events) override
 Adds a new event source to the poll loop. More...
 
virtual bool modSource (const std::shared_ptr< IPollSource > &source, uint32_t events) override
 
virtual void delSource (const std::shared_ptr< IPollSource > &source, int fd=-1) override
 
virtual bool hasSource (const std::shared_ptr< IPollSource > &source) override
 Returns true if the specified source is currently installed in the pollLoop. More...
 
virtual std::thread::id threadId () const override
 Returns the thread id of the poll loop thread. More...
 
virtual pid_t gettid () const override
 Returns the linux thread id of the poll loop thread. More...
 

Private Types

typedef struct AICommon::PollLoop::tagPollSourceWrapper PollSourceWrapper
 

Private Member Functions

void init ()
 
void run (const std::string &name, int priority)
 The poll loop thread function. More...
 
void enableDeferredTimer ()
 Enables the deferred timer event source. More...
 
void disableDeferredTimer ()
 Disables the deferred timer event source.
 

Private Attributes

const std::string mName
 
std::thread mEPollThread
 
std::atomic< pid_t > mEPollThreadId
 
int mEPollFd
 
int mDeathEventFd
 
int mDeferTimerFd
 
const struct itimerspec mDeferTimerSpec
 
Spinlock mLock
 
const int mMaxSources
 
int mDeferredSources
 
std::list< PollSourceWrappermSources
 

Detailed Description

A wrapper around epoll that allows for adding, modifying & deleting of source events.

Poll loop sources are a tuple of a shared_ptr<IPollSource>, an fd and a bitmask of events to listen on. PollSource objects are store as weak_ptrs and only locked when they have been triggered and their process() function is to be called.

This should make the race conditions with calling an object that has been destroy safe, however it does means that PollSource objects shouldn't assume that their process() methods won't be called after they've been removed from the poll loop.

Constructor & Destructor Documentation

◆ PollLoop()

PollLoop::PollLoop ( const std::string &  name,
int  maxSources = 512,
long  deferredTimeInterval = 20 
)

Constructs a poll loop with the given name and restrictions.

A constructed poll loop is not automatically started, rather the caller should call start to spawn a thread and run the poll loop inside it.

Parameters
[in]nameThe name to give the poll loop thread (nb: thread names are limited to 16 characters).
[in]maxSourcesThe maximum number of event sources that can be installed on the poll loop.
[in]deferredTimeIntervalThe time interval in milliseconds of the deferred timer

Member Function Documentation

◆ addSource()

bool PollLoop::addSource ( const std::shared_ptr< IPollSource > &  source,
int  fd,
uint32_t  events 
)
overridevirtual

Adds a new event source to the poll loop.

A source is a file descriptor, a bitmask of events to wait for and a IPollSource object that will be called when any of the events in the bitmask occur on the file descriptor.

This method may fail if the number of sources installed exceeds the maximum allowed.

Parameters
[in]sourceThe source object to call process() on when an event occurs
[in]fdThe file descriptor to poll on
[in]eventsA bitmask of events to listen on
Returns
true on success, false on failure.

Implements AICommon::IPollLoop.

◆ enableDeferredTimer()

void PollLoop::enableDeferredTimer ( )
inlineprivate

Enables the deferred timer event source.

This is an internal function that is called when a source event has been 'deferred', i.e. a client has called modSource(..., EPOLLDEFERRED).

◆ gettid()

pid_t PollLoop::gettid ( ) const
overridevirtual

Returns the linux thread id of the poll loop thread.

If the poll loop /threa is not current running -1 will be returned.

Implements AICommon::IPollLoop.

◆ hasSource()

bool PollLoop::hasSource ( const std::shared_ptr< IPollSource > &  source)
overridevirtual

Returns true if the specified source is currently installed in the pollLoop.

Parameters
[in]sourceThe source object to search for in the pollLoop
Returns
True if source installed

Implements AICommon::IPollLoop.

◆ run()

void PollLoop::run ( const std::string &  name,
int  priority 
)
private

The poll loop thread function.

This is the thread that does all the epoll stuff.

◆ start()

bool PollLoop::start ( int  priority = -1)
overridevirtual

Starts the poll thread.

If the poll loop was already running it is stopped and restarted.

Parameters
[in]priorityThe SCRED_RR priority in which to run the poll loop, if -1 the priority is inherited from the calling thread
Returns
true on success, false on failure.

Implements AICommon::IPollLoop.

◆ stop()

void PollLoop::stop ( )
overridevirtual

Stops the poll loop thread.

Stops the poll loop and cleans up all the resources associated with it.

Implements AICommon::IPollLoop.

◆ threadId()

std::thread::id PollLoop::threadId ( ) const
overridevirtual

Returns the thread id of the poll loop thread.

If the poll loop /threa is not current running, a default constructed std::thread::id is returned.

Implements AICommon::IPollLoop.


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