@@ -28,6 +28,7 @@ constexpr const char NamedPipe::NAMED_PIPE_PREFIX[];
28
28
constexpr const char NamedPipe::SEND_SEMAPHORE_PREFIX[];
29
29
constexpr const char NamedPipe::RECEIVE_SEMAPHORE_PREFIX[];
30
30
constexpr units::Duration NamedPipe::CYCLE_TIME;
31
+ constexpr units::Duration NamedPipe::NamedPipeData::WAIT_FOR_INIT_SLEEP_TIME;
31
32
32
33
NamedPipe::NamedPipe () noexcept
33
34
{
@@ -113,6 +114,14 @@ NamedPipe::NamedPipe(const IpcChannelName_t& name,
113
114
{
114
115
new (m_data) NamedPipeData (m_isInitialized, m_errorValue, maxMsgNumber);
115
116
}
117
+ else
118
+ {
119
+ m_isInitialized = m_data->waitForInitialization ();
120
+ if (m_isInitialized == false )
121
+ {
122
+ m_errorValue = IpcChannelError::INTERNAL_LOGIC_ERROR;
123
+ }
124
+ }
116
125
}
117
126
118
127
NamedPipe::NamedPipe (NamedPipe&& rhs) noexcept
@@ -326,8 +335,29 @@ NamedPipe::NamedPipeData::NamedPipeData(bool& isInitialized,
326
335
&semaphores[SEND_SEMAPHORE], CreateUnnamedSharedMemorySemaphore, static_cast <unsigned int >(maxMsgNumber))
327
336
.or_else ([&](auto ) { signalError (" send" ); });
328
337
338
+ if (!isInitialized)
339
+ {
340
+ return ;
341
+ }
342
+
329
343
Semaphore::placementCreate (&semaphores[RECEIVE_SEMAPHORE], CreateUnnamedSharedMemorySemaphore, 0U )
330
344
.or_else ([&](auto ) { signalError (" receive" ); });
345
+
346
+ if (!isInitialized)
347
+ {
348
+ return ;
349
+ }
350
+
351
+ initializationGuard.store (VALID_DATA);
352
+ }
353
+
354
+ NamedPipe::NamedPipeData::~NamedPipeData () noexcept
355
+ {
356
+ if (hasValidState ())
357
+ {
358
+ sendSemaphore ().~Semaphore ();
359
+ receiveSemaphore ().~Semaphore ();
360
+ }
331
361
}
332
362
333
363
Semaphore& NamedPipe::NamedPipeData::sendSemaphore () noexcept
@@ -340,5 +370,32 @@ Semaphore& NamedPipe::NamedPipeData::receiveSemaphore() noexcept
340
370
return reinterpret_cast <Semaphore&>(semaphores[RECEIVE_SEMAPHORE]);
341
371
}
342
372
373
+ bool NamedPipe::NamedPipeData::waitForInitialization () const noexcept
374
+ {
375
+ if (hasValidState ())
376
+ {
377
+ return true ;
378
+ }
379
+
380
+ units::Duration remainingTime = WAIT_FOR_INIT_TIMEOUT;
381
+
382
+ while (remainingTime.toNanoseconds () > 0U )
383
+ {
384
+ std::this_thread::sleep_for (std::chrono::nanoseconds (WAIT_FOR_INIT_SLEEP_TIME.toNanoseconds ()));
385
+ remainingTime = remainingTime - WAIT_FOR_INIT_SLEEP_TIME;
386
+ if (hasValidState ())
387
+ {
388
+ return true ;
389
+ }
390
+ }
391
+
392
+ return false ;
393
+ }
394
+
395
+ bool NamedPipe::NamedPipeData::hasValidState () const noexcept
396
+ {
397
+ return initializationGuard.load () == VALID_DATA;
398
+ }
399
+
343
400
} // namespace posix
344
401
} // namespace iox
0 commit comments