Skip to content

Commit 0693d54

Browse files
committed
iox-#33 Simplify semaphore creation
Restore cout/cerr/clog flags when changing Signed-off-by: Christian Eltzschig <[email protected]>
1 parent 98355e6 commit 0693d54

File tree

6 files changed

+52
-35
lines changed

6 files changed

+52
-35
lines changed

iceoryx_examples/waitset/ice_waitset_gateway.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ void subscriberCallback(iox::popo::UntypedSubscriber* const subscriber, uint64_t
4545
{
4646
subscriber->take().and_then([&](auto& userPayload) {
4747
auto chunkHeader = iox::mepoo::ChunkHeader::fromUserPayload(userPayload);
48+
auto flags = std::cout.flags();
4849
std::cout << "subscriber: " << std::hex << subscriber << " length: " << std::dec
4950
<< chunkHeader->userPayloadSize() << " ptr: " << std::hex << chunkHeader->userPayload()
50-
<< std::endl;
51+
<< std::dec << std::endl;
52+
std::cout.setf(flags);
5153
});
5254
// no nullptr check required since it is guaranteed != nullptr
5355
++(*sumOfAllSamples);
@@ -114,7 +116,9 @@ int main()
114116
}
115117
}
116118

119+
auto flags = std::cout.flags();
117120
std::cout << "sum of all samples: " << std::dec << sumOfAllSamples << std::endl;
121+
std::cout.setf(flags);
118122
}
119123

120124
return (EXIT_SUCCESS);

iceoryx_examples/waitset/ice_waitset_grouping.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ int main()
100100
auto subscriber = notification->getOrigin<iox::popo::UntypedSubscriber>();
101101
subscriber->take().and_then([&](auto& userPayload) {
102102
const CounterTopic* data = static_cast<const CounterTopic*>(userPayload);
103+
auto flags = std::cout.flags();
103104
std::cout << "received: " << std::dec << data->counter << std::endl;
105+
std::cout.setf(flags);
104106
subscriber->release(userPayload);
105107
});
106108
}

iceoryx_hoofs/include/iceoryx_hoofs/internal/posix_wrapper/posix_call.inl

+2
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,11 @@ PosixCallEvaluator<ReturnType>::evaluate() const&& noexcept
163163
}
164164
else if (!m_details.hasSilentErrno)
165165
{
166+
auto flags = std::cerr.flags();
166167
std::cerr << m_details.file << ":" << std::dec << m_details.line << " { " << m_details.callingFunction << " -> "
167168
<< m_details.posixFunctionName << " } ::: [ " << std::dec << m_details.result.errnum << " ] "
168169
<< m_details.result.getHumanReadableErrnum() << std::endl;
170+
std::cerr.setf(flags);
169171
}
170172

171173
return iox::cxx::error<PosixCallResult<ReturnType>>(m_details.result);

iceoryx_hoofs/source/posix_wrapper/named_pipe.cpp

+38-33
Original file line numberDiff line numberDiff line change
@@ -104,48 +104,53 @@ NamedPipe::NamedPipe(const IpcChannelName_t& name,
104104
return;
105105
}
106106

107-
cxx::expected<Semaphore, SemaphoreError> sendSemaphore = cxx::error<SemaphoreError>(SemaphoreError::UNDEFINED);
108-
cxx::expected<Semaphore, SemaphoreError> receiveSemaphore = cxx::error<SemaphoreError>(SemaphoreError::UNDEFINED);
109-
if (sharedMemory->hasOwnership())
107+
m_sharedMemory.emplace(std::move(*sharedMemory));
108+
109+
m_isInitialized = true;
110+
auto signalError = [&](const char* name, const char* openMode) {
111+
std::cerr << "Unable to " << openMode << " the named pipe semaphore \"" << name << "\" for named pipe \""
112+
<< name << "\"" << std::endl;
113+
m_isInitialized = false;
114+
m_errorValue = IpcChannelError::INTERNAL_LOGIC_ERROR;
115+
};
116+
117+
if (m_sharedMemory->hasOwnership())
110118
{
111-
sendSemaphore = Semaphore::create(CreateNamedSemaphore,
112-
convertName(SEND_SEMAPHORE_PREFIX, name).c_str(),
113-
static_cast<mode_t>(S_IRUSR | S_IWUSR),
114-
static_cast<unsigned int>(maxMsgNumber));
115-
receiveSemaphore = Semaphore::create(CreateNamedSemaphore,
116-
convertName(RECEIVE_SEMAPHORE_PREFIX, name).c_str(),
117-
static_cast<mode_t>(S_IRUSR | S_IWUSR),
118-
0U);
119+
Semaphore::create(CreateNamedSemaphore,
120+
convertName(SEND_SEMAPHORE_PREFIX, name).c_str(),
121+
static_cast<mode_t>(S_IRUSR | S_IWUSR),
122+
static_cast<unsigned int>(maxMsgNumber))
123+
.and_then([&](auto& r) { m_sendSemaphore.emplace(std::move(r)); })
124+
.or_else([&](auto) { signalError(convertName(SEND_SEMAPHORE_PREFIX, name).c_str(), "create"); });
125+
126+
Semaphore::create(CreateNamedSemaphore,
127+
convertName(RECEIVE_SEMAPHORE_PREFIX, name).c_str(),
128+
static_cast<mode_t>(S_IRUSR | S_IWUSR),
129+
0U)
130+
.and_then([&](auto& r) { m_receiveSemaphore.emplace(std::move(r)); })
131+
.or_else([&](auto) { signalError(convertName(RECEIVE_SEMAPHORE_PREFIX, name).c_str(), "create"); });
119132
}
120133
else
121134
{
122-
sendSemaphore = Semaphore::create(OpenNamedSemaphore, convertName(SEND_SEMAPHORE_PREFIX, name).c_str(), 0);
123-
receiveSemaphore =
124-
Semaphore::create(OpenNamedSemaphore, convertName(RECEIVE_SEMAPHORE_PREFIX, name).c_str(), 0);
125-
}
135+
Semaphore::create(OpenNamedSemaphore, convertName(SEND_SEMAPHORE_PREFIX, name).c_str(), 0)
136+
.and_then([&](auto& r) { m_sendSemaphore.emplace(std::move(r)); })
137+
.or_else([&](auto) { signalError(convertName(SEND_SEMAPHORE_PREFIX, name).c_str(), "open"); });
126138

