forked from eclipse-iceoryx/iceoryx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iox-eclipse-iceoryx#751 Add first unnamed semaphore interface and tests
Signed-off-by: Christian Eltzschig <me@elchris.org>
- Loading branch information
Showing
4 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
iceoryx_hoofs/include/iceoryx_hoofs/posix_wrapper/unnamed_semaphore.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
iceoryx_hoofs/test/moduletests/test_posix_unnamed_semaphore.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|