| 136 | template<ptrdiff_t LeastMaxValue> |
| 137 | template<class Rep, class Period> |
| 138 | bool CountingSemaphoreRP<LeastMaxValue>::try_acquire_for( |
| 139 | const std::chrono::duration<Rep, Period>& rel_time) { // okay std namespace |
| 140 | |
| 141 | if (mSpinlock == nullptr) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | // Convert duration to microseconds |
| 146 | auto us = std::chrono::duration_cast<std::chrono::microseconds>(rel_time); // okay std namespace |
| 147 | absolute_time_t timeout = make_timeout_time_us(us.count()); |
| 148 | |
| 149 | spin_lock_t* spinlock = static_cast<spin_lock_t*>(mSpinlock); |
| 150 | |
| 151 | // Spin until we can decrement the count or timeout |
| 152 | while (true) { |
| 153 | u32 save = spin_lock_blocking(spinlock); |
| 154 | |
| 155 | if (mCount > 0) { |
| 156 | mCount--; |
| 157 | spin_unlock(spinlock, save); |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | spin_unlock(spinlock, save); |
| 162 | |
| 163 | // Check if we've timed out |
| 164 | if (time_reached(timeout)) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | // Yield to avoid hogging CPU |
| 169 | tight_loop_contents(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | template<ptrdiff_t LeastMaxValue> |
| 174 | template<class Clock, class Duration> |