2 * Copyright © 2013 Canonical Ltd.
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.
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.
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/>.
16 * Authored by: Thomas Voss <thomas.voss@canonical.com>
19 #ifndef CORE_TESTING_CROSS_PROCESS_SYNC_H_
20 #define CORE_TESTING_CROSS_PROCESS_SYNC_H_
22 #include <core/posix/visibility.h>
34 * @brief A cross-process synchronization primitive that supports simple wait-condition-like scenarios.
36 class CORE_POSIX_DLL_PUBLIC CrossProcessSync
45 * @brief Thrown if any of the *_for functions times out.
47 struct Timeout : public std::runtime_error
49 Timeout() : std::runtime_error("Timeout while waiting for event to happen.")
56 * @brief Constructs a new sync element.
61 * @brief Copy c'tor, duping the underlying fds.
62 * @param rhs The instance to copy.
64 CrossProcessSync(const CrossProcessSync& rhs);
67 * @brief Closes the underlying fds.
69 ~CrossProcessSync() noexcept;
72 * @brief operator =, dup's the underlying fds.
73 * @param rhs The instance to assign from.
74 * @return A mutable reference to this instance.
76 CrossProcessSync& operator=(const CrossProcessSync& rhs);
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.
83 void try_signal_ready_for(const std::chrono::milliseconds& duration);
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.
91 std::uint32_t wait_for_signal_ready_for(const std::chrono::milliseconds& duration);
94 int fds[2]; ///< The cross-process pipe.
95 std::uint32_t counter; ///< Counts the number of times the sync has been signalled.
99 #endif // CORE_TESTING_CROSS_PROCESS_SYNC_H_