Skip to content

Commit

Permalink
iox-eclipse-iceoryx#751 Add first unnamed semaphore interface and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Eltzschig <me@elchris.org>
  • Loading branch information
elfenpiff committed May 19, 2022
1 parent 2adc07d commit 27c857e
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 0 deletions.
1 change: 1 addition & 0 deletions iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ add_library(iceoryx_hoofs
source/posix_wrapper/system_configuration.cpp
source/posix_wrapper/thread.cpp
source/posix_wrapper/timer.cpp
source/posix_wrapper/unnamed_semaphore.cpp
source/posix_wrapper/unix_domain_socket.cpp
source/relocatable_pointer/base_relative_pointer.cpp
source/relocatable_pointer/relative_pointer_data.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_POSIX_WRAPPER_UNNAMED_SEMAPHORE_HPP
#define IOX_HOOFS_POSIX_WRAPPER_UNNAMED_SEMAPHORE_HPP

#include "iceoryx_hoofs/cxx/expected.hpp"
#include "iceoryx_hoofs/cxx/optional.hpp"
#include "iceoryx_hoofs/design_pattern/builder.hpp"
#include "iceoryx_hoofs/posix_wrapper/semaphore.hpp"

namespace iox
{
namespace posix
{
template <typename SemaphoreChild>
class SemaphoreInterface
{
public:
SemaphoreInterface(const SemaphoreInterface&) noexcept = delete;
SemaphoreInterface(SemaphoreInterface&&) noexcept = delete;
SemaphoreInterface& operator=(const SemaphoreInterface&) noexcept = delete;
SemaphoreInterface& operator=(SemaphoreInterface&&) noexcept = delete;
~SemaphoreInterface() noexcept = default;

void post() noexcept;
cxx::expected<SemaphoreError> postUnsafe() noexcept;

uint32_t getValue() const noexcept;
cxx::expected<uint32_t, SemaphoreError> getValueUnsafe() const noexcept;

void timedWait() noexcept;
cxx::expected<SemaphoreError> timedWaitUnsafe() noexcept;

void tryWait() noexcept;
cxx::expected<SemaphoreError> tryWaitUnsafe() noexcept;

protected:
SemaphoreInterface() noexcept = default;
};

class UnnamedSemaphore : SemaphoreInterface<UnnamedSemaphore>
{
public:
UnnamedSemaphore(const UnnamedSemaphore&) noexcept = delete;
UnnamedSemaphore(UnnamedSemaphore&&) noexcept = delete;
UnnamedSemaphore& operator=(const UnnamedSemaphore&) noexcept = delete;
UnnamedSemaphore& operator=(UnnamedSemaphore&&) noexcept = delete;
~UnnamedSemaphore() noexcept;

friend class UnnamedSemaphoreBuilder;
friend class iox::cxx::optional<UnnamedSemaphore>;
friend class SemaphoreInterface<UnnamedSemaphore>;

private:
UnnamedSemaphore() noexcept = default;
iox_sem_t* getHandle() noexcept;
const iox_sem_t* getHandle() const noexcept;

iox_sem_t m_handle;
bool m_destroyHandle = true;
};

class UnnamedSemaphoreBuilder
{
IOX_BUILDER_PARAMETER(uint32_t, initialValue, 0U)
IOX_BUILDER_PARAMETER(bool, isInterProcessCapable, true)

public:
cxx::expected<SemaphoreError> create(cxx::optional<UnnamedSemaphore>& uninitializedSemaphore) noexcept;
};
} // namespace posix
} // namespace iox

#endif
92 changes: 92 additions & 0 deletions iceoryx_hoofs/source/posix_wrapper/unnamed_semaphore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/posix_wrapper/unnamed_semaphore.hpp"
#include "iceoryx_hoofs/internal/log/hoofs_logging.hpp"
#include "iceoryx_hoofs/posix_wrapper/posix_call.hpp"

#include <climits>

namespace iox
{
namespace posix
{
cxx::expected<SemaphoreError>
UnnamedSemaphoreBuilder::create(cxx::optional<UnnamedSemaphore>& uninitializedSemaphore) noexcept
{
uninitializedSemaphore.emplace();

auto result = posixCall(iox_sem_init)(&uninitializedSemaphore.value().m_handle,
(m_isInterProcessCapable) ? 0 : 1,
static_cast<unsigned int>(m_initialValue))
.failureReturnValue(-1)
.evaluate();

if (result.has_error())
{
uninitializedSemaphore.value().m_destroyHandle = false;
uninitializedSemaphore.reset();

switch (result.get_error().errnum)
{
case EINVAL:
LogError() << "The initial value of " << m_initialValue << " exceeds " << SEM_VALUE_MAX;
break;
case ENOSYS:
LogError() << "The system does not support process-shared semaphores";
break;
default:
LogError() << "This should never happen. An unknown error occurred.";
break;
}
}

return cxx::success<>();
}

UnnamedSemaphore::~UnnamedSemaphore() noexcept
{
if (m_destroyHandle)
{
auto result = posixCall(iox_sem_destroy)(getHandle()).failureReturnValue(-1).evaluate();
if (result.has_error())
{
switch (result.get_error().errnum)
{
case EINVAL:
LogError() << "The semaphore handle was no longer valid. This can indicate a corrupted system.";
break;
default:
LogError() << "This should never happen. An unknown error occurred.";
break;
}
}
}
}

iox_sem_t* UnnamedSemaphore::getHandle() noexcept
{
return &m_handle;
}

const iox_sem_t* UnnamedSemaphore::getHandle() const noexcept
{
return &m_handle;
}


} // namespace posix
} // namespace iox
45 changes: 45 additions & 0 deletions iceoryx_hoofs/test/moduletests/test_posix_unnamed_semaphore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

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


namespace
{
using namespace ::testing;
using namespace iox::posix;
using namespace iox::cxx;

class UnnamedSemaphoreTest : public Test
{
public:
void SetUp() override
{
}

void TearDown() override
{
}

optional<UnnamedSemaphore> sut;
};

TEST_F(UnnamedSemaphoreTest, asd)
{
}
} // namespace

0 comments on commit 27c857e

Please sign in to comment.