TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / external / process-cpp-minimal / include / core / testing / cross_process_sync.h
1 /*
2  * Copyright © 2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voss <thomas.voss@canonical.com>
17  */
18
19 #ifndef CORE_TESTING_CROSS_PROCESS_SYNC_H_
20 #define CORE_TESTING_CROSS_PROCESS_SYNC_H_
21
22 #include <core/posix/visibility.h>
23
24 #include <cstdint>
25
26 #include <chrono>
27 #include <stdexcept>
28
29 namespace core
30 {
31 namespace testing
32 {
33 /**
34  * @brief A cross-process synchronization primitive that supports simple wait-condition-like scenarios.
35  */
36 class CORE_POSIX_DLL_PUBLIC CrossProcessSync
37 {
38   public:
39     struct Error
40     {
41         Error() = delete;
42         ~Error() = delete;
43
44         /**
45          * @brief Thrown if any of the *_for functions times out.
46          */
47         struct Timeout : public std::runtime_error
48         {
49             Timeout() : std::runtime_error("Timeout while waiting for event to happen.")
50             {
51             }
52         };
53     };
54
55     /**
56      * @brief Constructs a new sync element.
57      */
58     CrossProcessSync();
59
60     /**
61      * @brief Copy c'tor, duping the underlying fds.
62      * @param rhs The instance to copy.
63      */
64     CrossProcessSync(const CrossProcessSync& rhs);
65
66     /**
67       * @brief Closes the underlying fds.
68       */
69     ~CrossProcessSync() noexcept;
70
71     /**
72      * @brief operator =, dup's the underlying fds.
73      * @param rhs The instance to assign from.
74      * @return A mutable reference to this instance.
75      */
76     CrossProcessSync& operator=(const CrossProcessSync& rhs);
77
78     /**
79      * @brief Try to signal the other side that we are ready for at most duration milliseconds.
80      * @throw Error::Timeout in case of a timeout.
81      * @throw std::system_error for problems with the underlying pipe.
82      */
83     void try_signal_ready_for(const std::chrono::milliseconds& duration);
84
85     /**
86      * @brief Wait for the other sides to signal readiness for at most duration milliseconds.
87      * @return The number of ready signals that have been collected since creation.
88      * @throw Error::Timeout in case of a timeout.
89      * @throw std::system_error for problems with the underlying pipe.
90      */
91     std::uint32_t wait_for_signal_ready_for(const std::chrono::milliseconds& duration);
92
93   private:
94     int fds[2]; ///< The cross-process pipe.
95     std::uint32_t counter; ///< Counts the number of times the sync has been signalled.
96 };
97 }
98 }
99 #endif // CORE_TESTING_CROSS_PROCESS_SYNC_H_