Skip to content

iceoryx_hoofs/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_HOOFS_POSIX_WRAPPER_SEMAPHORE_HPP
#define IOX_HOOFS_POSIX_WRAPPER_SEMAPHORE_HPP

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

#include <cstring>

namespace iox
{
namespace posix
{
enum class SemaphoreError
{
    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) noexcept;

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

    cxx::expected<SemaphoreError> wait() 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_sem_t* m_handlePtr = nullptr;

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

    Semaphore(CreateUnnamedSingleProcessSemaphore_t, const unsigned int value) noexcept;

    Semaphore(CreateUnnamedSharedMemorySemaphore_t, 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;

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

    bool open(const int oflag) noexcept;

    iox_sem_t* getHandle() const noexcept;

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

    static bool unlink(const char* name) noexcept;

    bool isNamedSemaphore() const noexcept;

    void closeHandle() noexcept;

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

#endif // IOX_HOOFS_POSIX_WRAPPER_SEMAPHORE_HPP

Updated on 18 December 2023 at 13:11:43 CET