Skip to content

Commit

Permalink
Merge pull request #1402 from ApexAI/iox-#751-remove-old-semaphore-po…
Browse files Browse the repository at this point in the history
…six-wrapper

iox-#751: remove old semaphore posix wrapper
  • Loading branch information
elfenpiff authored Jun 24, 2022
2 parents 10f2ab0 + 34fd386 commit e92836b
Show file tree
Hide file tree
Showing 54 changed files with 400 additions and 1,340 deletions.
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"
#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;

//! [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
12 changes: 10 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 @@ -27,15 +28,23 @@
#include <stddef.h>
#include <stdio.h>

#if !defined(_WIN32)
#include <unistd.h>
#endif

#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 +63,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);

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
Loading

0 comments on commit e92836b

Please sign in to comment.