1#include "semaphore.hpp"
3#include <linux/futex.h>
4#include <sys/syscall.h>
12#include "libxr_def.hpp"
13#include "monotonic_time.hpp"
20int FutexWait(std::atomic<uint32_t>* word, uint32_t expected,
const struct timespec* timeout)
22 return static_cast<int>(syscall(SYS_futex,
reinterpret_cast<uint32_t*
>(word), FUTEX_WAIT,
23 expected, timeout,
nullptr, 0));
26int FutexWake(std::atomic<uint32_t>* word,
int count)
28 return static_cast<int>(syscall(SYS_futex,
reinterpret_cast<uint32_t*
>(word), FUTEX_WAKE,
29 count,
nullptr,
nullptr, 0));
46 const uint32_t prev =
semaphore_handle_->count.fetch_add(1, std::memory_order_release);
55 const uint64_t deadline_ms =
56 (timeout == UINT32_MAX) ? UINT64_MAX : (MonotonicTime::NowMilliseconds() + timeout);
65 std::memory_order_acq_rel,
66 std::memory_order_relaxed))
73 timespec* ts_ptr =
nullptr;
74 if (timeout != UINT32_MAX)
76 const uint32_t remaining_ms = MonotonicTime::RemainingMilliseconds(deadline_ms);
77 if (remaining_ms == 0)
81 ts = MonotonicTime::RelativeFromMilliseconds(remaining_ms);
86 if (ans == 0 || errno == EAGAIN || errno == EINTR)
91 if (errno == ETIMEDOUT)
void Post()
释放(增加)信号量 Releases (increments) the semaphore
void PostFromCallback(bool in_isr)
从中断回调中释放(增加)信号量 Releases (increments) the semaphore from an ISR (Interrupt Service Routine)
~Semaphore()
析构信号量对象,释放资源 Destroys the semaphore object and releases resources
libxr_semaphore_handle semaphore_handle_
信号量句柄 Semaphore handle
Semaphore(uint32_t init_count=0)
构造一个信号量对象 Constructs a semaphore object
size_t Value()
获取当前信号量的值 Gets the current value of the semaphore
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
@ FAILED
操作失败 | Operation failed
@ OK
操作成功 | Operation successful