Skip to content

Commit 9cc1caa

Browse files
authored
Merge pull request #10596 from kjbracey-arm/sem_acquire
Introduce Semaphore::acquire methods
2 parents cc49181 + 2fbbd9d commit 9cc1caa

File tree

55 files changed

+268
-178
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+268
-178
lines changed

TESTS/lorawan/loraradio/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void test_set_tx_config()
125125

126126
TEST_ASSERT_EQUAL(RF_TX_RUNNING, radio->get_status());
127127

128-
TEST_ASSERT_EQUAL(1, event_sem.wait(1000));
128+
TEST_ASSERT_TRUE(event_sem.try_acquire_for(1000));
129129
TEST_ASSERT_EQUAL(EV_TX_DONE, received_event);
130130
received_event = EV_NONE;
131131
}
@@ -145,7 +145,7 @@ void test_set_rx_config()
145145

146146
TEST_ASSERT_EQUAL(RF_RX_RUNNING, radio->get_status());
147147

148-
TEST_ASSERT_EQUAL(1, event_sem.wait(1000));
148+
TEST_ASSERT_TRUE(event_sem.try_acquire_for(1000));
149149

150150
// Nobody was sending to us so timeout is expected.
151151
TEST_ASSERT_EQUAL(EV_RX_TIMEOUT, received_event);

TESTS/mbed_drivers/lp_ticker/main.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,20 @@ void test_multi_call_time(void)
141141
void test_detach(void)
142142
{
143143
LowPowerTicker ticker;
144-
int32_t ret;
144+
bool ret;
145145
const float ticker_time_s = 0.1f;
146146
const uint32_t wait_time_ms = 500;
147147
Semaphore sem(0, 1);
148148

149149
ticker.attach(callback(sem_release, &sem), ticker_time_s);
150150

151-
ret = sem.wait();
152-
TEST_ASSERT_TRUE(ret > 0);
151+
sem.acquire();
153152

154-
ret = sem.wait();
153+
sem.acquire();
155154
ticker.detach(); /* cancel */
156-
TEST_ASSERT_TRUE(ret > 0);
157155

158-
ret = sem.wait(wait_time_ms);
159-
TEST_ASSERT_EQUAL(0, ret);
156+
ret = sem.try_acquire_for(wait_time_ms);
157+
TEST_ASSERT_FALSE(ret);
160158
}
161159

162160
/** Test single callback time via attach

TESTS/mbed_drivers/ticker/main.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,22 +257,20 @@ void test_multi_call_time(void)
257257
void test_detach(void)
258258
{
259259
Ticker ticker;
260-
int32_t ret;
260+
bool ret;
261261
const float ticker_time_s = 0.1f;
262262
const uint32_t wait_time_ms = 500;
263263
Semaphore sem(0, 1);
264264

265265
ticker.attach(callback(sem_release, &sem), ticker_time_s);
266266

267-
ret = sem.wait();
268-
TEST_ASSERT_TRUE(ret > 0);
267+
sem.acquire();
269268

270-
ret = sem.wait();
269+
sem.acquire();
271270
ticker.detach(); /* cancel */
272-
TEST_ASSERT_TRUE(ret > 0);
273271

274-
ret = sem.wait(wait_time_ms);
275-
TEST_ASSERT_EQUAL(0, ret);
272+
ret = sem.try_acquire_for(wait_time_ms);
273+
TEST_ASSERT_FALSE(ret);
276274
}
277275

