Skip to content

iceoryx_utils/posix_wrapper/semaphore.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::posix

Classes🔗

Name
struct iox::posix::CreateUnnamedSingleProcessSemaphore_t
struct iox::posix::CreateUnnamedSharedMemorySemaphore_t
struct iox::posix::CreateNamedSemaphore_t
struct iox::posix::OpenNamedSemaphore_t
class iox::posix::Semaphore
Posix semaphore C++ Wrapping class.

Source code🔗

// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 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_UTILS_POSIX_WRAPPER_SEMAPHORE_HPP
#define IOX_UTILS_POSIX_WRAPPER_SEMAPHORE_HPP

#include "iceoryx_utils/cxx/expected.hpp"
#include "iceoryx_utils/cxx/helplets.hpp"
#include "iceoryx_utils/cxx/smart_c.hpp"
#include "iceoryx_utils/cxx/string.hpp"
#include "iceoryx_utils/design_pattern/creation.hpp"
#include "iceoryx_utils/internal/relocatable_pointer/relative_pointer.hpp"
#include "iceoryx_utils/internal/units/duration.hpp"
#include "iceoryx_utils/platform/fcntl.hpp"
#include "iceoryx_utils/platform/semaphore.hpp"
#include "iceoryx_utils/platform/stat.hpp"

#include <cstring>

namespace iox
{
namespace posix
{
enum class SemaphoreError
{
    INVALID_STATE,
    CREATION_FAILED,
    NAME_TOO_LONG,
    UNABLE_TO_OPEN_HANDLE,
    INVALID_SEMAPHORE_HANDLE,
    SEMAPHORE_OVERFLOW,
    INTERRUPTED_BY_SIGNAL_HANDLER,
    UNDEFINED
};

enum class SemaphoreWaitState
{
    TIMEOUT,
    NO_TIMEOUT,
};

struct CreateUnnamedSingleProcessSemaphore_t
{
};
struct CreateUnnamedSharedMemorySemaphore_t
{
};
struct CreateNamedSemaphore_t
{
};
struct OpenNamedSemaphore_t
{
};
static constexpr CreateUnnamedSingleProcessSemaphore_t CreateUnnamedSingleProcessSemaphore =
    CreateUnnamedSingleProcessSemaphore_t();
static constexpr CreateUnnamedSharedMemorySemaphore_t CreateUnnamedSharedMemorySemaphore =
    CreateUnnamedSharedMemorySemaphore_t();
static constexpr CreateNamedSemaphore_t CreateNamedSemaphore = CreateNamedSemaphore_t();
static constexpr OpenNamedSemaphore_t OpenNamedSemaphore = OpenNamedSemaphore_t();

class Semaphore : public DesignPattern::Creation<Semaphore, SemaphoreError>
{
  public:
    Semaphore() noexcept;

    Semaphore(Semaphore&& rhs) noexcept;

    Semaphore& operator=(Semaphore&& rhs) noexcept;

    Semaphore(const Semaphore&) = delete;

    Semaphore& operator=(const Semaphore&) = delete;

    ~Semaphore() noexcept;

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

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

    cxx::expected<SemaphoreWaitState, SemaphoreError> timedWait(const units::Duration abs_timeout,
                                                                const bool doContinueOnInterrupt) const noexcept;

    cxx::expected<bool, SemaphoreError> tryWait() const noexcept;

    cxx::expected<SemaphoreError> wait() const noexcept;

    iox_sem_t* getHandle() noexcept;

  private:
    cxx::string<128> m_name;
    bool m_isCreated = true;
    bool m_isNamedSemaphore = true;
    bool m_isShared = false;

    mutable iox_sem_t m_handle;
    mutable iox::rp::RelativePointer<iox_sem_t> m_handlePtr = &m_handle;

  private:
    friend class DesignPattern::Creation<Semaphore, SemaphoreError>;

    Semaphore(CreateUnnamedSingleProcessSemaphore_t, const unsigned int value) noexcept;

    Semaphore(CreateUnnamedSharedMemorySemaphore_t, const unsigned int value) noexcept;
    Semaphore(CreateUnnamedSharedMemorySemaphore_t, iox_sem_t* handle, const unsigned int value) noexcept;

    Semaphore(OpenNamedSemaphore_t, const char* name, const int oflag) noexcept;

    Semaphore(CreateNamedSemaphore_t, const char* name, const mode_t mode, const unsigned int value) noexcept;

    bool close() noexcept;

    bool destroy() noexcept;

    bool init(iox_sem_t* handle, const int pshared, const unsigned int value) noexcept;

    bool open(const int oflag) noexcept;

    bool open(const int oflag, const mode_t mode, const unsigned int value) noexcept;

    bool unlink(const char* name) noexcept;

    bool isNamedSemaphore() noexcept;

    void closeHandle() noexcept;

    template <typename SmartC>
    bool setHandleFromCall(const SmartC& call) noexcept;

    SemaphoreError errnoToEnum(const int errnoValue) const noexcept;
};
} // namespace posix
} // namespace iox

#include "iceoryx_utils/internal/posix_wrapper/semaphore.inl"

#endif // IOX_UTILS_POSIX_WRAPPER_SEMAPHORE_HPP

Updated on 31 May 2022 at 15:29:15 CEST