Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
DobbyTimer.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 2016 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: DobbyTimer.h
21  *
22  */
23 #ifndef DOBBYTIMER_H
24 #define DOBBYTIMER_H
25 
26 #include <IDGenerator.h>
27 
28 #include <time.h>
29 
30 #include <functional>
31 #include <set>
32 #include <thread>
33 #include <mutex>
34 #include <chrono>
35 
36 
37 // -----------------------------------------------------------------------------
58 {
59 public:
60  DobbyTimer();
61  ~DobbyTimer();
62 
63 public:
64  void stop();
65 
66 public:
67  int add(const std::chrono::milliseconds& timeout, bool oneShot,
68  const std::function<bool()>& func);
69  bool remove(int timerId);
70 
71 private:
72  void timerThread();
73 
74  void updateTimerFd() const;
75  struct timespec calcAbsTime(const struct timespec& now,
76  const std::chrono::milliseconds& timeout) const;
77 
78 
79 private:
80  typedef struct tagTimerEntry
81  {
82  int id;
83  bool oneshot;
84  struct timespec expiry;
85  std::function<bool()> func;
86  std::chrono::milliseconds timeout;
87 
88  bool isLessThanOrEqualTo(const struct timespec& rhs) const
89  {
90  return (expiry.tv_sec < rhs.tv_sec) ||
91  ((expiry.tv_sec == rhs.tv_sec) &&
92  (expiry.tv_nsec <= rhs.tv_nsec));
93  }
94 
95  } TimerEntry;
96 
98  {
99  public:
100  bool operator() (const TimerEntry& a, const TimerEntry& b)
101  {
102  return (a.expiry.tv_sec < b.expiry.tv_sec) ||
103  ((a.expiry.tv_sec == b.expiry.tv_sec) &&
104  (a.expiry.tv_nsec < b.expiry.tv_nsec));
105  }
106  };
107 
108  std::multiset<TimerEntry, TimerEntryCompare> mTimersQueue;
109 
110 private:
111  std::recursive_mutex mLock;
112  std::thread mThread;
113  int mTimerFd;
114  int mEventFd;
115 
116  AICommon::IDGenerator<6> mIdGenerator;
117 };
118 
119 
120 #endif // !defined(DOBBYTIMER_H)
Definition: DobbyTimer.h:98
Utility object that can be used to register a callback function to execute in the future.
Definition: DobbyTimer.h:58
void stop()
Stops the poll loop thread and cancels all timers.
Definition: DobbyTimer.cpp:86
struct timespec calcAbsTime(const struct timespec &now, const std::chrono::milliseconds &timeout) const
Calculates the a new time value based on the time now and the supplied millisecond offset.
Definition: DobbyTimer.cpp:119
void updateTimerFd() const
Writes the item on the head of the expiry queue into the timerfd for the next wake-up time.
Definition: DobbyTimer.cpp:272
int add(const std::chrono::milliseconds &timeout, bool oneShot, const std::function< bool()> &func)
Adds a new timer to the timer queue.
Definition: DobbyTimer.cpp:153
void timerThread()
The thread function that runs the timer poll loop.
Definition: DobbyTimer.cpp:306
bool remove(int timerId)
Removes the given timer from the timer queue.
Definition: DobbyTimer.cpp:215
Definition: DobbyTimer.h:81