Skip to content

Commit

Permalink
iox-#33 Cleanup code, remove deprecated semaphore ctor, privatize get…
Browse files Browse the repository at this point in the history
…Handle in semaphore

Signed-off-by: Christian Eltzschig <me@elchris.org>
  • Loading branch information
elfenpiff committed Jun 17, 2021
1 parent 8c6e0b9 commit 0a8ae0b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 50 deletions.
10 changes: 4 additions & 6 deletions iceoryx_hoofs/include/iceoryx_hoofs/posix_wrapper/semaphore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ class Semaphore : public DesignPattern::Creation<Semaphore, SemaphoreError>
/// @return if an error during the call occurs the error value is set
cxx::expected<SemaphoreError> wait() noexcept;

/// @brief returns the pointer to the managed semaphore. You can use this
/// pointer with all the sem_** functions.
iox_sem_t* getHandle() const noexcept;

private:
cxx::string<128> m_name;
bool m_isCreated = true;
Expand All @@ -199,8 +195,6 @@ class Semaphore : public DesignPattern::Creation<Semaphore, SemaphoreError>
/// For details see man sem_init.
/// @param[in] value initial value of the semaphore
Semaphore(CreateUnnamedSharedMemorySemaphore_t, const unsigned int value) noexcept;
/// @deprecated do not use this constructor
Semaphore(CreateUnnamedSharedMemorySemaphore_t, iox_sem_t* handle, const unsigned int value) noexcept;

/// @brief Opens an already existing named semaphore. If a semaphore with
/// name does not exist an uninitialized Semaphore is returned
Expand Down Expand Up @@ -305,6 +299,10 @@ class Semaphore : public DesignPattern::Creation<Semaphore, SemaphoreError>
/// @return returns false when sem_open fails otherwise true
bool open(const int oflag) noexcept;

/// @brief returns the pointer to the managed semaphore. You can use this
/// pointer with all the sem_** functions.
iox_sem_t* getHandle() const noexcept;

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

/// @brief calls sem_unlink which removes a named semaphore
Expand Down
55 changes: 27 additions & 28 deletions iceoryx_hoofs/source/posix_wrapper/named_pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,34 +115,6 @@ NamedPipe::NamedPipe(const IpcChannelName_t& name,
}
}

NamedPipe::NamedPipeData::NamedPipeData(bool& isInitialized,
IpcChannelError& error,
const uint64_t maxMsgNumber) noexcept
{
auto signalError = [&](const char* name) {
std::cerr << "Unable to create " << name << " semaphore for named pipe \"" << 'x' << "\"";
isInitialized = false;
error = IpcChannelError::INTERNAL_LOGIC_ERROR;
};

Semaphore::placementCreate(
&semaphores[SEND_SEMAPHORE], CreateUnnamedSharedMemorySemaphore, static_cast<unsigned int>(maxMsgNumber))
.or_else([&](auto) { signalError("send"); });

Semaphore::placementCreate(&semaphores[RECEIVE_SEMAPHORE], CreateUnnamedSharedMemorySemaphore, 0U)
.or_else([&](auto) { signalError("receive"); });
}

Semaphore& NamedPipe::NamedPipeData::sendSemaphore() noexcept
{
return reinterpret_cast<Semaphore&>(semaphores[SEND_SEMAPHORE]);
}

Semaphore& NamedPipe::NamedPipeData::receiveSemaphore() noexcept
{
return reinterpret_cast<Semaphore&>(semaphores[RECEIVE_SEMAPHORE]);
}

NamedPipe::NamedPipe(NamedPipe&& rhs) noexcept
{
*this = std::move(rhs);
Expand Down Expand Up @@ -340,6 +312,33 @@ cxx::expected<std::string, IpcChannelError> NamedPipe::timedReceive(const units:
return cxx::error<IpcChannelError>(IpcChannelError::TIMEOUT);
}

NamedPipe::NamedPipeData::NamedPipeData(bool& isInitialized,
IpcChannelError& error,
const uint64_t maxMsgNumber) noexcept
{
auto signalError = [&](const char* name) {
std::cerr << "Unable to create " << name << " semaphore for named pipe \"" << 'x' << "\"";
isInitialized = false;
error = IpcChannelError::INTERNAL_LOGIC_ERROR;
};

Semaphore::placementCreate(
&semaphores[SEND_SEMAPHORE], CreateUnnamedSharedMemorySemaphore, static_cast<unsigned int>(maxMsgNumber))
.or_else([&](auto) { signalError("send"); });

Semaphore::placementCreate(&semaphores[RECEIVE_SEMAPHORE], CreateUnnamedSharedMemorySemaphore, 0U)
.or_else([&](auto) { signalError("receive"); });
}

Semaphore& NamedPipe::NamedPipeData::sendSemaphore() noexcept
{
return reinterpret_cast<Semaphore&>(semaphores[SEND_SEMAPHORE]);
}

Semaphore& NamedPipe::NamedPipeData::receiveSemaphore() noexcept
{
return reinterpret_cast<Semaphore&>(semaphores[RECEIVE_SEMAPHORE]);
}

} // namespace posix
} // namespace iox
16 changes: 0 additions & 16 deletions iceoryx_hoofs/source/posix_wrapper/semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,6 @@ Semaphore::Semaphore(CreateUnnamedSharedMemorySemaphore_t, const unsigned int va
}
}

Semaphore::Semaphore(CreateUnnamedSharedMemorySemaphore_t, iox_sem_t* handle, const unsigned int value) noexcept
: m_isNamedSemaphore(false)
, m_isShared(true)
, m_handlePtr(handle)
{
if (init(handle, 1, value))
{
m_isInitialized = true;
}
else
{
m_isInitialized = false;
m_errorValue = SemaphoreError::CREATION_FAILED;
}
}

Semaphore::Semaphore(OpenNamedSemaphore_t, const char* name, const int oflag) noexcept
: m_isCreated(false)
{
Expand Down

0 comments on commit 0a8ae0b

Please sign in to comment.