Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iox-#751: remove old semaphore posix wrapper #1402

Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
9a4fb4c
iox-#751 expect uses 'write' instead of 'std::cout' so that it can be…
elfenpiff Jun 16, 2022
6589841
iox-#751 Remove old semaphore and use the new unnamed semaphore in th…
elfenpiff Jun 16, 2022
208586b
iox-#751 Remove manual signal handling from roudi app and use signal_…
elfenpiff Jun 16, 2022
803f501
iox-#751 Add wait_loop to adaptive_wait and use it in the tests for a…
elfenpiff Jun 16, 2022
42d1bde
iox-#751 Replace ShutdownManager with SignalWatcher in dds gateway, r…
elfenpiff Jun 16, 2022
1164205
iox-#751 Add code examples and feature note to release notes
elfenpiff Jun 16, 2022
3b371aa
iox-#751 Remove unused SemaphoreError enum values
elfenpiff Jun 16, 2022
af640e1
iox-#751 the platform IOX_SEM_VALUE_MAX is a constexpr to ensure type…
elfenpiff Jun 16, 2022
1a15a54
iox-#751 Release note simplified, m_ added for consistency reasons, f…
elfenpiff Jun 20, 2022
394f231
iox-#751 .expect prints error message with LogFatal, SignalWatcher ma…
elfenpiff Jun 21, 2022
4866c4b
iox-#751 Fix rpc c example by using iox_ws_mark_for_destruction
elfenpiff Jun 21, 2022
7a32fdd
iox-#751 Fix rpc c++ example by signalling waitset SIGTERM
elfenpiff Jun 23, 2022
3513d5f
iox-#751 Renaming, update copyright year, improve code readability
elfenpiff Jun 23, 2022
c6dce93
iox-#751 replace adaptive_wait with Barrier
elfenpiff Jun 23, 2022
795f0bd
iox-#751 Update readmes and copyright year
elfenpiff Jun 23, 2022
b303960
iox-#751 Do not include unistd.h since it is not available in windows
elfenpiff Jun 23, 2022
34fd386
iox-#751 Replace adaptive_wait with Barrier
elfenpiff Jun 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions doc/website/release-notes/iceoryx-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Refactor semaphore [\#751](https://github.com/eclipse-iceoryx/iceoryx/issues/751)
- Introduce `UnnamedSemaphore`
- Introduce `NamedSemaphore`
- Remove old `Semaphore`
- Extend `concatenate`, `operator+`, `unsafe_append` and `append` of `iox::cxx::string` for chars [\#208](https://github.com/eclipse-iceoryx/iceoryx/issues/208)
- Extend `unsafe_append` and `append` methods of `iox::cxx::string` for `std::string` [\#208](https://github.com/eclipse-iceoryx/iceoryx/issues/208)
- The iceoryx development environment supports multiple running docker containers [\#1410](https://github.com/eclipse-iceoryx/iceoryx/issues/1410)
Expand Down Expand Up @@ -79,3 +80,62 @@
// after
#include "iceoryx_hoofs/design_pattern/builder.hpp"
```

3. `UnnamedSemaphore` replaces `Semaphore` with `CreateUnnamed*` option

```cpp
// before
#include "iceoryx_hoofs/posix_wrapper/semaphore.hpp"

auto semaphore = iox::posix::Semaphore::create(iox::posix::CreateUnnamedSingleProcessSemaphore, 0);

// after
#include "iceoryx_hoofs/posix_wrapper/unnamed_semaphore.hpp"

iox::cxx::optional<iox::posix::UnnamedSemaphore> semaphore;
auto result = iox::posix::UnnamedSemaphoreBuilder()
.initialValue(0U)
.isInterProcessCapable(true)
.create(semaphore);
```

4. `NamedSemaphore` replaces `Semaphore` with `CreateNamedSemaphore` option

```cpp
// before
#include "iceoryx_hoofs/posix_wrapper/semaphore.hpp"

auto semaphore = iox::posix::Semaphore::create(iox::posix::CreateNamedSemaphore,
"mySemaphoreName",
S_IRUSR | S_IWUSR,
0);
// after
#include "iceoryx_hoofs/posix_wrapper/named_semaphore.hpp"

iox::cxx::optional<iox::posix::NamedSemaphore> semaphore;
auto result = iox::posix::NamedSemaphoreBuilder()
.name("mySemaphoreName")
.openMode(iox::posix::OpenMode::OPEN_OR_CREATE)
.permissions(iox::cxx::perms::owner_all)
.initialValue(0U)
.create(semaphore);
```

5. `RoudiApp::waitForSignal` is deprecated
```cpp
// before
//// in my custom roudi app implementation
uint8_t MyCustomRoudiApp::run() noexcept {
// ...

waitForSignal();
}

// after
//// in my custom roudi app implementation
uint8_t MyCustomRoudiApp::run() noexcept {
// ...

iox::posix::waitForTerminationRequest();
}
```
4 changes: 2 additions & 2 deletions iceoryx_binding_c/test/moduletests/test_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ void notifyClient(ClientPortData& portData)
portData.m_connectionState = iox::ConnectionState::CONNECTED;
iox::popo::ChunkQueuePusher<ClientChunkQueueData_t> pusher{&portData.m_chunkReceiverData};
pusher.push(iox::mepoo::SharedChunk());
EXPECT_FALSE(portData.m_chunkReceiverData.m_conditionVariableDataPtr->m_semaphore.post().has_error());
EXPECT_FALSE(portData.m_chunkReceiverData.m_conditionVariableDataPtr->m_semaphore->post().has_error());
}

TIMING_TEST_F(iox_listener_test, NotifyingClientEventWorks, Repeat(5), [&] {
Expand Down Expand Up @@ -491,7 +491,7 @@ void notifyServer(ServerPortData& portData)
{
iox::popo::ChunkQueuePusher<ServerChunkQueueData_t> pusher{&portData.m_chunkReceiverData};
pusher.push(iox::mepoo::SharedChunk());
EXPECT_FALSE(portData.m_chunkReceiverData.m_conditionVariableDataPtr->m_semaphore.post().has_error());
EXPECT_FALSE(portData.m_chunkReceiverData.m_conditionVariableDataPtr->m_semaphore->post().has_error());
}

TEST_F(iox_listener_test, AttachingServerWorks)
Expand Down
6 changes: 3 additions & 3 deletions iceoryx_binding_c/test/moduletests/test_wait_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ void notifyClient(ClientPortData& portData)
portData.m_connectionState = iox::ConnectionState::CONNECTED;
iox::popo::ChunkQueuePusher<ClientChunkQueueData_t> pusher{&portData.m_chunkReceiverData};
pusher.push(iox::mepoo::SharedChunk());
EXPECT_FALSE(portData.m_chunkReceiverData.m_conditionVariableDataPtr->m_semaphore.post().has_error());
EXPECT_FALSE(portData.m_chunkReceiverData.m_conditionVariableDataPtr->m_semaphore->post().has_error());
}

TEST_F(iox_ws_test, NotifyingClientEventWorks)
Expand Down Expand Up @@ -928,7 +928,7 @@ void notifyServer(ServerPortData& portData)
{
iox::popo::ChunkQueuePusher<ServerChunkQueueData_t> pusher{&portData.m_chunkReceiverData};
pusher.push(iox::mepoo::SharedChunk());
EXPECT_FALSE(portData.m_chunkReceiverData.m_conditionVariableDataPtr->m_semaphore.post().has_error());
EXPECT_FALSE(portData.m_chunkReceiverData.m_conditionVariableDataPtr->m_semaphore->post().has_error());
}

TEST_F(iox_ws_test, AttachingServerEventWorks)
Expand Down Expand Up @@ -1140,7 +1140,7 @@ void notifyServiceDiscovery(SubscriberPortData& portData)
{
iox::popo::ChunkQueuePusher<SubscriberChunkReceiverData_t> pusher{&portData.m_chunkReceiverData};
pusher.push(iox::mepoo::SharedChunk());
EXPECT_FALSE(portData.m_chunkReceiverData.m_conditionVariableDataPtr->m_semaphore.post().has_error());
EXPECT_FALSE(portData.m_chunkReceiverData.m_conditionVariableDataPtr->m_semaphore->post().has_error());
}

TEST_F(iox_ws_test, NotifyingServiceDiscoveryEventWorks)
Expand Down
39 changes: 3 additions & 36 deletions iceoryx_dds/source/gateway/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,46 +21,13 @@
#include "iceoryx_hoofs/cxx/helplets.hpp"
#include "iceoryx_hoofs/cxx/optional.hpp"
#include "iceoryx_hoofs/platform/signal.hpp"
#include "iceoryx_hoofs/posix_wrapper/semaphore.hpp"
#include "iceoryx_hoofs/posix_wrapper/signal_handler.hpp"
#include "iceoryx_hoofs/posix_wrapper/signal_watcher.hpp"
elfenpiff marked this conversation as resolved.
Show resolved Hide resolved
#include "iceoryx_posh/gateway/gateway_config.hpp"
#include "iceoryx_posh/gateway/toml_gateway_config_parser.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"

class ShutdownManager
{
public:
static void scheduleShutdown(int num)
{
char reason = '\0';
psignal(num, &reason);
s_semaphore.post().or_else([](auto) {
std::cerr << "failed to call post on shutdown semaphore" << std::endl;
std::terminate();
});
}
static void waitUntilShutdown()
{
s_semaphore.wait().or_else([](auto) {
std::cerr << "failed to call wait on shutdown semaphore" << std::endl;
std::terminate();
});
}

private:
static iox::posix::Semaphore s_semaphore;
ShutdownManager() = default;
};
iox::posix::Semaphore ShutdownManager::s_semaphore =
iox::posix::Semaphore::create(iox::posix::CreateUnnamedSingleProcessSemaphore, 0u).value();

int main()
{
// Set OS signal handlers
auto signalGuardInt = iox::posix::registerSignalHandler(iox::posix::Signal::INT, ShutdownManager::scheduleShutdown);
auto signalGuardTerm =
iox::posix::registerSignalHandler(iox::posix::Signal::TERM, ShutdownManager::scheduleShutdown);

// Start application
iox::runtime::PoshRuntime::initRuntime("iox-dds-gateway");

Expand All @@ -84,7 +51,7 @@ int main()
dds2ioxGateway.runMultithreaded();

// Run until SIGINT or SIGTERM
ShutdownManager::waitUntilShutdown();
iox::posix::waitForTerminationRequest();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/cxx/optional.hpp"
#include "iceoryx_hoofs/posix_wrapper/semaphore.hpp"
#include "iceoryx_hoofs/posix_wrapper/signal_watcher.hpp"
#include "iceoryx_posh/popo/listener.hpp"
#include "iceoryx_posh/popo/subscriber.hpp"
Expand Down
1 change: 0 additions & 1 deletion iceoryx_examples/callbacks/ice_callbacks_subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/cxx/optional.hpp"
#include "iceoryx_hoofs/posix_wrapper/semaphore.hpp"
#include "iceoryx_hoofs/posix_wrapper/signal_watcher.hpp"
#include "iceoryx_posh/popo/listener.hpp"
#include "iceoryx_posh/popo/subscriber.hpp"
Expand Down
7 changes: 4 additions & 3 deletions iceoryx_examples/request_response/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ At first, the includes for the client port, request-response types, WaitSet, and
```cpp
#include "request_and_response_types.hpp"

#include "iceoryx_hoofs/posix_wrapper/signal_handler.hpp"
#include "iceoryx_hoofs/posix_wrapper/signal_watcher.hpp"
#include "iceoryx_posh/popo/client.hpp"
#include "iceoryx_posh/popo/wait_set.hpp"
Expand Down Expand Up @@ -70,14 +71,14 @@ full or the server is too slow. The `ClientOptions` are similar to `PublisherOpt

<!--[geoffrey][iceoryx_examples/request_response/client_cxx_waitset.cpp][create waitset]-->
```cpp
iox::popo::WaitSet<> waitset;
waitset.emplace();

iox::popo::ClientOptions options;
options.responseQueueCapacity = 2U;
iox::popo::Client<AddRequest, AddResponse> client({"Example", "Request-Response", "Add"}, options);

// attach client to waitset
waitset.attachState(client, iox::popo::ClientState::HAS_RESPONSE).or_else([](auto) {
waitset->attachState(client, iox::popo::ClientState::HAS_RESPONSE).or_else([](auto) {
std::cerr << "failed to attach client" << std::endl;
std::exit(EXIT_FAILURE);
});
Expand Down Expand Up @@ -131,7 +132,7 @@ Once the request has been sent, we block and wait for samples to arrive. Then we
iterate over the notification vector to check if we were triggered from our client:
<!-- [geoffrey] [iceoryx_examples/request_response/client_cxx_waitset.cpp] [[wait and check if the client triggered]] -->
```cpp
auto notificationVector = waitset.timedWait(iox::units::Duration::fromSeconds(5));
auto notificationVector = waitset->timedWait(iox::units::Duration::fromSeconds(5));

for (auto& notification : notificationVector)
{
Expand Down
23 changes: 18 additions & 5 deletions iceoryx_examples/request_response/client_cxx_waitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
//! [iceoryx includes]
#include "request_and_response_types.hpp"

#include "iceoryx_hoofs/posix_wrapper/signal_handler.hpp"
#include "iceoryx_hoofs/posix_wrapper/signal_watcher.hpp"
#include "iceoryx_posh/popo/client.hpp"
#include "iceoryx_posh/popo/wait_set.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
//! [iceoryx includes]

#include <atomic>
#include <iostream>

constexpr char APP_NAME[] = "iox-cpp-request-response-client-waitset";

std::atomic_bool keepRunning = {true};
iox::cxx::optional<iox::popo::WaitSet<>> waitset;
elfenpiff marked this conversation as resolved.
Show resolved Hide resolved

//! [context data to store Fibonacci numbers and sequence ids]
struct ContextData
Expand All @@ -38,16 +41,25 @@ struct ContextData
};
//! [context data to store Fibonacci numbers and sequence ids]

void signalHandler(int)
{
keepRunning = false;
waitset.and_then([&](auto& w) { w.markForDestruction(); });
}

int main()
{
auto sigTermGuard = iox::posix::registerSignalHandler(iox::posix::Signal::TERM, signalHandler);
auto sigIntGuard = iox::posix::registerSignalHandler(iox::posix::Signal::INT, signalHandler);

//! [initialize runtime]
iox::runtime::PoshRuntime::initRuntime(APP_NAME);
//! [initialize runtime]

ContextData ctx;

//! [create waitset]
iox::popo::WaitSet<> waitset;
waitset.emplace();

//! [create client]
iox::popo::ClientOptions options;
Expand All @@ -56,14 +68,14 @@ int main()
//! [create client]

// attach client to waitset
waitset.attachState(client, iox::popo::ClientState::HAS_RESPONSE).or_else([](auto) {
waitset->attachState(client, iox::popo::ClientState::HAS_RESPONSE).or_else([](auto) {
std::cerr << "failed to attach client" << std::endl;
std::exit(EXIT_FAILURE);
});
//! [create waitset]

//! [mainloop]
while (!iox::posix::hasTerminationRequested())
while (keepRunning)
{
//! [send request]
client.loan()
Expand All @@ -84,7 +96,7 @@ int main()

// We block and wait for samples to arrive, when the time is up we send the request again
//! [wait and check if the client triggered]
auto notificationVector = waitset.timedWait(iox::units::Duration::fromSeconds(5));
auto notificationVector = waitset->timedWait(iox::units::Duration::fromSeconds(5));

for (auto& notification : notificationVector)
{
Expand Down Expand Up @@ -117,6 +129,7 @@ int main()
}
//! [mainloop]

waitset.reset();
std::cout << "shutting down" << std::endl;

return (EXIT_SUCCESS);
Expand Down
3 changes: 1 addition & 2 deletions iceoryx_examples/request_response_in_c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ Afterwards we create our waitset and attach the client state `ClientState_HAS_RE
to it.
<!--[geoffrey][iceoryx_examples/request_response_in_c/client_c_waitset.c][create waitset and attach client]-->
```c
iox_ws_storage_t waitsetStorage;
iox_ws_t waitset = iox_ws_init(&waitsetStorage);
waitset = iox_ws_init(&waitsetStorage);
if (iox_ws_attach_client_state(waitset, client, ClientState_HAS_RESPONSE, 0U, NULL) != WaitSetResult_SUCCESS)
{
Expand Down
9 changes: 7 additions & 2 deletions iceoryx_examples/request_response_in_c/client_c_waitset.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "iceoryx_binding_c/request_header.h"
#include "iceoryx_binding_c/response_header.h"
#include "iceoryx_binding_c/runtime.h"
#include "iceoryx_binding_c/user_trigger.h"
#include "iceoryx_binding_c/wait_set.h"
#include "request_and_response_c_types.h"
#include "sleep_for.h"
Expand All @@ -26,16 +27,21 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>

#define NUMBER_OF_NOTIFICATIONS 1

bool keepRunning = true;
const char APP_NAME[] = "iox-c-request-response-client-waitset";

iox_ws_t waitset;
iox_ws_storage_t waitsetStorage;

void sigHandler(int signalValue)
{
(void)signalValue;
keepRunning = false;
iox_ws_mark_for_destruction(waitset);
}

int main()
Expand All @@ -54,8 +60,7 @@ int main()
int64_t expectedResponseSequenceId = requestSequenceId;

//! [create waitset and attach client]
iox_ws_storage_t waitsetStorage;
iox_ws_t waitset = iox_ws_init(&waitsetStorage);
waitset = iox_ws_init(&waitsetStorage);
elfenpiff marked this conversation as resolved.
Show resolved Hide resolved

if (iox_ws_attach_client_state(waitset, client, ClientState_HAS_RESPONSE, 0U, NULL) != WaitSetResult_SUCCESS)
{
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ iox_add_library(
source/cxx/adaptive_wait.cpp
source/cxx/deadline_timer.cpp
source/cxx/filesystem.cpp
source/cxx/functional_interface.cpp
source/cxx/helplets.cpp
source/cxx/requires.cpp
source/cxx/unique_id.cpp
Expand All @@ -71,7 +72,6 @@ iox_add_library(
source/posix_wrapper/named_pipe.cpp
source/posix_wrapper/named_semaphore.cpp
source/posix_wrapper/posix_access_rights.cpp
source/posix_wrapper/semaphore.cpp
source/posix_wrapper/semaphore_interface.cpp
source/posix_wrapper/shared_memory_object.cpp
source/posix_wrapper/shared_memory_object/allocator.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
#ifndef IOX_HOOFS_CXX_FUNCTIONAL_POLICY_HPP
#define IOX_HOOFS_CXX_FUNCTIONAL_POLICY_HPP

#include "iceoryx_hoofs/cxx/attributes.hpp"
#include "iceoryx_hoofs/cxx/function_ref.hpp"
#include "iceoryx_hoofs/cxx/type_traits.hpp"
#include "iceoryx_hoofs/platform/unistd.hpp"

#include <iostream>
#include <utility>

namespace iox
Expand Down Expand Up @@ -54,6 +55,8 @@ struct HasGetErrorMethod<Derived, cxx::void_t<decltype(std::declval<Derived>().g
{
};

void print_expect_message(const char* message) noexcept;

template <typename Derived>
struct Expect
{
Expand Down
Loading