1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2019-2019 Bareos GmbH & Co. KG
5 
6    This program is Free Software; you can redistribute it and/or
7    modify it under the terms of version three of the GNU Affero General Public
8    License as published by the Free Software Foundation and included
9    in the file LICENSE.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14    Affero General Public License for more details.
15 
16    You should have received a copy of the GNU Affero General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA.
20 */
21 
22 #ifndef BAREOS_SRC_DIRD_SCHEDULER_SYSTEM_TIME_SOURCE_H_
23 #define BAREOS_SRC_DIRD_SCHEDULER_SYSTEM_TIME_SOURCE_H_
24 
25 #include "dird/scheduler_time_adapter.h"
26 
27 #include <chrono>
28 #include <atomic>
29 #include <thread>
30 
31 namespace directordaemon {
32 
33 class SystemTimeSource : public TimeSource {
34  public:
SystemTime()35   time_t SystemTime() override { return time(nullptr); }
36 
SleepFor(std::chrono::seconds wait_interval)37   void SleepFor(std::chrono::seconds wait_interval) override
38   {
39     std::chrono::milliseconds wait_increment = std::chrono::milliseconds(100);
40     std::size_t loop_count = wait_interval / wait_increment;
41 
42     // avoid loop counter wrap due to roundoff
43     if (loop_count == 0) { loop_count = 1; }
44 
45     while (running_ && loop_count--) {
46       // cannot use condition variable because notify_one
47       // does not securely work in a signal handler
48       std::this_thread::sleep_for(wait_increment);
49     }
50   }
51 
Terminate()52   void Terminate() override { running_ = false; }
53 
54  private:
55   std::atomic<bool> running_{true};
56 };
57 
58 }  // namespace directordaemon
59 
60 #endif  // BAREOS_SRC_DIRD_SCHEDULER_SYSTEM_TIME_SOURCE_H_
61