OpenVPN 3 Core Library
Loading...
Searching...
No Matches
asyncsleep.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// Interruptible sleep
13
14#ifndef OPENVPN_COMMON_ASYNCSLEEP_H
15#define OPENVPN_COMMON_ASYNCSLEEP_H
16
17#include <algorithm>
18#include <chrono>
19#include <thread>
20
22
23namespace openvpn {
24
25// returns false if Stop signal prevented full wait
26inline bool async_sleep_milliseconds(int milliseconds, Stop *async_stop)
27{
28 const int milliseconds_per_retry = 250;
29 volatile bool stop = false;
30
31 // allow asynchronous stop
32 Stop::Scope stop_scope(async_stop, [&stop]()
33 { stop = true; });
34
35 while (milliseconds > 0 && !stop)
36 {
37 const int ms = std::min(milliseconds, milliseconds_per_retry);
38 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
39 milliseconds -= ms;
40 }
41
42 return !stop;
43}
44
45} // namespace openvpn
46
47#endif
bool async_sleep_milliseconds(int milliseconds, Stop *async_stop)