2 * Copyright (c) 2015 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
26 #include <dpl/test/test_runner.h>
28 #include "synchronization_pipe.h"
30 static void closeFd(int *fd) {
37 SynchronizationPipe::SynchronizationPipe() {
38 auto ret = pipe(m_pipeCP);
39 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed");
42 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed");
45 SynchronizationPipe::~SynchronizationPipe() {
46 closeFd(m_pipeCP + 0);
47 closeFd(m_pipeCP + 1);
48 closeFd(m_pipePC + 0);
49 closeFd(m_pipePC + 1);
52 void SynchronizationPipe::claimParentEp() {
56 m_readEp = m_pipeCP[0];
57 closeFd(m_pipeCP + 1);
59 m_writeEp = m_pipePC[1];
60 closeFd(m_pipePC + 0);
65 void SynchronizationPipe::claimChildEp() {
69 m_readEp = m_pipePC[0];
70 closeFd(m_pipePC + 1);
72 m_writeEp = m_pipeCP[1];
73 closeFd(m_pipeCP + 0);
78 void SynchronizationPipe::post() {
79 RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed");
80 auto ret = TEMP_FAILURE_RETRY(write(m_writeEp, "#", 1));
81 RUNNER_ASSERT_ERRNO_MSG(ret > 0, "Write failed ret = " << ret);
84 void SynchronizationPipe::wait() {
85 RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed");
88 auto ret = TEMP_FAILURE_RETRY(read(m_readEp, &buf, 1));
89 RUNNER_ASSERT_ERRNO_MSG(ret > 0, "Read failed ret = " << ret);