278276
/** Test single callback time via attach

TESTS/mbed_drivers/timeout/timeout_tests.h

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ void test_single_call(void)
7878

7979
timeout.attach_callback(mbed::callback(sem_callback, &sem), TEST_DELAY_US);
8080

81-
int32_t sem_slots = sem.wait(0);
82-
TEST_ASSERT_EQUAL(0, sem_slots);
81+
bool acquired = sem.try_acquire();
82+
TEST_ASSERT_FALSE(acquired);
8383

84-
sem_slots = sem.wait(TEST_DELAY_MS + 2);
85-
TEST_ASSERT_EQUAL(1, sem_slots);
84+
acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
85+
TEST_ASSERT_TRUE(acquired);
8686

87-
sem_slots = sem.wait(TEST_DELAY_MS + 2);
88-
TEST_ASSERT_EQUAL(0, sem_slots);
87+
acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
88+
TEST_ASSERT_FALSE(acquired);
8989

9090
timeout.detach();
9191
}
@@ -110,12 +110,12 @@ void test_cancel(void)
110110

111111
timeout.attach_callback(mbed::callback(sem_callback, &sem), 2.0f * TEST_DELAY_US);
112112

113-
int32_t sem_slots = sem.wait(TEST_DELAY_MS);
114-
TEST_ASSERT_EQUAL(0, sem_slots);
113+
bool acquired = sem.try_acquire_for(TEST_DELAY_MS);
114+
TEST_ASSERT_FALSE(acquired);
115115
timeout.detach();
116116

117-
sem_slots = sem.wait(TEST_DELAY_MS + 2);
118-
TEST_ASSERT_EQUAL(0, sem_slots);
117+
acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
118+
TEST_ASSERT_FALSE(acquired);
119119
}
120120

121121
/** Template for tests: callback override
@@ -143,14 +143,14 @@ void test_override(void)
143143

144144
timeout.attach_callback(mbed::callback(sem_callback, &sem1), 2.0f * TEST_DELAY_US);
145145

146-
int32_t sem_slots = sem1.wait(TEST_DELAY_MS);
147-
TEST_ASSERT_EQUAL(0, sem_slots);
146+
bool acquired = sem1.try_acquire_for(TEST_DELAY_MS);
147+
TEST_ASSERT_FALSE(acquired);
148148
timeout.attach_callback(mbed::callback(sem_callback, &sem2), 2.0f * TEST_DELAY_US);
149149

150-
sem_slots = sem2.wait(2 * TEST_DELAY_MS + 2);
151-
TEST_ASSERT_EQUAL(1, sem_slots);
152-
sem_slots = sem1.wait(0);
153-
TEST_ASSERT_EQUAL(0, sem_slots);
150+
acquired = sem2.try_acquire_for(2 * TEST_DELAY_MS + 2);
151+
TEST_ASSERT_TRUE(acquired);
152+
acquired = sem1.try_acquire();
153+
TEST_ASSERT_FALSE(acquired);
154154

155155
timeout.detach();
156156
}
@@ -200,8 +200,8 @@ void test_no_wait(void)
200200
T timeout;
201201
timeout.attach_callback(mbed::callback(sem_callback, &sem), 0ULL);
202202

203-
int32_t sem_slots = sem.wait(0);
204-
TEST_ASSERT_EQUAL(1, sem_slots);
203+
bool acquired = sem.try_acquire();
204+
TEST_ASSERT_TRUE(acquired);
205205
timeout.detach();
206206
}
207207

@@ -227,9 +227,8 @@ void test_delay_accuracy(void)
227227
timer.start();
228228
timeout.attach_callback(mbed::callback(sem_callback, &sem), delay_us);
229229

230-
int32_t sem_slots = sem.wait(osWaitForever);
230+
sem.acquire();
231231
timer.stop();
232-
TEST_ASSERT_EQUAL(1, sem_slots);
233232
TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.read_high_resolution_us());
234233

235234
timeout.detach();
@@ -265,7 +264,7 @@ void test_sleep(void)
265264

266265
bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check();
267266
TEST_ASSERT_FALSE_MESSAGE(deep_sleep_allowed, "Deep sleep should be disallowed");
268-
while (sem.wait(0) != 1) {
267+
while (!sem.try_acquire()) {
269268
sleep();
270269
}
271270
timer.stop();
@@ -324,7 +323,7 @@ void test_deepsleep(void)
324323

325324
bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check();
326325
TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed, "Deep sleep should be allowed");
327-
while (sem.wait(0) != 1) {
326+
while (!sem.try_acquire()) {
328327
sleep();
329328
}
330329
timer.stop();

TESTS/mbed_drivers/timerevent/main.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ class TestTimerEvent: public TimerEvent {
6565
using TimerEvent::insert_absolute;
6666
using TimerEvent::remove;
6767

68-
int32_t sem_wait(uint32_t millisec)
68+
bool sem_try_acquire(uint32_t millisec)
6969
{
70-
return sem.wait(millisec);
70+
return sem.try_acquire_for(millisec);
7171
}
7272
};
7373

7474
class TestTimerEventRelative: public TestTimerEvent {
7575
public:
76-
static const int32_t SEM_SLOTS_AFTER_PAST_TS_INSERTED = 0;
76+
static const bool SEM_ACQUIRED_AFTER_PAST_TS_INSERTED = false;
7777
TestTimerEventRelative() :
7878
TestTimerEvent()
7979
{
@@ -98,7 +98,7 @@ class TestTimerEventRelative: public TestTimerEvent {
9898

9999
class TestTimerEventAbsolute: public TestTimerEvent {
100100
public:
101-
static const int32_t SEM_SLOTS_AFTER_PAST_TS_INSERTED = 1;
101+
static const bool SEM_ACQUIRED_AFTER_PAST_TS_INSERTED = true;
102102
TestTimerEventAbsolute() :
103103
TestTimerEvent()
104104
{
@@ -141,11 +141,11 @@ void test_insert(void)
141141
T tte;
142142

143143
tte.set_future_timestamp(TEST_DELAY_US);
144-
int32_t sem_slots = tte.sem_wait(0);
145-
TEST_ASSERT_EQUAL(0, sem_slots);
144+
bool acquired = tte.sem_try_acquire(0);
145+
TEST_ASSERT_FALSE(acquired);
146146

147-
sem_slots = tte.sem_wait(TEST_DELAY_US / 1000 + DELTA);
148-
TEST_ASSERT_EQUAL(1, sem_slots);
147+
acquired = tte.sem_try_acquire(TEST_DELAY_US / 1000 + DELTA);
148+
TEST_ASSERT_TRUE(acquired);
149149

150150
tte.remove();
151151
}
@@ -170,12 +170,12 @@ void test_remove(void)
170170
T tte;
171171

172172
tte.set_future_timestamp(TEST_DELAY_US * 2);
173-
int32_t sem_slots = tte.sem_wait(TEST_DELAY_US / 1000);
174-
TEST_ASSERT_EQUAL(0, sem_slots);
173+
bool acquired = tte.sem_try_acquire(TEST_DELAY_US / 1000);
174+
TEST_ASSERT_FALSE(acquired);
175175
tte.remove();
176176

177-
sem_slots = tte.sem_wait(TEST_DELAY_US * 2 / 1000 + DELTA);
178-
TEST_ASSERT_EQUAL(0, sem_slots);
177+
acquired = tte.sem_try_acquire(TEST_DELAY_US * 2 / 1000 + DELTA);
178+
TEST_ASSERT_FALSE(acquired);
179179
}
180180

181181
/** Test insert_absolute zero
@@ -188,8 +188,8 @@ void test_insert_zero(void)
188188
TestTimerEvent tte;
189189

190190
tte.insert_absolute(0ULL);
191-
int32_t sem_slots = tte.sem_wait(0);
192-
TEST_ASSERT_EQUAL(1, sem_slots);
191+
bool acquired = tte.sem_try_acquire(0);
192+
TEST_ASSERT_TRUE(acquired);
193193

194194
tte.remove();
195195
}
@@ -215,8 +215,8 @@ void test_insert_past(void)
215215
T tte;
216216

217217
tte.set_past_timestamp();
218-
int32_t sem_slots = tte.sem_wait(0);
219-
TEST_ASSERT_EQUAL(tte.SEM_SLOTS_AFTER_PAST_TS_INSERTED, sem_slots);
218+
bool acquired = tte.sem_try_acquire(0);
219+
TEST_ASSERT_EQUAL(tte.SEM_ACQUIRED_AFTER_PAST_TS_INSERTED, acquired);
220220

221221
tte.remove();
222222
}

TESTS/mbed_platform/error_handling/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ void test_error_hook()
261261
}
262262

263263
MBED_WARNING1(MBED_ERROR_INVALID_ARGUMENT, "Test for error hook", 1234);
264-
int32_t sem_status = callback_sem.wait(5000);
264+
bool acquired = callback_sem.try_acquire_for(5000);
265265

266-
TEST_ASSERT(sem_status > 0);
266+
TEST_ASSERT(acquired);
267267
}
268268

269269
#if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED && defined(MBED_TEST_SIM_BLOCKDEVICE)

TESTS/mbedmicro-rtos-mbed/event_flags/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void send_thread_sync(EventFlags *ef)
6666
for (uint32_t i = 0; i <= MAX_FLAG_POS; i++) {
6767
const uint32_t flag = flags & (1 << i);
6868
if (flag) {
69-
sync_sem.wait();
69+
sync_sem.acquire();
7070
ef->set(flag);
7171
ThisThread::sleep_for(wait_ms);
7272
}

TESTS/mbedmicro-rtos-mbed/rtostimer/main.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Stopwatch: public Timer {
5050

5151
void start(void)
5252
{
53-
_sem.wait(0);
53+
_sem.try_acquire();
5454
Timer::start();
5555
}
5656

@@ -68,7 +68,7 @@ class Stopwatch: public Timer {
6868
if (!running) {
6969
return 1;
7070
}
71-
return _sem.wait(millisec);
71+
return _sem.try_acquire_for(millisec);
7272
}
7373
};
7474

@@ -169,8 +169,8 @@ void test_start_again()
169169
osStatus status = rtostimer.start(DELAY_MS);
170170
TEST_ASSERT_EQUAL(osOK, status);
171171

172-
int32_t slots = sem.wait(DELAY_MS + DELTA_MS);
173-
TEST_ASSERT_EQUAL(1, slots);
172+
bool acquired = sem.try_acquire_for(DELAY_MS + DELTA_MS);
173+
TEST_ASSERT(acquired);
174174

175175
#if !MBED_TRAP_ERRORS_ENABLED
176176
status = rtostimer.stop();
@@ -180,8 +180,8 @@ void test_start_again()
180180
status = rtostimer.start(DELAY_MS);
181181
TEST_ASSERT_EQUAL(osOK, status);
182182

183-
slots = sem.wait(DELAY_MS + DELTA_MS);
184-
TEST_ASSERT_EQUAL(1, slots);
183+
acquired = sem.try_acquire_for(DELAY_MS + DELTA_MS);
184+
TEST_ASSERT(acquired);
185185

186186
#if !MBED_TRAP_ERRORS_ENABLED
187187
status = rtostimer.stop();
@@ -255,14 +255,14 @@ void test_stop()
255255
osStatus status = rtostimer.start(DELAY_MS);
256256
TEST_ASSERT_EQUAL(osOK, status);
257257

258-
int32_t slots = sem.wait(RESTART_DELAY_MS);
259-
TEST_ASSERT_EQUAL(0, slots);
258+
bool acquired = sem.try_acquire_for(RESTART_DELAY_MS);
259+
TEST_ASSERT_FALSE(acquired);
260260

261261
status = rtostimer.stop();
262262
TEST_ASSERT_EQUAL(osOK, status);
263263

264-
slots = sem.wait(DELAY_MS + DELTA_MS);
265-
TEST_ASSERT_EQUAL(0, slots);
264+
acquired = sem.try_acquire_for(DELAY_MS + DELTA_MS);
265+
TEST_ASSERT_FALSE(acquired);
266266

267267
#if !MBED_TRAP_ERRORS_ENABLED
268268
status = rtostimer.stop();

TESTS/mbedmicro-rtos-mbed/semaphore/main.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void test_thread(int const *delay)
4646
{
4747
const int thread_delay = *delay;
4848
while (true) {
49-
two_slots.wait();
49+
two_slots.acquire();
5050
sem_counter++;
5151
const bool sem_lock_failed = sem_counter > SEMAPHORE_SLOTS;
5252
if (sem_lock_failed) {
@@ -96,8 +96,7 @@ struct thread_data {
9696

9797
void single_thread(struct thread_data *data)
9898
{
99-
int32_t cnt = data->sem->wait();
100-
TEST_ASSERT_EQUAL(1, cnt);
99+
data->sem->acquire();
101100
data->data++;
102101
}
103102

@@ -140,8 +139,8 @@ void test_single_thread()
140139

141140
void timeout_thread(Semaphore *sem)
142141
{
143-
int32_t cnt = sem->wait(30);
144-
TEST_ASSERT_EQUAL(0, cnt);
142+
bool acquired = sem->try_acquire_for(30);
143+
TEST_ASSERT_FALSE(acquired);
145144
}
146145

147146
/** Test timeout
@@ -188,8 +187,8 @@ void test_no_timeout()
188187
Timer timer;
189188
timer.start();
190189

191-
int32_t cnt = sem.wait(0);
192-
TEST_ASSERT_EQUAL(T, cnt);
190+
bool acquired = sem.try_acquire();
191+
TEST_ASSERT_EQUAL(T > 0, acquired);
193192

194193
TEST_ASSERT_UINT32_WITHIN(5000, 0, timer.read_us());
195194
}
@@ -205,8 +204,8 @@ void test_multiple_tokens_wait()
205204
Semaphore sem(5);
206205

207206
for (int i = 5; i >= 0; i--) {
208-
int32_t cnt = sem.wait(0);
209-
TEST_ASSERT_EQUAL(i, cnt);
207+
bool acquired = sem.try_acquire();
208+
TEST_ASSERT_EQUAL(i > 0, acquired);
210209
}
211210
}
212211

0 commit comments

Comments
 (0)