GCC Code Coverage Report


Directory: ./
File: Os/Posix/CountingSemaphore.cpp
Date: 2026-09-23 21:11:01
Exec Total Coverage
Lines: 35 37 94.6%
Functions: 6 7 85.7%
Branches: 22 34 64.7%

Line Branch Exec Source
1 // ======================================================================
2 // \title Os/Posix/CountingSemaphore.cpp
3 // \brief Posix implementations for Os::CountingSemaphore
4 // ======================================================================
5 #include "Os/Posix/CountingSemaphore.hpp"
6 #include <cerrno>
7 #include <ctime>
8 #include "Fw/Types/Assert.hpp"
9 #include "Os/Posix/error.hpp"
10
11 namespace Os {
12 namespace Posix {
13 namespace Semaphore {
14
15 2585 PosixCountingSemaphore::PosixCountingSemaphore(U32 initial_count) {
16 // Default all pthread semaphores to only be visible to single application (since F' FSW is a single application)
17
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2585 times.
2585 int status = sem_init(&this->m_handle.m_semaphore, 0, initial_count);
18 2585 FW_ASSERT(status == 0, static_cast<FwAssertArgType>(status));
19 2585 }
20
21 5170 PosixCountingSemaphore::~PosixCountingSemaphore() {
22
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2585 times.
5170 (void)sem_destroy(&this->m_handle.m_semaphore);
23 5170 }
24
25 1044 PosixCountingSemaphore::Status PosixCountingSemaphore::wait() {
26 1044 int status = 0;
27 // @non-terminating@: retry on EINTR
28 do {
29
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1044 times.
1044 status = sem_wait(&this->m_handle.m_semaphore);
30
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1044 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1044 } while (status != 0 && errno == EINTR);
31 1044 FW_ASSERT(status == 0 || errno != 0, status);
32
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1044 times.
1044 return status == 0 ? Status::OP_OK : posix_status_to_semaphore_status(errno);
33 }
34
35 8 PosixCountingSemaphore::Status PosixCountingSemaphore::waitTimeout(const Fw::TimeInterval& interval) {
36 8 struct timespec abstime{};
37 8 int clock_status = clock_gettime(CLOCK_REALTIME, &abstime);
38 8 FW_ASSERT(clock_status == 0, static_cast<FwAssertArgType>(clock_status));
39
40
1/1
✓ Branch 4 taken 8 times.
8 abstime.tv_sec += interval.getSeconds();
41
1/1
✓ Branch 4 taken 8 times.
8 abstime.tv_nsec += static_cast<long>(interval.getUSeconds()) * 1000L;
42
43
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (abstime.tv_nsec >= 1000000000L) {
44 1 abstime.tv_sec += abstime.tv_nsec / 1000000000L;
45 1 abstime.tv_nsec = abstime.tv_nsec % 1000000000L;
46 }
47
48 8 int status = 0;
49 // @non-terminating@: retry on EINTR
50 do {
51
2/3
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✓ Branch 5 taken 8 times.
8 status = sem_timedwait(&this->m_handle.m_semaphore, &abstime);
52
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
8 } while (status != 0 && errno == EINTR);
53 8 FW_ASSERT(status == 0 || errno != 0, status);
54
3/3
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
16 return status == 0 ? Status::OP_OK : posix_status_to_semaphore_status(errno);
55 }
56
57 234713 PosixCountingSemaphore::Status PosixCountingSemaphore::tryWait() {
58
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 235493 times.
234713 int status = sem_trywait(&this->m_handle.m_semaphore);
59 235725 FW_ASSERT(status == 0 || errno != 0, status);
60
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 236467 times.
236479 return status == 0 ? Status::OP_OK : posix_status_to_semaphore_status(errno);
61 }
62
63 236324 PosixCountingSemaphore::Status PosixCountingSemaphore::post() {
64
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 237194 times.
236324 int status = sem_post(&this->m_handle.m_semaphore);
65 237416 FW_ASSERT(status == 0 || errno != 0, status);
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237631 times.
237631 return status == 0 ? Status::OP_OK : posix_status_to_semaphore_status(errno);
67 }
68
69 ✗ CountingSemaphoreHandle* PosixCountingSemaphore::getHandle() {
70 ✗ return &m_handle;
71 }
72
73 } // namespace Semaphore
74 } // namespace Posix
75 } // namespace Os
76