2 * Copyright (c) 2015-2020 Samsung Electronics Co., Ltd. All rights reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file synchronization_pipe.cpp
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief A crippled abstraction of widely praised, but often misused communication mechanism
27 #include <dpl/test/test_runner.h>
29 #include "synchronization_pipe.h"
31 static void closeFd(int *fd) {
38 SynchronizationPipe::SynchronizationPipe() {
39 auto ret = pipe(m_pipeCP);
40 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed");
43 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed");
46 SynchronizationPipe::~SynchronizationPipe() {
47 closeFd(m_pipeCP + 0);
48 closeFd(m_pipeCP + 1);
49 closeFd(m_pipePC + 0);
50 closeFd(m_pipePC + 1);
53 void SynchronizationPipe::claimParentEp() {
57 m_readEp = m_pipeCP[0];
58 closeFd(m_pipeCP + 1);
60 m_writeEp = m_pipePC[1];
61 closeFd(m_pipePC + 0);
66 void SynchronizationPipe::claimChildEp() {
70 m_readEp = m_pipePC[0];
71 closeFd(m_pipePC + 1);
73 m_writeEp = m_pipeCP[1];
74 closeFd(m_pipeCP + 0);
79 void SynchronizationPipe::post() {
83 void SynchronizationPipe::wait() {
88 void SynchronizationPipe::pollForWait() {
89 RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed");
94 auto ret = TEMP_FAILURE_RETRY(poll(fds, 1, -1));
95 RUNNER_ASSERT_ERRNO(ret > 0);
98 void SynchronizationPipe::post(const void *data, size_t size) {
99 RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed");
100 auto ret = TEMP_FAILURE_RETRY(write(m_writeEp, data, size));
101 RUNNER_ASSERT_ERRNO_MSG(ret > 0 && size_t(ret) == size, "Write failed size = " << size << " ret = " << ret);
104 void SynchronizationPipe::wait(void *data, size_t size) {
105 RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed");
107 auto ret = TEMP_FAILURE_RETRY(read(m_readEp, data, size));
108 RUNNER_ASSERT_ERRNO_MSG(ret > 0 && size_t(ret) == size, "Read failed size = " << size << " ret = " << ret);