process-cpp  3.0.0
A simple convenience library for handling processes in C++11.
cross_process_sync.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2013 Canonical Ltd.
3  *
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.
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 
20 
21 #include <system_error>
22 
23 #include <poll.h>
24 #include <unistd.h>
25 
26 namespace
27 {
28 const int read_fd = 0;
29 const int write_fd = 1;
30 }
31 
33 {
34  if (::pipe(fds) < 0)
35  throw std::system_error(errno, std::system_category());
36 }
37 
39 {
40  fds[0] = ::dup(rhs.fds[0]);
41  fds[1] = ::dup(rhs.fds[1]);
42 }
43 
45 {
46  ::close(fds[0]);
47  ::close(fds[1]);
48 }
49 
51 {
52  ::close(fds[0]);
53  ::close(fds[1]);
54  fds[0] = ::dup(rhs.fds[0]);
55  fds[1] = ::dup(rhs.fds[1]);
56 
57  counter = rhs.counter;
58 
59  return *this;
60 }
61 
62 void core::testing::CrossProcessSync::try_signal_ready_for(const std::chrono::milliseconds& duration)
63 {
64  static const short empty_revents = 0;
65  pollfd poll_fd[1] = { { fds[write_fd], POLLOUT, empty_revents } };
66  int rc = -1;
67 
68  if ((rc = ::poll(poll_fd, 1, duration.count())) < 0)
69  throw std::system_error(errno, std::system_category());
70  else if (rc == 0)
71  throw Error::Timeout{};
72 
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());
76 }
77 
78 std::uint32_t core::testing::CrossProcessSync::wait_for_signal_ready_for(const std::chrono::milliseconds& duration)
79 {
80  static const short empty_revents = 0;
81  pollfd poll_fd[1] = { { fds[read_fd], POLLIN, empty_revents } };
82  int rc = -1;
83 
84  if ((rc = ::poll(poll_fd, 1, duration.count())) < 0)
85  throw std::system_error(errno, std::system_category());
86  else if (rc == 0)
87  throw Error::Timeout{};
88 
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());
92 
93  if (value != 1)
94  throw std::system_error(errno, std::system_category());
95 
96  counter += value;
97 
98  return counter;
99 }
configure_file
configure_file(process-cpp.pc.in process-cpp.pc @ONLY) install(FILES $
Definition: data/CMakeLists.txt:17
core::testing::CrossProcessSync
A cross-process synchronization primitive that supports simple wait-condition-like scenarios.
Definition: cross_process_sync.h:37
if
cmake_build_type_lower if("${cmake_build_type_lower}" STREQUAL "debug") option(PROCESS_CPP_ENABLE_DOC_GENERATION_BY_DEFAULT "Generate package by default" OFF) else() option(PROCESS_CPP_ENABLE_DOC_GENERATION_BY_DEFAULT "Generate package by default" ON) endif() if(PROCESS_CPP_ENABLE_DOC_GENERATION) if($
Definition: doc/CMakeLists.txt:25
add_library
add_library(process-cpp SHARED core/posix/backtrace.h core/posix/backtrace.cpp core/posix/child_process.cpp core/posix/exec.cpp core/posix/fork.cpp core/posix/process.cpp core/posix/process_group.cpp core/posix/signal.cpp core/posix/signalable.cpp core/posix/standard_stream.cpp core/posix/wait.cpp core/posix/this_process.cpp core/posix/linux/proc/process/oom_adj.cpp core/posix/linux/proc/process/oom_score.cpp core/posix/linux/proc/process/oom_score_adj.cpp core/posix/linux/proc/process/stat.cpp core/testing/cross_process_sync.cpp core/testing/fork_and_run.cpp) target_link_libraries(process-cpp $
Definition: src/CMakeLists.txt:17
option
option(PROCESS_CPP_ENABLE_DOC_GENERATION "Generate package documentation with doxygen" ON) string(TOLOWER "$
Definition: doc/CMakeLists.txt:17
core::testing::CrossProcessSync::operator=
CrossProcessSync & operator=(const CrossProcessSync &rhs)
operator =, dup's the underlying fds.
Definition: cross_process_sync.cpp:50
core::posix::exec
CORE_POSIX_DLL_PUBLIC ChildProcess exec(const std::string &fn, const std::vector< std::string > &argv, const std::map< std::string, std::string > &env, const StandardStream &flags)
exec execve's the executable with the provided arguments and environment.
Definition: exec.cpp:33
set
set(IS_ALL ALL) endif() find_package(Doxygen) if(DOXYGEN_FOUND) configure_file($
Definition: doc/CMakeLists.txt:42
core::posix::fork
CORE_POSIX_DLL_PUBLIC ChildProcess fork(const std::function< posix::exit::Status()> &main, const StandardStream &flags)
fork forks a new process and executes the provided main function in the newly forked process.
Definition: fork.cpp:57
core
Definition: child_process.h:32
core::testing::CrossProcessSync::try_signal_ready_for
void try_signal_ready_for(const std::chrono::milliseconds &duration)
Try to signal the other side that we are ready for at most duration milliseconds.
Definition: cross_process_sync.cpp:62
core::testing::CrossProcessSync::~CrossProcessSync
~CrossProcessSync() noexcept
Closes the underlying fds.
Definition: cross_process_sync.cpp:44
cmake_minimum_required
cmake_minimum_required(VERSION 2.8) project(process-cpp) find_package(Boost COMPONENTS iostreams system REQUIRED) find_package(PkgConfig REQUIRED) find_package(Threads REQUIRED) pkg_check_modules(PROPERTIES_CPP properties-cpp) set(CMAKE_MODULE_PATH $
Definition: CMakeLists.txt:17
set
set(GMOCK_INCLUDE_DIR "/usr/include/gmock/include" CACHE PATH "gmock source include directory") set(GMOCK_SOURCE_DIR "/usr/src/gmock" CACHE PATH "gmock source directory") set(GTEST_INCLUDE_DIR "$
Definition: tests/CMakeLists.txt:18
core::testing::CrossProcessSync::Error::Timeout
Thrown if any of the *_for functions times out.
Definition: cross_process_sync.h:48
core::testing::CrossProcessSync::CrossProcessSync
CrossProcessSync()
Constructs a new sync element.
Definition: cross_process_sync.cpp:32
cross_process_sync.h
core::testing::CrossProcessSync::wait_for_signal_ready_for
std::uint32_t wait_for_signal_ready_for(const std::chrono::milliseconds &duration)
Wait for the other sides to signal readiness for at most duration milliseconds.
Definition: cross_process_sync.cpp:78
core::testing::fork_and_run
CORE_POSIX_DLL_PUBLIC ForkAndRunResult fork_and_run(const std::function< core::posix::exit::Status()> &service, const std::function< core::posix::exit::Status()> &client)
Forks two processes for both the service and the client.
Definition: fork_and_run.cpp:41