mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
Problem: - 'linux' is a predefined macro on Linux platforms - Using 'namespace linux' causes compilation errors - Preprocessor replaces 'linux' with '1' before compilation Solution: - Rename 'namespace linux' to 'namespace linux_' - Rename 'namespace posix' to 'namespace posix_' - Update all 7 call sites accordingly: - 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 prevents preprocessor macro expansion issues while maintaining the ODR violation fix from the previous commit.
67 lines
1.9 KiB
C++
67 lines
1.9 KiB
C++
#pragma once
|
|
|
|
#include "libipc/utility/log.h"
|
|
#include "libipc/mutex.h"
|
|
|
|
#include "get_wait_time.h"
|
|
#include "sync_obj_impl.h"
|
|
|
|
#include "a0/err_macro.h"
|
|
#include "a0/mtx.h"
|
|
|
|
namespace ipc {
|
|
namespace detail {
|
|
namespace sync {
|
|
|
|
class condition : public sync::obj_impl<a0_cnd_t> {
|
|
public:
|
|
condition() = default;
|
|
~condition() = default;
|
|
|
|
bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
|
|
if (!valid()) return false;
|
|
if (tm == invalid_value) {
|
|
int eno = A0_SYSERR(a0_cnd_wait(native(), static_cast<a0_mtx_t *>(mtx.native())));
|
|
if (eno != 0) {
|
|
ipc::error("fail condition wait[%d]\n", eno);
|
|
return false;
|
|
}
|
|
} else {
|
|
auto ts = linux_::detail::make_timespec(tm);
|
|
int eno = A0_SYSERR(a0_cnd_timedwait(native(), static_cast<a0_mtx_t *>(mtx.native()), {ts}));
|
|
if (eno != 0) {
|
|
if (eno != ETIMEDOUT) {
|
|
ipc::error("fail condition timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
|
eno, tm, ts.tv_sec, ts.tv_nsec);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool notify(ipc::sync::mutex &mtx) noexcept {
|
|
if (!valid()) return false;
|
|
int eno = A0_SYSERR(a0_cnd_signal(native(), static_cast<a0_mtx_t *>(mtx.native())));
|
|
if (eno != 0) {
|
|
ipc::error("fail condition notify[%d]\n", eno);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool broadcast(ipc::sync::mutex &mtx) noexcept {
|
|
if (!valid()) return false;
|
|
int eno = A0_SYSERR(a0_cnd_broadcast(native(), static_cast<a0_mtx_t *>(mtx.native())));
|
|
if (eno != 0) {
|
|
ipc::error("fail condition broadcast[%d]\n", eno);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
};
|
|
|
|
} // namespace sync
|
|
} // namespace detail
|
|
} // namespace ipc
|