F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
CountingSemaphore.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Darwin/CountingSemaphore.cpp
3 // \brief Darwin implementations for Os::CountingSemaphore
4 // ======================================================================
6 #include "Fw/Types/Assert.hpp"
7 #include "Os/Darwin/error.hpp"
8 
9 namespace Os {
10 namespace Darwin {
11 namespace Semaphore {
12 
14  long value = static_cast<long>(initial_count);
15  this->m_handle.m_semaphore = dispatch_semaphore_create(value);
16  FW_ASSERT(this->m_handle.m_semaphore != nullptr);
17  this->m_handle.m_initial_count = value;
18 }
19 
21  FW_ASSERT(this->m_handle.m_semaphore != nullptr);
22  // dispatch_release requires the semaphore value >= the initial value
23  // passed to dispatch_semaphore_create. Signal enough times to restore it.
24  for (long i = 0; i < this->m_handle.m_initial_count; ++i) {
25  dispatch_semaphore_signal(this->m_handle.m_semaphore);
26  }
27  dispatch_release(this->m_handle.m_semaphore);
28  this->m_handle.m_semaphore = nullptr;
29 }
30 
32  FW_ASSERT(this->m_handle.m_semaphore != nullptr);
33  long result = dispatch_semaphore_wait(this->m_handle.m_semaphore, DISPATCH_TIME_FOREVER);
34  FW_ASSERT(result == 0, static_cast<FwAssertArgType>(result));
36 }
37 
39  FW_ASSERT(this->m_handle.m_semaphore != nullptr);
40 
41  int64_t nsec = static_cast<int64_t>(interval.getSeconds()) * 1000000000LL +
42  static_cast<int64_t>(interval.getUSeconds()) * 1000LL;
43  dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, nsec);
44  long result = dispatch_semaphore_wait(this->m_handle.m_semaphore, timeout);
45 
47 }
48 
50  FW_ASSERT(this->m_handle.m_semaphore != nullptr);
51  long result = dispatch_semaphore_wait(this->m_handle.m_semaphore, DISPATCH_TIME_NOW);
53 }
54 
56  FW_ASSERT(this->m_handle.m_semaphore != nullptr);
57  // dispatch_semaphore_signal returns non-zero if a thread was woken, zero otherwise.
58  // Both outcomes are valid — no error condition exists.
59  (void)dispatch_semaphore_signal(this->m_handle.m_semaphore);
61 }
62 
64  return &m_handle;
65 }
66 
67 } // namespace Semaphore
68 } // namespace Darwin
69 } // namespace Os
Operation succeeded.
Definition: Os.hpp:27
Status tryWait() override
non-blocking attempt to decrement the semaphore
Status waitTimeout(const Fw::TimeInterval &interval) override
wait on the semaphore with a timeout
CountingSemaphoreHandle * getHandle() override
return the underlying semaphore handle (implementation specific)
Status post() override
post (increment) the semaphore, potentially waking a waiting thread
Os::CountingSemaphore::Status dispatch_result_to_semaphore_status(long dispatch_result)
Definition: error.cpp:10
U32 getUSeconds() const
Status wait() override
wait (decrement) the semaphore, blocking if count is zero
#define FW_ASSERT(...)
Definition: Assert.hpp:14
U32 getSeconds() const