Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
PollLoop.h
1/*
2* If not stated otherwise in this file or this component's LICENSE file the
3* following copyright and licenses apply:
4*
5* Copyright 2015 Sky UK
6*
7* Licensed under the Apache License, Version 2.0 (the "License");
8* you may not use this file except in compliance with the License.
9* You may obtain a copy of the License at
10*
11* http://www.apache.org/licenses/LICENSE-2.0
12*
13* Unless required by applicable law or agreed to in writing, software
14* distributed under the License is distributed on an "AS IS" BASIS,
15* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16* See the License for the specific language governing permissions and
17* limitations under the License.
18*/
19/*
20 * File: PollLoop.h
21 *
22 */
23#ifndef POLLLOOP_H
24#define POLLLOOP_H
25
26#include "IPollLoop.h"
27#include "SpinLock.h"
28
29#include <strings.h>
30#include <sys/timerfd.h>
31
32#include <atomic>
33#include <thread>
34#include <string>
35#include <mutex>
36#include <list>
37#include <map>
38
39namespace AICommon
40{
41
42
43// -----------------------------------------------------------------------------
60class PollLoop : public IPollLoop, public std::enable_shared_from_this<PollLoop>
61{
62public:
63 PollLoop(const std::string& name, int maxSources = 512, long deferredTimeInterval = 20);
64 virtual ~PollLoop();
65
66public:
67 virtual bool start(int priority = -1) override;
68 virtual void stop() override;
69
70 virtual bool addSource(const std::shared_ptr<IPollSource>& source, int fd, uint32_t events) override;
71 virtual bool modSource(const std::shared_ptr<IPollSource>& source, uint32_t events) override;
72 virtual void delSource(const std::shared_ptr<IPollSource>& source, int fd = -1) override;
73 virtual bool hasSource(const std::shared_ptr<IPollSource>& source) override;
74
75 virtual std::thread::id threadId() const override;
76 virtual pid_t gettid() const override;
77
78private:
79 void init();
80 void run(const std::string& name, int priority);
81
82 inline void enableDeferredTimer();
83 inline void disableDeferredTimer();
84
85private:
86 // The name given to the poll thread
87 const std::string mName;
88
89 std::thread mEPollThread;
90
91 // The TID of the poll thread
92 std::atomic<pid_t> mEPollThreadId;
93
94 // The actual epoll descriptor, on valid when the epoll loop is running
95 int mEPollFd;
96
97 // The eventfd used to kill the thread (on stop())
98 int mDeathEventFd;
99
100 // A timerfd that is used to wake up epoll sources that previous asked to
101 // defer their processing
102 int mDeferTimerFd;
103
104 // The time period that the defer timer fires
105 const struct itimerspec mDeferTimerSpec;
106
107 // Spinlock protecting access to the source list
108 Spinlock mLock;
109
110 // The maximum number of sources that can be added to the poll loop
111 const int mMaxSources;
112
113 // The number of sources that currently have the EPOLLDEFERRED flag set
114 int mDeferredSources;
115
116private:
117 typedef struct tagPollSourceWrapper
118 {
119 tagPollSourceWrapper(std::shared_ptr<IPollSource> _source, int _fd, uint32_t _events)
120 : source(_source)
121 , fd(_fd)
122 , events(_events)
123 { }
124
125 std::weak_ptr<IPollSource> source;
126 int fd;
127 uint32_t events;
128
130
131 std::list<PollSourceWrapper> mSources;
132};
133
134} // namespace AICommon
135
136#endif // !defined(POLLLOOP_H)
Definition IPollLoop.h:49
A wrapper around epoll that allows for adding, modifying & deleting of source events.
Definition PollLoop.h:61
virtual bool addSource(const std::shared_ptr< IPollSource > &source, int fd, uint32_t events) override
Adds a new event source to the poll loop.
Definition PollLoop.cpp:182
virtual bool start(int priority=-1) override
Starts the poll thread.
Definition PollLoop.cpp:423
virtual bool hasSource(const std::shared_ptr< IPollSource > &source) override
Returns true if the specified source is currently installed in the pollLoop.
Definition PollLoop.cpp:391
void enableDeferredTimer()
Enables the deferred timer event source.
Definition PollLoop.cpp:99
virtual ~PollLoop()
Destructs the poll loop, tears down the thread if the loop is running.
Definition PollLoop.cpp:84
virtual void stop() override
Stops the poll loop thread.
Definition PollLoop.cpp:546
void run(const std::string &name, int priority)
The poll loop thread function.
Definition PollLoop.cpp:618
void disableDeferredTimer()
Disables the deferred timer event source.
Definition PollLoop.cpp:135
virtual pid_t gettid() const override
Returns the linux thread id of the poll loop thread.
Definition PollLoop.cpp:605
virtual std::thread::id threadId() const override
Returns the thread id of the poll loop thread.
Definition PollLoop.cpp:593
Definition SpinLock.h:42