2 * Copyright © 2013 Canonical Ltd.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * 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 #include <core/testing/cross_process_sync.h>
21 #include <system_error>
28 const int read_fd = 0;
29 const int write_fd = 1;
32 core::testing::CrossProcessSync::CrossProcessSync() : counter(0)
35 throw std::system_error(errno, std::system_category());
38 core::testing::CrossProcessSync::CrossProcessSync(const CrossProcessSync& rhs) : counter(rhs.counter)
40 fds[0] = ::dup(rhs.fds[0]);
41 fds[1] = ::dup(rhs.fds[1]);
44 core::testing::CrossProcessSync::~CrossProcessSync() noexcept
50 core::testing::CrossProcessSync& core::testing::CrossProcessSync::operator=(const core::testing::CrossProcessSync& rhs)
54 fds[0] = ::dup(rhs.fds[0]);
55 fds[1] = ::dup(rhs.fds[1]);
57 counter = rhs.counter;
62 void core::testing::CrossProcessSync::try_signal_ready_for(const std::chrono::milliseconds& duration)
64 static const short empty_revents = 0;
65 pollfd poll_fd[1] = { { fds[write_fd], POLLOUT, empty_revents } };
68 if ((rc = ::poll(poll_fd, 1, duration.count())) < 0)
69 throw std::system_error(errno, std::system_category());
71 throw Error::Timeout{};
73 static const std::uint32_t value = 1;
74 if (sizeof(value) != write(fds[write_fd], std::addressof(value), sizeof(value)))
75 throw std::system_error(errno, std::system_category());
78 std::uint32_t core::testing::CrossProcessSync::wait_for_signal_ready_for(const std::chrono::milliseconds& duration)
80 static const short empty_revents = 0;
81 pollfd poll_fd[1] = { { fds[read_fd], POLLIN, empty_revents } };
84 if ((rc = ::poll(poll_fd, 1, duration.count())) < 0)
85 throw std::system_error(errno, std::system_category());
87 throw Error::Timeout{};
89 std::uint32_t value = 0;
90 if (sizeof(value) != read(fds[read_fd], std::addressof(value), sizeof(value)))
91 throw std::system_error(errno, std::system_category());
94 throw std::system_error(errno, std::system_category());