Skip to content

Commit e66bd88

Browse files
committed
fix(platform): resolve ODR violation in make_timespec/calc_wait_time inline functions
Problem: - Both linux/get_wait_time.h and posix/get_wait_time.h define inline functions make_timespec() and calc_wait_time() in namespace ipc::detail - On Linux, semaphore uses posix implementation, but may include both headers - This causes ODR (One Definition Rule) violation - undefined behavior - Different inline function definitions with same name violates C++ standard - Manifested as test failures in SemaphoreTest::WaitTimeout Solution: - Add platform-specific namespace layer between ipc and detail: - linux/get_wait_time.h: ipc::linux::detail::make_timespec() - posix/get_wait_time.h: ipc::posix::detail::make_timespec() - Update all call sites to use fully qualified names: - linux/condition.h: linux::detail::make_timespec() - linux/mutex.h: linux::detail::make_timespec() (2 places) - posix/condition.h: posix::detail::make_timespec() - posix/mutex.h: posix::detail::make_timespec() (2 places) - posix/semaphore_impl.h: posix::detail::make_timespec() This ensures each platform's implementation is uniquely named, preventing ODR violations and ensuring correct function resolution at compile time.
1 parent ff74cdd commit e66bd88

File tree

7 files changed

+11
-7
lines changed

7 files changed

+11
-7
lines changed

src/libipc/platform/linux/condition.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class condition : public sync::obj_impl<a0_cnd_t> {
2727
return false;
2828
}
2929
} else {
30-
auto ts = detail::make_timespec(tm);
30+
auto ts = linux::detail::make_timespec(tm);
3131
int eno = A0_SYSERR(a0_cnd_timedwait(native(), static_cast<a0_mtx_t *>(mtx.native()), {ts}));
3232
if (eno != 0) {
3333
if (eno != ETIMEDOUT) {

src/libipc/platform/linux/get_wait_time.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "a0/err_macro.h"
1111

1212
namespace ipc {
13+
namespace linux {
1314
namespace detail {
1415

1516
inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept {
@@ -43,4 +44,5 @@ inline timespec make_timespec(std::uint64_t tm /*ms*/) noexcept(false) {
4344
}
4445

4546
} // namespace detail
47+
} // namespace linux
4648
} // namespace ipc

src/libipc/platform/linux/mutex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class robust_mutex : public sync::obj_impl<a0_mtx_t> {
2525
bool lock(std::uint64_t tm) noexcept {
2626
if (!valid()) return false;
2727
for (;;) {
28-
auto ts = detail::make_timespec(tm);
28+
auto ts = linux::detail::make_timespec(tm);
2929
int eno = A0_SYSERR(
3030
(tm == invalid_value) ? a0_mtx_lock(native())
3131
: a0_mtx_timedlock(native(), {ts}));
@@ -56,7 +56,7 @@ class robust_mutex : public sync::obj_impl<a0_mtx_t> {
5656

5757
bool try_lock() noexcept(false) {
5858
if (!valid()) return false;
59-
int eno = A0_SYSERR(a0_mtx_timedlock(native(), {detail::make_timespec(0)}));
59+
int eno = A0_SYSERR(a0_mtx_timedlock(native(), {linux::detail::make_timespec(0)}));
6060
switch (eno) {
6161
case 0:
6262
return true;

src/libipc/platform/posix/condition.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class condition {
115115
}
116116
break;
117117
default: {
118-
auto ts = detail::make_timespec(tm);
118+
auto ts = posix::detail::make_timespec(tm);
119119
int eno;
120120
if ((eno = ::pthread_cond_timedwait(cond_, static_cast<pthread_mutex_t *>(mtx.native()), &ts)) != 0) {
121121
if (eno != ETIMEDOUT) {

src/libipc/platform/posix/get_wait_time.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "libipc/utility/log.h"
1111

1212
namespace ipc {
13+
namespace posix {
1314
namespace detail {
1415

1516
inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept {
@@ -36,4 +37,5 @@ inline timespec make_timespec(std::uint64_t tm /*ms*/) noexcept(false) {
3637
}
3738

3839
} // namespace detail
40+
} // namespace posix
3941
} // namespace ipc

src/libipc/platform/posix/mutex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class mutex {
196196
bool lock(std::uint64_t tm) noexcept {
197197
if (!valid()) return false;
198198
for (;;) {
199-
auto ts = detail::make_timespec(tm);
199+
auto ts = posix::detail::make_timespec(tm);
200200
int eno = (tm == invalid_value)
201201
? ::pthread_mutex_lock(mutex_)
202202
: ::pthread_mutex_timedlock(mutex_, &ts);
@@ -230,7 +230,7 @@ class mutex {
230230

231231
bool try_lock() noexcept(false) {
232232
if (!valid()) return false;
233-
auto ts = detail::make_timespec(0);
233+
auto ts = posix::detail::make_timespec(0);
234234
int eno = ::pthread_mutex_timedlock(mutex_, &ts);
235235
switch (eno) {
236236
case 0:

src/libipc/platform/posix/semaphore_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class semaphore {
8888
return false;
8989
}
9090
} else {
91-
auto ts = detail::make_timespec(tm);
91+
auto ts = posix::detail::make_timespec(tm);
9292
if (::sem_timedwait(h_, &ts) != 0) {
9393
if (errno != ETIMEDOUT) {
9494
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",

0 commit comments

Comments
 (0)