127-
if (sendSemaphore.has_error() || receiveSemaphore.has_error())
128-
{
129-
std::cerr << "Unable to create or open named pipe semaphores: \"" << convertName(SEND_SEMAPHORE_PREFIX, name)
130-
<< "\" and \"" << convertName(RECEIVE_SEMAPHORE_PREFIX, name) << "\" for named pipe \"" << name
131-
<< "\"" << std::endl;
132-
m_isInitialized = false;
133-
m_errorValue = IpcChannelError::INTERNAL_LOGIC_ERROR;
134-
return;
139+
Semaphore::create(OpenNamedSemaphore, convertName(RECEIVE_SEMAPHORE_PREFIX, name).c_str(), 0)
140+
.and_then([&](auto& r) { m_receiveSemaphore.emplace(std::move(r)); })
141+
.or_else([&](auto) { signalError(convertName(RECEIVE_SEMAPHORE_PREFIX, name).c_str(), "open"); });
135142
}
136143

137-
m_receiveSemaphore.emplace(std::move(*receiveSemaphore));
138-
m_sendSemaphore.emplace(std::move(*sendSemaphore));
139-
140-
m_sharedMemory.emplace(std::move(*sharedMemory));
141-
m_messages =
142-
static_cast<MessageQueue_t*>(m_sharedMemory->allocate(sizeof(MessageQueue_t), alignof(MessageQueue_t)));
143-
144-
if (channelSide == IpcChannelSide::SERVER)
144+
if (m_isInitialized)
145145
{
146-
new (m_messages) MessageQueue_t();
146+
m_messages =
147+
static_cast<MessageQueue_t*>(m_sharedMemory->allocate(sizeof(MessageQueue_t), alignof(MessageQueue_t)));
148+
149+
if (channelSide == IpcChannelSide::SERVER)
150+
{
151+
new (m_messages) MessageQueue_t();
152+
}
147153
}
148-
m_isInitialized = true;
149154
}
150155

151156
NamedPipe::NamedPipe(NamedPipe&& rhs) noexcept

iceoryx_hoofs/source/posix_wrapper/shared_memory_object.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ SharedMemoryObject::SharedMemoryObject(const SharedMemory::Name_t& name,
7676

7777
if (m_isInitialized == false)
7878
{
79+
auto flags = std::cerr.flags();
7980
std::cerr << "Unable to create a shared memory object with the following properties [ name = " << name
8081
<< ", sizeInBytes = " << memorySizeInBytes
8182
<< ", access mode = " << ACCESS_MODE_STRING[static_cast<uint64_t>(accessMode)]
8283
<< ", open mode = " << OPEN_MODE_STRING[static_cast<uint64_t>(openMode)]
83-
<< ", baseAddressHint = " << std::hex << baseAddressHint
84+
<< ", baseAddressHint = " << std::hex << baseAddressHint << std::dec
8485
<< ", permissions = " << std::bitset<sizeof(mode_t)>(permissions) << " ]" << std::endl;
86+
std::cerr.setf(flags);
8587
return;
8688
}
8789

iceoryx_hoofs/source/posix_wrapper/shared_memory_object/memory_map.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ MemoryMap::MemoryMap(const void* baseAddressHint,
5353
})
5454
.or_else([&](auto& r) {
5555
constexpr uint64_t FLAGS_BIT_SIZE = 32U;
56+
auto flags = std::cerr.flags();
5657
std::cerr << "Unable to map memory with the following properties [ baseAddressHint = " << std::hex
5758
<< baseAddressHint << ", length = " << std::dec << m_length
5859
<< ", fileDescriptor = " << fileDescriptor
5960
<< ", access mode = " << ACCESS_MODE_STRING[static_cast<uint64_t>(accessMode)]
6061
<< ", flags = " << std::bitset<FLAGS_BIT_SIZE>(static_cast<uint32_t>(flags))
6162
<< ", offset = " << std::hex << offset << std::dec << " ]" << std::endl;
63+
std::cerr.setf(flags);
6264
this->m_errorValue = this->errnoToEnum(r.errnum);
6365
this->m_isInitialized = false;
6466
this->m_baseAddress = nullptr;

0 commit comments

Comments
 (0)