Introduce Semaphore::acquire methods

Deprecate wait() in favour of acquire(), try_acquire(),
try_acquire_for() and try_acquire_until().

Brings Semaphore more into line with CMSIS-RTOS 2 (which uses "acquire"),
itself (as it has "release"), and other classes having "try", "try for"
and "try until".

Also steps away from vague "wait" term - the primary operation here is
to acquire the semaphore, and this will of course sleep.
pull/10596/head
Kevin Bracey 2019-03-18 12:21:51 +02:00
parent 51b835b03b
commit 2fbbd9d2ca
55 changed files with 268 additions and 178 deletions

View File

@ -125,7 +125,7 @@ void test_set_tx_config()
TEST_ASSERT_EQUAL(RF_TX_RUNNING, radio->get_status()); TEST_ASSERT_EQUAL(RF_TX_RUNNING, radio->get_status());
TEST_ASSERT_EQUAL(1, event_sem.wait(1000)); TEST_ASSERT_TRUE(event_sem.try_acquire_for(1000));
TEST_ASSERT_EQUAL(EV_TX_DONE, received_event); TEST_ASSERT_EQUAL(EV_TX_DONE, received_event);
received_event = EV_NONE; received_event = EV_NONE;
} }
@ -145,7 +145,7 @@ void test_set_rx_config()
TEST_ASSERT_EQUAL(RF_RX_RUNNING, radio->get_status()); TEST_ASSERT_EQUAL(RF_RX_RUNNING, radio->get_status());
TEST_ASSERT_EQUAL(1, event_sem.wait(1000)); TEST_ASSERT_TRUE(event_sem.try_acquire_for(1000));
// Nobody was sending to us so timeout is expected. // Nobody was sending to us so timeout is expected.
TEST_ASSERT_EQUAL(EV_RX_TIMEOUT, received_event); TEST_ASSERT_EQUAL(EV_RX_TIMEOUT, received_event);

View File

@ -141,22 +141,20 @@ void test_multi_call_time(void)
void test_detach(void) void test_detach(void)
{ {
LowPowerTicker ticker; LowPowerTicker ticker;
int32_t ret; bool ret;
const float ticker_time_s = 0.1f; const float ticker_time_s = 0.1f;
const uint32_t wait_time_ms = 500; const uint32_t wait_time_ms = 500;
Semaphore sem(0, 1); Semaphore sem(0, 1);
ticker.attach(callback(sem_release, &sem), ticker_time_s); ticker.attach(callback(sem_release, &sem), ticker_time_s);
ret = sem.wait(); sem.acquire();
TEST_ASSERT_TRUE(ret > 0);
ret = sem.wait(); sem.acquire();
ticker.detach(); /* cancel */ ticker.detach(); /* cancel */
TEST_ASSERT_TRUE(ret > 0);
ret = sem.wait(wait_time_ms); ret = sem.try_acquire_for(wait_time_ms);
TEST_ASSERT_EQUAL(0, ret); TEST_ASSERT_FALSE(ret);
} }
/** Test single callback time via attach /** Test single callback time via attach

View File

@ -257,22 +257,20 @@ void test_multi_call_time(void)
void test_detach(void) void test_detach(void)
{ {
Ticker ticker; Ticker ticker;
int32_t ret; bool ret;
const float ticker_time_s = 0.1f; const float ticker_time_s = 0.1f;
const uint32_t wait_time_ms = 500; const uint32_t wait_time_ms = 500;
Semaphore sem(0, 1); Semaphore sem(0, 1);
ticker.attach(callback(sem_release, &sem), ticker_time_s); ticker.attach(callback(sem_release, &sem), ticker_time_s);
ret = sem.wait(); sem.acquire();
TEST_ASSERT_TRUE(ret > 0);
ret = sem.wait(); sem.acquire();
ticker.detach(); /* cancel */ ticker.detach(); /* cancel */
TEST_ASSERT_TRUE(ret > 0);
ret = sem.wait(wait_time_ms); ret = sem.try_acquire_for(wait_time_ms);
TEST_ASSERT_EQUAL(0, ret); TEST_ASSERT_FALSE(ret);
} }
/** Test single callback time via attach /** Test single callback time via attach

View File

@ -78,14 +78,14 @@ void test_single_call(void)
timeout.attach_callback(mbed::callback(sem_callback, &sem), TEST_DELAY_US); timeout.attach_callback(mbed::callback(sem_callback, &sem), TEST_DELAY_US);
int32_t sem_slots = sem.wait(0); bool acquired = sem.try_acquire();
TEST_ASSERT_EQUAL(0, sem_slots); TEST_ASSERT_FALSE(acquired);
sem_slots = sem.wait(TEST_DELAY_MS + 2); acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
TEST_ASSERT_EQUAL(1, sem_slots); TEST_ASSERT_TRUE(acquired);
sem_slots = sem.wait(TEST_DELAY_MS + 2); acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
TEST_ASSERT_EQUAL(0, sem_slots); TEST_ASSERT_FALSE(acquired);
timeout.detach(); timeout.detach();
} }
@ -110,12 +110,12 @@ void test_cancel(void)
timeout.attach_callback(mbed::callback(sem_callback, &sem), 2.0f * TEST_DELAY_US); timeout.attach_callback(mbed::callback(sem_callback, &sem), 2.0f * TEST_DELAY_US);
int32_t sem_slots = sem.wait(TEST_DELAY_MS); bool acquired = sem.try_acquire_for(TEST_DELAY_MS);
TEST_ASSERT_EQUAL(0, sem_slots); TEST_ASSERT_FALSE(acquired);
timeout.detach(); timeout.detach();
sem_slots = sem.wait(TEST_DELAY_MS + 2); acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
TEST_ASSERT_EQUAL(0, sem_slots); TEST_ASSERT_FALSE(acquired);
} }
/** Template for tests: callback override /** Template for tests: callback override
@ -143,14 +143,14 @@ void test_override(void)
timeout.attach_callback(mbed::callback(sem_callback, &sem1), 2.0f * TEST_DELAY_US); timeout.attach_callback(mbed::callback(sem_callback, &sem1), 2.0f * TEST_DELAY_US);
int32_t sem_slots = sem1.wait(TEST_DELAY_MS); bool acquired = sem1.try_acquire_for(TEST_DELAY_MS);
TEST_ASSERT_EQUAL(0, sem_slots); TEST_ASSERT_FALSE(acquired);
timeout.attach_callback(mbed::callback(sem_callback, &sem2), 2.0f * TEST_DELAY_US); timeout.attach_callback(mbed::callback(sem_callback, &sem2), 2.0f * TEST_DELAY_US);
sem_slots = sem2.wait(2 * TEST_DELAY_MS + 2); acquired = sem2.try_acquire_for(2 * TEST_DELAY_MS + 2);
TEST_ASSERT_EQUAL(1, sem_slots); TEST_ASSERT_TRUE(acquired);
sem_slots = sem1.wait(0); acquired = sem1.try_acquire();
TEST_ASSERT_EQUAL(0, sem_slots); TEST_ASSERT_FALSE(acquired);
timeout.detach(); timeout.detach();
} }
@ -200,8 +200,8 @@ void test_no_wait(void)
T timeout; T timeout;
timeout.attach_callback(mbed::callback(sem_callback, &sem), 0ULL); timeout.attach_callback(mbed::callback(sem_callback, &sem), 0ULL);
int32_t sem_slots = sem.wait(0); bool acquired = sem.try_acquire();
TEST_ASSERT_EQUAL(1, sem_slots); TEST_ASSERT_TRUE(acquired);
timeout.detach(); timeout.detach();
} }
@ -227,9 +227,8 @@ void test_delay_accuracy(void)
timer.start(); timer.start();
timeout.attach_callback(mbed::callback(sem_callback, &sem), delay_us); timeout.attach_callback(mbed::callback(sem_callback, &sem), delay_us);
int32_t sem_slots = sem.wait(osWaitForever); sem.acquire();
timer.stop(); timer.stop();
TEST_ASSERT_EQUAL(1, sem_slots);
TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.read_high_resolution_us()); TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.read_high_resolution_us());
timeout.detach(); timeout.detach();
@ -265,7 +264,7 @@ void test_sleep(void)
bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check(); bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check();
TEST_ASSERT_FALSE_MESSAGE(deep_sleep_allowed, "Deep sleep should be disallowed"); TEST_ASSERT_FALSE_MESSAGE(deep_sleep_allowed, "Deep sleep should be disallowed");
while (sem.wait(0) != 1) { while (!sem.try_acquire()) {
sleep(); sleep();
} }
timer.stop(); timer.stop();
@ -324,7 +323,7 @@ void test_deepsleep(void)
bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check(); bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check();
TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed, "Deep sleep should be allowed"); TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed, "Deep sleep should be allowed");
while (sem.wait(0) != 1) { while (!sem.try_acquire()) {
sleep(); sleep();
} }
timer.stop(); timer.stop();

View File

@ -65,15 +65,15 @@ public:
using TimerEvent::insert_absolute; using TimerEvent::insert_absolute;
using TimerEvent::remove; using TimerEvent::remove;
int32_t sem_wait(uint32_t millisec) bool sem_try_acquire(uint32_t millisec)
{ {
return sem.wait(millisec); return sem.try_acquire_for(millisec);
} }
}; };
class TestTimerEventRelative: public TestTimerEvent { class TestTimerEventRelative: public TestTimerEvent {
public: public:
static const int32_t SEM_SLOTS_AFTER_PAST_TS_INSERTED = 0; static const bool SEM_ACQUIRED_AFTER_PAST_TS_INSERTED = false;
TestTimerEventRelative() : TestTimerEventRelative() :
TestTimerEvent() TestTimerEvent()
{ {
@ -98,7 +98,7 @@ public:
class TestTimerEventAbsolute: public TestTimerEvent { class TestTimerEventAbsolute: public TestTimerEvent {
public: public:
static const int32_t SEM_SLOTS_AFTER_PAST_TS_INSERTED = 1; static const bool SEM_ACQUIRED_AFTER_PAST_TS_INSERTED = true;
TestTimerEventAbsolute() : TestTimerEventAbsolute() :
TestTimerEvent() TestTimerEvent()
{ {
@ -141,11 +141,11 @@ void test_insert(void)
T tte; T tte;
tte.set_future_timestamp(TEST_DELAY_US); tte.set_future_timestamp(TEST_DELAY_US);
int32_t sem_slots = tte.sem_wait(0); bool acquired = tte.sem_try_acquire(0);
TEST_ASSERT_EQUAL(0, sem_slots); TEST_ASSERT_FALSE(acquired);
sem_slots = tte.sem_wait(TEST_DELAY_US / 1000 + DELTA); acquired = tte.sem_try_acquire(TEST_DELAY_US / 1000 + DELTA);
TEST_ASSERT_EQUAL(1, sem_slots); TEST_ASSERT_TRUE(acquired);
tte.remove(); tte.remove();
} }
@ -170,12 +170,12 @@ void test_remove(void)
T tte; T tte;
tte.set_future_timestamp(TEST_DELAY_US * 2); tte.set_future_timestamp(TEST_DELAY_US * 2);
int32_t sem_slots = tte.sem_wait(TEST_DELAY_US / 1000); bool acquired = tte.sem_try_acquire(TEST_DELAY_US / 1000);
TEST_ASSERT_EQUAL(0, sem_slots); TEST_ASSERT_FALSE(acquired);
tte.remove(); tte.remove();
sem_slots = tte.sem_wait(TEST_DELAY_US * 2 / 1000 + DELTA); acquired = tte.sem_try_acquire(TEST_DELAY_US * 2 / 1000 + DELTA);
TEST_ASSERT_EQUAL(0, sem_slots); TEST_ASSERT_FALSE(acquired);
} }
/** Test insert_absolute zero /** Test insert_absolute zero
@ -188,8 +188,8 @@ void test_insert_zero(void)
TestTimerEvent tte; TestTimerEvent tte;
tte.insert_absolute(0ULL); tte.insert_absolute(0ULL);
int32_t sem_slots = tte.sem_wait(0); bool acquired = tte.sem_try_acquire(0);
TEST_ASSERT_EQUAL(1, sem_slots); TEST_ASSERT_TRUE(acquired);
tte.remove(); tte.remove();
} }
@ -215,8 +215,8 @@ void test_insert_past(void)
T tte; T tte;
tte.set_past_timestamp(); tte.set_past_timestamp();
int32_t sem_slots = tte.sem_wait(0); bool acquired = tte.sem_try_acquire(0);
TEST_ASSERT_EQUAL(tte.SEM_SLOTS_AFTER_PAST_TS_INSERTED, sem_slots); TEST_ASSERT_EQUAL(tte.SEM_ACQUIRED_AFTER_PAST_TS_INSERTED, acquired);
tte.remove(); tte.remove();
} }

View File

@ -261,9 +261,9 @@ void test_error_hook()
} }
MBED_WARNING1(MBED_ERROR_INVALID_ARGUMENT, "Test for error hook", 1234); MBED_WARNING1(MBED_ERROR_INVALID_ARGUMENT, "Test for error hook", 1234);
int32_t sem_status = callback_sem.wait(5000); bool acquired = callback_sem.try_acquire_for(5000);
TEST_ASSERT(sem_status > 0); TEST_ASSERT(acquired);
} }
#if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED && defined(MBED_TEST_SIM_BLOCKDEVICE) #if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED && defined(MBED_TEST_SIM_BLOCKDEVICE)

View File

@ -66,7 +66,7 @@ void send_thread_sync(EventFlags *ef)
for (uint32_t i = 0; i <= MAX_FLAG_POS; i++) { for (uint32_t i = 0; i <= MAX_FLAG_POS; i++) {
const uint32_t flag = flags & (1 << i); const uint32_t flag = flags & (1 << i);
if (flag) { if (flag) {
sync_sem.wait(); sync_sem.acquire();
ef->set(flag); ef->set(flag);
ThisThread::sleep_for(wait_ms); ThisThread::sleep_for(wait_ms);
} }

View File

@ -50,7 +50,7 @@ public:
void start(void) void start(void)
{ {
_sem.wait(0); _sem.try_acquire();
Timer::start(); Timer::start();
} }
@ -68,7 +68,7 @@ public:
if (!running) { if (!running) {
return 1; return 1;
} }
return _sem.wait(millisec); return _sem.try_acquire_for(millisec);
} }
}; };
@ -169,8 +169,8 @@ void test_start_again()
osStatus status = rtostimer.start(DELAY_MS); osStatus status = rtostimer.start(DELAY_MS);
TEST_ASSERT_EQUAL(osOK, status); TEST_ASSERT_EQUAL(osOK, status);
int32_t slots = sem.wait(DELAY_MS + DELTA_MS); bool acquired = sem.try_acquire_for(DELAY_MS + DELTA_MS);
TEST_ASSERT_EQUAL(1, slots); TEST_ASSERT(acquired);
#if !MBED_TRAP_ERRORS_ENABLED #if !MBED_TRAP_ERRORS_ENABLED
status = rtostimer.stop(); status = rtostimer.stop();
@ -180,8 +180,8 @@ void test_start_again()
status = rtostimer.start(DELAY_MS); status = rtostimer.start(DELAY_MS);
TEST_ASSERT_EQUAL(osOK, status); TEST_ASSERT_EQUAL(osOK, status);
slots = sem.wait(DELAY_MS + DELTA_MS); acquired = sem.try_acquire_for(DELAY_MS + DELTA_MS);
TEST_ASSERT_EQUAL(1, slots); TEST_ASSERT(acquired);
#if !MBED_TRAP_ERRORS_ENABLED #if !MBED_TRAP_ERRORS_ENABLED
status = rtostimer.stop(); status = rtostimer.stop();
@ -255,14 +255,14 @@ void test_stop()
osStatus status = rtostimer.start(DELAY_MS); osStatus status = rtostimer.start(DELAY_MS);
TEST_ASSERT_EQUAL(osOK, status); TEST_ASSERT_EQUAL(osOK, status);
int32_t slots = sem.wait(RESTART_DELAY_MS); bool acquired = sem.try_acquire_for(RESTART_DELAY_MS);
TEST_ASSERT_EQUAL(0, slots); TEST_ASSERT_FALSE(acquired);
status = rtostimer.stop(); status = rtostimer.stop();
TEST_ASSERT_EQUAL(osOK, status); TEST_ASSERT_EQUAL(osOK, status);
slots = sem.wait(DELAY_MS + DELTA_MS); acquired = sem.try_acquire_for(DELAY_MS + DELTA_MS);
TEST_ASSERT_EQUAL(0, slots); TEST_ASSERT_FALSE(acquired);
#if !MBED_TRAP_ERRORS_ENABLED #if !MBED_TRAP_ERRORS_ENABLED
status = rtostimer.stop(); status = rtostimer.stop();

View File

@ -46,7 +46,7 @@ void test_thread(int const *delay)
{ {
const int thread_delay = *delay; const int thread_delay = *delay;
while (true) { while (true) {
two_slots.wait(); two_slots.acquire();
sem_counter++; sem_counter++;
const bool sem_lock_failed = sem_counter > SEMAPHORE_SLOTS; const bool sem_lock_failed = sem_counter > SEMAPHORE_SLOTS;
if (sem_lock_failed) { if (sem_lock_failed) {
@ -96,8 +96,7 @@ struct thread_data {
void single_thread(struct thread_data *data) void single_thread(struct thread_data *data)
{ {
int32_t cnt = data->sem->wait(); data->sem->acquire();
TEST_ASSERT_EQUAL(1, cnt);
data->data++; data->data++;
} }
@ -140,8 +139,8 @@ void test_single_thread()
void timeout_thread(Semaphore *sem) void timeout_thread(Semaphore *sem)
{ {
int32_t cnt = sem->wait(30); bool acquired = sem->try_acquire_for(30);
TEST_ASSERT_EQUAL(0, cnt); TEST_ASSERT_FALSE(acquired);
} }
/** Test timeout /** Test timeout
@ -188,8 +187,8 @@ void test_no_timeout()
Timer timer; Timer timer;
timer.start(); timer.start();
int32_t cnt = sem.wait(0); bool acquired = sem.try_acquire();
TEST_ASSERT_EQUAL(T, cnt); TEST_ASSERT_EQUAL(T > 0, acquired);
TEST_ASSERT_UINT32_WITHIN(5000, 0, timer.read_us()); TEST_ASSERT_UINT32_WITHIN(5000, 0, timer.read_us());
} }
@ -205,8 +204,8 @@ void test_multiple_tokens_wait()
Semaphore sem(5); Semaphore sem(5);
for (int i = 5; i >= 0; i--) { for (int i = 5; i >= 0; i--) {
int32_t cnt = sem.wait(0); bool acquired = sem.try_acquire();
TEST_ASSERT_EQUAL(i, cnt); TEST_ASSERT_EQUAL(i > 0, acquired);
} }
} }

View File

@ -65,7 +65,7 @@ template <int32_t signals, uint32_t timeout, int32_t test_val>
void run_release_wait_signal_wait(Sync *sync) void run_release_wait_signal_wait(Sync *sync)
{ {
sync->sem_parent.release(); sync->sem_parent.release();
sync->sem_child.wait(); sync->sem_child.acquire();
osEvent ev = Thread::signal_wait(signals, timeout); osEvent ev = Thread::signal_wait(signals, timeout);
TEST_ASSERT_EQUAL(test_val, ev.status); TEST_ASSERT_EQUAL(test_val, ev.status);
} }
@ -81,7 +81,7 @@ template <int32_t signals, int32_t test_val>
void run_wait_clear(Sync *sync) void run_wait_clear(Sync *sync)
{ {
sync->sem_parent.release(); sync->sem_parent.release();
sync->sem_child.wait(); sync->sem_child.acquire();
int32_t ret = ThisThread::flags_clear(signals); int32_t ret = ThisThread::flags_clear(signals);
TEST_ASSERT_EQUAL(test_val, ret); TEST_ASSERT_EQUAL(test_val, ret);
} }
@ -92,7 +92,7 @@ void run_double_wait_clear(Sync *sync)
int32_t ret; int32_t ret;
sync->sem_parent.release(); sync->sem_parent.release();
sync->sem_child.wait(); sync->sem_child.acquire();
ret = ThisThread::flags_clear(signals1); ret = ThisThread::flags_clear(signals1);
TEST_ASSERT_EQUAL(test_val1, ret); TEST_ASSERT_EQUAL(test_val1, ret);
@ -106,7 +106,7 @@ void run_loop_wait_clear(Sync *sync)
for (int i = 0; i <= MAX_FLAG_POS; i++) { for (int i = 0; i <= MAX_FLAG_POS; i++) {
int32_t signal = 1 << i; int32_t signal = 1 << i;
signals |= signal; signals |= signal;
sync->sem_child.wait(); sync->sem_child.acquire();
int32_t ret = ThisThread::flags_clear(NO_SIGNALS); int32_t ret = ThisThread::flags_clear(NO_SIGNALS);
TEST_ASSERT_EQUAL(signals, ret); TEST_ASSERT_EQUAL(signals, ret);
sync->sem_parent.release(); sync->sem_parent.release();
@ -128,7 +128,7 @@ void test_clear_no_signals(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE); Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_double_wait_clear<NO_SIGNALS, NO_SIGNALS, ALL_SIGNALS, ALL_SIGNALS>, &sync)); t.start(callback(run_double_wait_clear<NO_SIGNALS, NO_SIGNALS, ALL_SIGNALS, ALL_SIGNALS>, &sync));
sem_parent.wait(); sem_parent.acquire();
t.signal_set(ALL_SIGNALS); t.signal_set(ALL_SIGNALS);
sem_child.release(); sem_child.release();
t.join(); t.join();
@ -163,7 +163,7 @@ void test_set_all(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE); Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_wait_clear<NO_SIGNALS, ALL_SIGNALS>, &sync)); t.start(callback(run_wait_clear<NO_SIGNALS, ALL_SIGNALS>, &sync));
sem_parent.wait(); sem_parent.acquire();
ret = t.signal_set(ALL_SIGNALS); ret = t.signal_set(ALL_SIGNALS);
TEST_ASSERT_EQUAL(ALL_SIGNALS, ret); TEST_ASSERT_EQUAL(ALL_SIGNALS, ret);
@ -190,7 +190,7 @@ void test_set_prohibited(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE); Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_wait_clear<NO_SIGNALS, ALL_SIGNALS>, &sync)); t.start(callback(run_wait_clear<NO_SIGNALS, ALL_SIGNALS>, &sync));
sem_parent.wait(); sem_parent.acquire();
t.signal_set(ALL_SIGNALS); t.signal_set(ALL_SIGNALS);
#if !MBED_TRAP_ERRORS_ENABLED #if !MBED_TRAP_ERRORS_ENABLED
@ -216,7 +216,7 @@ void test_clear_all(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE); Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_double_wait_clear<ALL_SIGNALS, NO_SIGNALS, ALL_SIGNALS, NO_SIGNALS>, &sync)); t.start(callback(run_double_wait_clear<ALL_SIGNALS, NO_SIGNALS, ALL_SIGNALS, NO_SIGNALS>, &sync));
sem_parent.wait(); sem_parent.acquire();
t.signal_set(ALL_SIGNALS); t.signal_set(ALL_SIGNALS);
sem_child.release(); sem_child.release();
t.join(); t.join();
@ -245,7 +245,7 @@ void test_set_all_loop(void)
signals |= signal; signals |= signal;
TEST_ASSERT_EQUAL(signals, ret); TEST_ASSERT_EQUAL(signals, ret);
sem_child.release(); sem_child.release();
sem_parent.wait(); sem_parent.acquire();
} }
t.join(); t.join();
} }
@ -280,7 +280,7 @@ void test_wait_all_already_set(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE); Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_release_wait_signal_wait<ALL_SIGNALS, osWaitForever, osEventSignal>, &sync)); t.start(callback(run_release_wait_signal_wait<ALL_SIGNALS, osWaitForever, osEventSignal>, &sync));
sem_parent.wait(); sem_parent.acquire();
TEST_ASSERT_EQUAL(Thread::WaitingSemaphore, t.get_state()); TEST_ASSERT_EQUAL(Thread::WaitingSemaphore, t.get_state());
t.signal_set(ALL_SIGNALS); t.signal_set(ALL_SIGNALS);
sem_child.release(); sem_child.release();
@ -300,7 +300,7 @@ void test_wait_all(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE); Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_release_signal_wait<ALL_SIGNALS, osWaitForever, osEventSignal>, &sem)); t.start(callback(run_release_signal_wait<ALL_SIGNALS, osWaitForever, osEventSignal>, &sem));
sem.wait(); sem.acquire();
TEST_ASSERT_EQUAL(Thread::WaitingThreadFlag, t.get_state()); TEST_ASSERT_EQUAL(Thread::WaitingThreadFlag, t.get_state());
t.signal_set(ALL_SIGNALS); t.signal_set(ALL_SIGNALS);
@ -321,7 +321,7 @@ void test_wait_all_loop(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE); Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_release_signal_wait<ALL_SIGNALS, osWaitForever, osEventSignal>, &sem)); t.start(callback(run_release_signal_wait<ALL_SIGNALS, osWaitForever, osEventSignal>, &sem));
sem.wait(); sem.acquire();
TEST_ASSERT_EQUAL(Thread::WaitingThreadFlag, t.get_state()); TEST_ASSERT_EQUAL(Thread::WaitingThreadFlag, t.get_state());
for (int i = 0; i < MAX_FLAG_POS; i++) { for (int i = 0; i < MAX_FLAG_POS; i++) {
@ -348,7 +348,7 @@ void test_set_double(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE); Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_release_signal_wait < SIGNAL1 | SIGNAL2 | SIGNAL3, osWaitForever, osEventSignal >, &sem)); t.start(callback(run_release_signal_wait < SIGNAL1 | SIGNAL2 | SIGNAL3, osWaitForever, osEventSignal >, &sem));
sem.wait(); sem.acquire();
TEST_ASSERT_EQUAL(Thread::WaitingThreadFlag, t.get_state()); TEST_ASSERT_EQUAL(Thread::WaitingThreadFlag, t.get_state());
ret = t.signal_set(SIGNAL1); ret = t.signal_set(SIGNAL1);

View File

@ -62,9 +62,9 @@ public:
{ {
} }
int32_t sem_wait(uint32_t millisec) bool sem_try_acquire(uint32_t millisec)
{ {
return _sem.wait(millisec); return _sem.try_acquire_for(millisec);
} }
}; };
@ -204,8 +204,8 @@ void test_cancel_tick(void)
st.schedule_tick(TEST_TICKS); st.schedule_tick(TEST_TICKS);
st.cancel_tick(); st.cancel_tick();
int32_t sem_slots = st.sem_wait((DELAY_US + DELAY_DELTA_US) / 1000ULL); bool acquired = st.sem_try_acquire((DELAY_US + DELAY_DELTA_US) / 1000ULL);
TEST_ASSERT_EQUAL_INT32(0, sem_slots); TEST_ASSERT_FALSE(acquired);
TEST_ASSERT_EQUAL_UINT32(0, st.get_tick()); TEST_ASSERT_EQUAL_UINT32(0, st.get_tick());
} }
@ -220,8 +220,8 @@ void test_schedule_zero(void)
SysTimerTest st; SysTimerTest st;
st.schedule_tick(0UL); st.schedule_tick(0UL);
int32_t sem_slots = st.sem_wait(0UL); bool acquired = st.sem_try_acquire(0);
TEST_ASSERT_EQUAL_INT32(1, sem_slots); TEST_ASSERT_TRUE(acquired);
} }
/** Test handler called once /** Test handler called once
@ -238,20 +238,20 @@ void test_handler_called_once(void)
SysTimerTest st; SysTimerTest st;
st.schedule_tick(TEST_TICKS); st.schedule_tick(TEST_TICKS);
us_timestamp_t t1 = st.get_time(); us_timestamp_t t1 = st.get_time();
int32_t sem_slots = st.sem_wait(0); bool acquired = st.sem_try_acquire(0);
TEST_ASSERT_EQUAL_INT32(0, sem_slots); TEST_ASSERT_FALSE(acquired);
// Wait in a busy loop to prevent entering sleep or deepsleep modes. // Wait in a busy loop to prevent entering sleep or deepsleep modes.
while (sem_slots != 1) { while (!acquired) {
sem_slots = st.sem_wait(0); acquired = st.sem_try_acquire(0);
} }
us_timestamp_t t2 = st.get_time(); us_timestamp_t t2 = st.get_time();
TEST_ASSERT_EQUAL_INT32(1, sem_slots); TEST_ASSERT_TRUE(acquired);
TEST_ASSERT_EQUAL_UINT32(1, st.get_tick()); TEST_ASSERT_EQUAL_UINT32(1, st.get_tick());
TEST_ASSERT_UINT64_WITHIN(DELAY_DELTA_US, DELAY_US, t2 - t1); TEST_ASSERT_UINT64_WITHIN(DELAY_DELTA_US, DELAY_US, t2 - t1);
sem_slots = st.sem_wait((DELAY_US + DELAY_DELTA_US) / 1000ULL); acquired = st.sem_try_acquire((DELAY_US + DELAY_DELTA_US) / 1000ULL);
TEST_ASSERT_EQUAL_INT32(0, sem_slots); TEST_ASSERT_FALSE(acquired);
TEST_ASSERT_EQUAL_UINT32(1, st.get_tick()); TEST_ASSERT_EQUAL_UINT32(1, st.get_tick());
} }
@ -275,7 +275,7 @@ void test_sleep(void)
st.schedule_tick(TEST_TICKS); st.schedule_tick(TEST_TICKS);
TEST_ASSERT_FALSE_MESSAGE(sleep_manager_can_deep_sleep(), "Deep sleep should be disallowed"); TEST_ASSERT_FALSE_MESSAGE(sleep_manager_can_deep_sleep(), "Deep sleep should be disallowed");
while (st.sem_wait(0) != 1) { while (!st.sem_try_acquire(0)) {
sleep(); sleep();
} }
timer.stop(); timer.stop();
@ -313,7 +313,7 @@ void test_deepsleep(void)
lptimer.start(); lptimer.start();
st.schedule_tick(TEST_TICKS); st.schedule_tick(TEST_TICKS);
TEST_ASSERT_TRUE_MESSAGE(sleep_manager_can_deep_sleep_test_check(), "Deep sleep should be allowed"); TEST_ASSERT_TRUE_MESSAGE(sleep_manager_can_deep_sleep_test_check(), "Deep sleep should be allowed");
while (st.sem_wait(0) != 1) { while (!st.sem_try_acquire(0)) {
sleep(); sleep();
} }
lptimer.stop(); lptimer.stop();

View File

@ -647,7 +647,7 @@ void test_mutex()
void test_semaphore_thread(Semaphore *sem) void test_semaphore_thread(Semaphore *sem)
{ {
sem->wait(); sem->acquire();
} }
/** Testing thread states: wait semaphore /** Testing thread states: wait semaphore

View File

@ -48,7 +48,7 @@ void ASYNCHRONOUS_DNS_CACHE()
mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data)); mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data));
TEST_ASSERT(err >= 0); TEST_ASSERT(err >= 0);
semaphore.wait(); semaphore.acquire();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, data.result); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, data.result);
TEST_ASSERT(strlen(data.addr.get_ip_address()) > 1); TEST_ASSERT(strlen(data.addr.get_ip_address()) > 1);

View File

@ -58,7 +58,7 @@ void ASYNCHRONOUS_DNS_CANCEL()
// Wait for callback(s) to complete // Wait for callback(s) to complete
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
semaphore.wait(); semaphore.acquire();
} }
for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) { for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {

View File

@ -45,7 +45,7 @@ void ASYNCHRONOUS_DNS_NON_ASYNC_AND_ASYNC()
TEST_ASSERT(strlen(addr.get_ip_address()) > 1); TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
} }
semaphore.wait(100); semaphore.try_acquire_for(100);
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, data.result); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, data.result);

View File

@ -82,7 +82,7 @@ void do_asynchronous_gethostbyname(const char hosts[][DNS_TEST_HOST_LEN], unsign
// Wait for callback(s) to complete // Wait for callback(s) to complete
for (unsigned int i = 0; i < count; i++) { for (unsigned int i = 0; i < count; i++) {
semaphore.wait(); semaphore.acquire();
} }
// Print result // Print result

View File

@ -34,8 +34,6 @@ static void test_dns_query_ticker(void)
void SYNCHRONOUS_DNS_CACHE() void SYNCHRONOUS_DNS_CACHE()
{ {
rtos::Semaphore semaphore;
Ticker ticker; Ticker ticker;
ticker.attach_us(&test_dns_query_ticker, 100); ticker.attach_us(&test_dns_query_ticker, 100);

View File

@ -187,7 +187,7 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
} }
TEST_ASSERT_EQUAL(bytes2send, tcp_stats[j].sent_bytes); TEST_ASSERT_EQUAL(bytes2send, tcp_stats[j].sent_bytes);
#endif #endif
tx_sem.wait(split2half_rmng_tcp_test_time() * 1000); // *1000 to convert s->ms tx_sem.try_acquire_for(split2half_rmng_tcp_test_time() * 1000); // *1000 to convert s->ms
if (receive_error) { if (receive_error) {
break; break;
} }

View File

@ -192,7 +192,7 @@ void TLSSOCKET_ECHOTEST_NONBLOCK()
} }
TEST_ASSERT_EQUAL(bytes2send, tls_stats[j].sent_bytes); TEST_ASSERT_EQUAL(bytes2send, tls_stats[j].sent_bytes);
#endif #endif
tx_sem.wait(); tx_sem.acquire();
if (receive_error) { if (receive_error) {
break; break;
} }

View File

@ -183,7 +183,7 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent); printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent);
continue; continue;
} }
if (tx_sem.wait(WAIT2RECV_TIMEOUT * 2) == 0) { // RX might wait up to WAIT2RECV_TIMEOUT before recvfrom if (!tx_sem.try_acquire_for(WAIT2RECV_TIMEOUT * 2)) { // RX might wait up to WAIT2RECV_TIMEOUT before recvfrom
continue; continue;
} }
break; break;

View File

@ -525,9 +525,9 @@ void worker_loop_start(void (*test_step_cb_fnc)(int opt), int timeout)
} }
#if MBED_CONF_APP_ECHO_SERVER #if MBED_CONF_APP_ECHO_SERVER
worker_loop_semaphore.wait(); worker_loop_semaphore.acquire();
#else #else
worker_loop_semaphore.wait(600 * SECOND_TO_MS); worker_loop_semaphore.try_acquire_for(600 * SECOND_TO_MS);
#endif #endif
worker_loop_event_queue.cancel(test_step_cb_timer); worker_loop_event_queue.cancel(test_step_cb_timer);
@ -554,7 +554,7 @@ static void worker_loop_event_cb(int event)
void worker_loop_link_up_wait(void) void worker_loop_link_up_wait(void)
{ {
if (!link_up) { if (!link_up) {
link_status_semaphore.wait(); link_status_semaphore.acquire();
} }
} }

View File

@ -54,7 +54,7 @@ nsapi_connection_status_t wait_status_callback()
nsapi_connection_status_t status; nsapi_connection_status_t status;
while (true) { while (true) {
status_semaphore.wait(); status_semaphore.acquire();
status = statuses[status_read_counter]; status = statuses[status_read_counter];
status_read_counter++; status_read_counter++;

View File

@ -67,7 +67,7 @@ void MULTIHOMING_ASYNCHRONOUS_DNS()
mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data), NSAPI_UNSPEC, interface_name[j]); mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data), NSAPI_UNSPEC, interface_name[j]);
TEST_ASSERT(err >= 0); TEST_ASSERT(err >= 0);
semaphore.wait(); semaphore.acquire();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, data.result); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, data.result);
printf("DNS: query interface_name %s %d \n", interface_name[j], j); printf("DNS: query interface_name %s %d \n", interface_name[j], j);

View File

@ -179,7 +179,7 @@ void MULTIHOMING_UDPSOCKET_ECHOTEST_NONBLOCK()
printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent); printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent);
continue; continue;
} }
if (tx_sem.wait(WAIT2RECV_TIMEOUT * 2) == 0) { // RX might wait up to WAIT2RECV_TIMEOUT before recvfrom if (!tx_sem.try_acquire_for(WAIT2RECV_TIMEOUT * 2)) { // RX might wait up to WAIT2RECV_TIMEOUT before recvfrom
continue; continue;
} }
break; break;

View File

@ -474,7 +474,7 @@ TEST_F(TestAT_CellularContext, connect_disconnect_sync)
my_AT_CTX ctx1(at, &dev); my_AT_CTX ctx1(at, &dev);
ctx1.attach(&network_cb); ctx1.attach(&network_cb);
Semaphore_stub::wait_return_value = 1; Semaphore_stub::acquire_return_value = true;
// call callback so that network is opened which is needed in disconnect // call callback so that network is opened which is needed in disconnect
cell_callback_data_t data; cell_callback_data_t data;

View File

@ -19,6 +19,7 @@
#include "Semaphore_stub.h" #include "Semaphore_stub.h"
int Semaphore_stub::wait_return_value = 0; int Semaphore_stub::wait_return_value = 0;
bool Semaphore_stub::acquire_return_value;
namespace rtos { namespace rtos {
@ -47,6 +48,26 @@ int32_t Semaphore::wait_until(uint64_t millisec)
return Semaphore_stub::wait_return_value; return Semaphore_stub::wait_return_value;
} }
void Semaphore::acquire()
{
}
bool Semaphore::try_acquire()
{
return Semaphore_stub::acquire_return_value;
}
bool Semaphore::try_acquire_for(uint32_t millisec)
{
return Semaphore_stub::acquire_return_value;
}
bool Semaphore::try_acquire_until(uint64_t millisec)
{
return Semaphore_stub::acquire_return_value;
}
osStatus Semaphore::release(void) osStatus Semaphore::release(void)
{ {
return 0; return 0;

View File

@ -19,6 +19,7 @@
namespace Semaphore_stub { namespace Semaphore_stub {
extern int wait_return_value; extern int wait_return_value;
extern bool acquire_return_value;
} }

View File

@ -170,8 +170,8 @@ nsapi_error_t AT_CellularContext::check_operation(nsapi_error_t err, ContextOper
_current_op = op; _current_op = op;
if (err == NSAPI_ERROR_IN_PROGRESS || err == NSAPI_ERROR_OK) { if (err == NSAPI_ERROR_IN_PROGRESS || err == NSAPI_ERROR_OK) {
if (_is_blocking) { if (_is_blocking) {
int sema_err = _semaphore.wait(get_timeout_for_operation(op)); // cellular network searching may take several minutes int sema_acq = _semaphore.try_acquire_for(get_timeout_for_operation(op)); // cellular network searching may take several minutes
if (sema_err != 1) { if (!sema_acq) {
tr_warning("No cellular connection"); tr_warning("No cellular connection");
return NSAPI_ERROR_TIMEOUT; return NSAPI_ERROR_TIMEOUT;
} }
@ -1043,7 +1043,7 @@ nsapi_error_t AT_CellularContext::setup_control_plane_opt()
} }
//wait for control plane opt call back to release semaphore //wait for control plane opt call back to release semaphore
_cp_opt_semaphore.wait(CP_OPT_NW_REPLY_TIMEOUT); _cp_opt_semaphore.try_acquire_for(CP_OPT_NW_REPLY_TIMEOUT);
if (_cp_in_use) { if (_cp_in_use) {
return NSAPI_ERROR_OK; return NSAPI_ERROR_OK;

View File

@ -103,7 +103,7 @@ nsapi_error_t QUECTEL_BG96_CellularContext::activate_non_ip_context()
_at.unlock(); _at.unlock();
if (ret == NSAPI_ERROR_OK) { if (ret == NSAPI_ERROR_OK) {
_semaphore.wait(NIDD_OPEN_URC_TIMEOUT); _semaphore.try_acquire_for(NIDD_OPEN_URC_TIMEOUT);
if (_cid == -1) { if (_cid == -1) {
return NSAPI_ERROR_NO_CONNECTION; return NSAPI_ERROR_NO_CONNECTION;
} }

View File

@ -16,6 +16,7 @@
#include "nsapi.h" #include "nsapi.h"
#include "mbed_interface.h" #include "mbed_interface.h"
#include "mbed_assert.h" #include "mbed_assert.h"
#include "Semaphore.h"
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
@ -152,8 +153,7 @@ static bool convert_mbed_addr_to_lwip(ip_addr_t *out, const nsapi_addr_t *in)
void LWIP::tcpip_init_irq(void *eh) void LWIP::tcpip_init_irq(void *eh)
{ {
LWIP *lwip = static_cast<LWIP *>(eh); static_cast<rtos::Semaphore *>(eh)->release();
lwip->tcpip_inited.release();
sys_tcpip_thread_set(); sys_tcpip_thread_set();
} }
@ -172,8 +172,10 @@ LWIP::LWIP()
} }
lwip_init_tcp_isn(0, (u8_t *) &tcp_isn_secret); lwip_init_tcp_isn(0, (u8_t *) &tcp_isn_secret);
tcpip_init(&LWIP::tcpip_init_irq, this); rtos::Semaphore tcpip_inited;
tcpip_inited.wait(0);
tcpip_init(&LWIP::tcpip_init_irq, &tcpip_inited);
tcpip_inited.acquire();
// Zero out socket set // Zero out socket set
arena_init(); arena_init();

View File

@ -590,7 +590,6 @@ private:
static void tcpip_thread_callback(void *ptr); static void tcpip_thread_callback(void *ptr);
char ip_address[40]; char ip_address[40];
rtos::Semaphore tcpip_inited;
Interface *default_interface; Interface *default_interface;
LWIPMemoryManager memory_manager; LWIPMemoryManager memory_manager;
osThreadId tcpip_thread_id; osThreadId tcpip_thread_id;

View File

@ -17,6 +17,7 @@
#ifndef MESHINTERFACENANOSTACK_H #ifndef MESHINTERFACENANOSTACK_H
#define MESHINTERFACENANOSTACK_H #define MESHINTERFACENANOSTACK_H
#include "Semaphore.h"
#include "MeshInterface.h" #include "MeshInterface.h"
#include "NanostackRfPhy.h" #include "NanostackRfPhy.h"
#include "Nanostack.h" #include "Nanostack.h"

View File

@ -92,11 +92,7 @@ nsapi_error_t Nanostack::LoWPANNDInterface::bringup(bool dhcp, const char *ip,
if (blocking) { if (blocking) {
// wait connection for ever // wait connection for ever
int32_t count = connect_semaphore.wait(osWaitForever); connect_semaphore.acquire();
if (count <= 0) {
return NSAPI_ERROR_DHCP_FAILURE; // sort of...
}
} }
return 0; return 0;

View File

@ -78,9 +78,9 @@ nsapi_error_t Nanostack::EthernetInterface::bringup(bool dhcp, const char *ip,
} }
if (blocking) { if (blocking) {
int32_t count = connect_semaphore.wait(30000); bool acquired = connect_semaphore.try_acquire_for(30000);
if (count <= 0) { if (!acquired) {
return NSAPI_ERROR_DHCP_FAILURE; // sort of... return NSAPI_ERROR_DHCP_FAILURE; // sort of...
} }
} }
@ -112,7 +112,7 @@ nsapi_error_t Nanostack::EthernetInterface::bringdown()
} }
if (_blocking) { if (_blocking) {
int32_t count = disconnect_semaphore.wait(30000); int32_t count = disconnect_semaphore.try_acquire_for(30000);
if (count <= 0) { if (count <= 0) {
return NSAPI_ERROR_TIMEOUT; return NSAPI_ERROR_TIMEOUT;

View File

@ -148,11 +148,7 @@ nsapi_error_t Nanostack::ThreadInterface::bringup(bool dhcp, const char *ip,
// -devices without network settings gets connectivity once commissioned and attached to network // -devices without network settings gets connectivity once commissioned and attached to network
_connect_status = NSAPI_STATUS_CONNECTING; _connect_status = NSAPI_STATUS_CONNECTING;
if (_blocking) { if (_blocking) {
int32_t count = connect_semaphore.wait(osWaitForever); connect_semaphore.acquire();
if (count <= 0) {
return NSAPI_ERROR_DHCP_FAILURE; // sort of...
}
} }
return 0; return 0;
} }

View File

@ -92,11 +92,7 @@ nsapi_error_t Nanostack::WisunInterface::bringup(bool dhcp, const char *ip,
if (blocking) { if (blocking) {
// wait connection for ever // wait connection for ever
int32_t count = connect_semaphore.wait(osWaitForever); connect_semaphore.acquire();
if (count <= 0) {
return NSAPI_ERROR_DHCP_FAILURE; // sort of...
}
} }
return 0; return 0;

View File

@ -25,7 +25,6 @@
#include "netsocket/TCPSocket.h" #include "netsocket/TCPSocket.h"
#include "netsocket/NetworkStack.h" #include "netsocket/NetworkStack.h"
#include "netsocket/NetworkInterface.h" #include "netsocket/NetworkInterface.h"
#include "rtos/Semaphore.h"
/** TCP socket server /** TCP socket server

View File

@ -412,7 +412,7 @@ bool Kinetis_EMAC::link_out(emac_mem_buf_t *buf)
} }
/* Check if a descriptor is available for the transfer (wait 10ms before dropping the buffer) */ /* Check if a descriptor is available for the transfer (wait 10ms before dropping the buffer) */
if (xTXDCountSem.wait(10) == 0) { if (!xTXDCountSem.try_acquire_for(10)) {
memory_manager->free(buf); memory_manager->free(buf);
return false; return false;
} }

View File

@ -20,7 +20,6 @@
#define GD32_EMAC_H_ #define GD32_EMAC_H_
#include "EMAC.h" #include "EMAC.h"
#include "rtos/Semaphore.h"
#include "rtos/Mutex.h" #include "rtos/Mutex.h"
class GD32_EMAC : public EMAC { class GD32_EMAC : public EMAC {

View File

@ -18,7 +18,6 @@
#define NUMAKER_EMAC_H_ #define NUMAKER_EMAC_H_
#include "EMAC.h" #include "EMAC.h"
#include "rtos/Semaphore.h"
#include "rtos/Mutex.h" #include "rtos/Mutex.h"
class NUMAKER_EMAC : public EMAC { class NUMAKER_EMAC : public EMAC {
@ -161,7 +160,6 @@ private:
mbed_rtos_storage_thread_t thread_cb; mbed_rtos_storage_thread_t thread_cb;
osThreadId_t thread; /**< Processing thread */ osThreadId_t thread; /**< Processing thread */
rtos::Mutex TXLockMutex;/**< TX critical section mutex */ rtos::Mutex TXLockMutex;/**< TX critical section mutex */
rtos::Semaphore xTXDCountSem; /**< TX free buffer counting semaphore */
emac_link_input_cb_t emac_link_input_cb; /**< Callback for incoming data */ emac_link_input_cb_t emac_link_input_cb; /**< Callback for incoming data */
emac_link_state_change_cb_t emac_link_state_cb; /**< Link state change callback */ emac_link_state_change_cb_t emac_link_state_cb; /**< Link state change callback */
EMACMemoryManager *memory_manager; /**< Memory manager */ EMACMemoryManager *memory_manager; /**< Memory manager */

View File

@ -411,7 +411,7 @@ bool Kinetis_EMAC::link_out(emac_mem_buf_t *buf)
SCB_CleanDCache_by_Addr(static_cast<uint32_t *>(memory_manager->get_ptr(buf)), memory_manager->get_len(buf)); SCB_CleanDCache_by_Addr(static_cast<uint32_t *>(memory_manager->get_ptr(buf)), memory_manager->get_len(buf));
/* Check if a descriptor is available for the transfer (wait 10ms before dropping the buffer) */ /* Check if a descriptor is available for the transfer (wait 10ms before dropping the buffer) */
if (xTXDCountSem.wait(10) == 0) { if (!xTXDCountSem.try_acquire_for(10)) {
memory_manager->free(buf); memory_manager->free(buf);
return false; return false;
} }

View File

@ -541,7 +541,7 @@ bool LPC17_EMAC::link_out(emac_mem_buf_t *p)
/* Wait until enough descriptors are available for the transfer. */ /* Wait until enough descriptors are available for the transfer. */
/* THIS WILL BLOCK UNTIL THERE ARE ENOUGH DESCRIPTORS AVAILABLE */ /* THIS WILL BLOCK UNTIL THERE ARE ENOUGH DESCRIPTORS AVAILABLE */
for (int32_t count = 0; count < dn; count++) { for (int32_t count = 0; count < dn; count++) {
xTXDCountSem.wait(); xTXDCountSem.acquire();
} }
MBED_ASSERT(dn <= lpc_tx_ready()); MBED_ASSERT(dn <= lpc_tx_ready());
@ -655,7 +655,7 @@ void LPC17_EMAC::packet_tx(void* pvParameters)
while (1) { while (1) {
/* Wait for transmit cleanup task to wakeup */ /* Wait for transmit cleanup task to wakeup */
lpc17_emac->TxCleanSem.wait(); lpc17_emac->TxCleanSem.acquire();
/* Error handling for TX underruns. This should never happen unless /* Error handling for TX underruns. This should never happen unless
something is holding the bus or the clocks are going too slow. It something is holding the bus or the clocks are going too slow. It

View File

@ -409,7 +409,7 @@ bool LPC546XX_EMAC::link_out(emac_mem_buf_t *buf)
} }
/* Check if a descriptor is available for the transfer. */ /* Check if a descriptor is available for the transfer. */
if (xTXDCountSem.wait(0) == 0) { if (!xTXDCountSem.try_acquire()) {
memory_manager->free(buf); memory_manager->free(buf);
return false; return false;
} }

View File

@ -18,7 +18,6 @@
#define STM32_EMAC_H_ #define STM32_EMAC_H_
#include "EMAC.h" #include "EMAC.h"
#include "rtos/Semaphore.h"
#include "rtos/Mutex.h" #include "rtos/Mutex.h"
class RDA5981x_EMAC : public EMAC { class RDA5981x_EMAC : public EMAC {

View File

@ -17,7 +17,6 @@
#define STM32_EMAC_H_ #define STM32_EMAC_H_
#include "EMAC.h" #include "EMAC.h"
#include "rtos/Semaphore.h"
#include "rtos/Mutex.h" #include "rtos/Mutex.h"
class STM32_EMAC : public EMAC { class STM32_EMAC : public EMAC {

View File

@ -124,7 +124,7 @@ void SL_EMAC::power_down()
tr_debug("Link coming down, waiting for TX to be done."); tr_debug("Link coming down, waiting for TX to be done.");
tx_sem.wait(); tx_sem.acquire();
NVIC_DisableIRQ(ETH_IRQn); NVIC_DisableIRQ(ETH_IRQn);
@ -491,8 +491,7 @@ bool SL_EMAC::link_out(emac_mem_buf_t *buf)
} }
/* Wait for previous packet to finish transmitting */ /* Wait for previous packet to finish transmitting */
int32_t stat = tx_sem.wait(100); if (!tx_sem.try_acquire_for(100)) {
if (stat <= 0) {
tr_warn("TX process didn't complete within 100ms"); tr_warn("TX process didn't complete within 100ms");
memory_manager->free(buf); memory_manager->free(buf);
return false; return false;

View File

@ -52,8 +52,7 @@ bool ConditionVariable::wait_for(uint32_t millisec)
_mutex.unlock(); _mutex.unlock();
int32_t sem_count = current_thread.sem.wait(millisec); bool timeout = !current_thread.sem.try_acquire_for(millisec);
bool timeout = (sem_count > 0) ? false : true;
_mutex.lock(); _mutex.lock();

View File

@ -22,6 +22,7 @@
#include "rtos/Semaphore.h" #include "rtos/Semaphore.h"
#include "rtos/Kernel.h" #include "rtos/Kernel.h"
#include "platform/mbed_assert.h" #include "platform/mbed_assert.h"
#include "platform/mbed_error.h"
#include <string.h> #include <string.h>
@ -46,7 +47,17 @@ void Semaphore::constructor(int32_t count, uint16_t max_count)
MBED_ASSERT(_id != NULL); MBED_ASSERT(_id != NULL);
} }
int32_t Semaphore::wait(uint32_t millisec) bool Semaphore::try_acquire()
{
osStatus_t status = osSemaphoreAcquire(_id, 0);
if (status != osOK && status != osErrorResource) {
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_SEMAPHORE_LOCK_FAILED), "Semaphore acquire failed", status);
}
return status == osOK;
}
/* To sidestep deprecation warnings */
int32_t Semaphore::_wait(uint32_t millisec)
{ {
osStatus_t stat = osSemaphoreAcquire(_id, millisec); osStatus_t stat = osSemaphoreAcquire(_id, millisec);
switch (stat) { switch (stat) {
@ -61,17 +72,60 @@ int32_t Semaphore::wait(uint32_t millisec)
} }
} }
int32_t Semaphore::wait(uint32_t millisec)
{
return _wait(millisec);
}
int32_t Semaphore::wait_until(uint64_t millisec) int32_t Semaphore::wait_until(uint64_t millisec)
{ {
uint64_t now = Kernel::get_ms_count(); uint64_t now = Kernel::get_ms_count();
if (now >= millisec) { if (now >= millisec) {
return wait(0); return _wait(0);
} else if (millisec - now >= osWaitForever) { } else if (millisec - now >= osWaitForever) {
// API permits early return // API permits early return
return wait(osWaitForever - 1); return _wait(osWaitForever - 1);
} else { } else {
return wait(millisec - now); return _wait(millisec - now);
}
}
void Semaphore::acquire()
{
osStatus_t status = osSemaphoreAcquire(_id, osWaitForever);
if (status != osOK) {
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_SEMAPHORE_LOCK_FAILED), "Semaphore acquire failed", status);
}
}
bool Semaphore::try_acquire_for(uint32_t millisec)
{
osStatus_t status = osSemaphoreAcquire(_id, millisec);
if (status == osOK) {
return true;
}
bool success = (status == osErrorResource && millisec == 0) ||
(status == osErrorTimeout && millisec != osWaitForever);
if (!success) {
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_SEMAPHORE_LOCK_FAILED), "Semaphore acquire failed", status);
}
return false;
}
bool Semaphore::try_acquire_until(uint64_t millisec)
{
uint64_t now = Kernel::get_ms_count();
if (now >= millisec) {
return try_acquire();
} else if (millisec - now >= osWaitForever) {
// API permits early return
return try_acquire_for(osWaitForever - 1);
} else {
return try_acquire_for(millisec - now);
} }
} }

View File

@ -26,6 +26,7 @@
#include "cmsis_os2.h" #include "cmsis_os2.h"
#include "mbed_rtos1_types.h" #include "mbed_rtos1_types.h"
#include "mbed_rtos_storage.h" #include "mbed_rtos_storage.h"
#include "platform/mbed_toolchain.h"
#include "platform/NonCopyable.h" #include "platform/NonCopyable.h"
namespace rtos { namespace rtos {
@ -60,14 +61,21 @@ public:
Semaphore(int32_t count, uint16_t max_count); Semaphore(int32_t count, uint16_t max_count);
/** Wait until a Semaphore resource becomes available. /** Wait until a Semaphore resource becomes available.
@deprecated Do not use this function. This function has been replaced with acquire(), try_acquire() and try_acquire_for() functions.
@param millisec timeout value. (default: osWaitForever). @param millisec timeout value. (default: osWaitForever).
@return number of available tokens, before taking one; or -1 in case of incorrect parameters @return number of available tokens, before taking one; or -1 in case of incorrect parameters
@note You may call this function from ISR context if the millisec parameter is set to 0. @note You may call this function from ISR context if the millisec parameter is set to 0.
*/ */
MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with acquire, try_acquire() and try_acquire_for() functions")
int32_t wait(uint32_t millisec = osWaitForever); int32_t wait(uint32_t millisec = osWaitForever);
/** Wait until a Semaphore resource becomes available. /** Wait until a Semaphore resource becomes available.
@deprecated Do not use this function. This function has been replaced with try_acquire_until().
@param millisec absolute timeout time, referenced to Kernel::get_ms_count() @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
@return number of available tokens, before taking one; or -1 in case of incorrect parameters @return number of available tokens, before taking one; or -1 in case of incorrect parameters
@note the underlying RTOS may have a limit to the maximum wait time @note the underlying RTOS may have a limit to the maximum wait time
@ -77,9 +85,44 @@ public:
@note You cannot call this function from ISR context. @note You cannot call this function from ISR context.
*/ */
MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with try_acquire_until()")
int32_t wait_until(uint64_t millisec); int32_t wait_until(uint64_t millisec);
/** Release a Semaphore resource that was obtain with Semaphore::wait. /** Wait until a Semaphore resource becomes available.
@note You cannot call this function from ISR context.
*/
void acquire();
/** Try to acquire a Semaphore resource, and return immediately
@return true if a resource was acquired, false otherwise.
@note equivalent to try_acquire_for(0)
@note You may call this function from ISR context.
*/
bool try_acquire();
/** Wait until a Semaphore resource becomes available.
@param millisec timeout value.
@return true if a resource was acquired, false otherwise.
@note You may call this function from ISR context if the millisec parameter is set to 0.
*/
bool try_acquire_for(uint32_t millisec);
/** Wait until a Semaphore resource becomes available.
@param millisec absolute timeout time, referenced to Kernel::get_ms_count()
@return true if a resource was acquired, false otherwise.
@note the underlying RTOS may have a limit to the maximum wait time
due to internal 32-bit computations, but this is guaranteed to work if the
wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
the acquire attempt will time out earlier than specified.
@note You cannot call this function from ISR context.
*/
bool try_acquire_until(uint64_t millisec);
/** Release a Semaphore resource that was obtain with Semaphore::acquire.
@return status code that indicates the execution status of the function: @return status code that indicates the execution status of the function:
@a osOK the token has been correctly released. @a osOK the token has been correctly released.
@a osErrorResource the maximum token count has been reached. @a osErrorResource the maximum token count has been reached.
@ -98,6 +141,8 @@ public:
private: private:
void constructor(int32_t count, uint16_t max_count); void constructor(int32_t count, uint16_t max_count);
int32_t _wait(uint32_t millisec);
osSemaphoreId_t _id; osSemaphoreId_t _id;
mbed_rtos_storage_semaphore_t _obj_mem; mbed_rtos_storage_semaphore_t _obj_mem;
}; };

View File

@ -148,10 +148,7 @@ osStatus Thread::terminate()
osStatus Thread::join() osStatus Thread::join()
{ {
int32_t ret = _join_sem.wait(); _join_sem.acquire();
if (ret < 0) {
return osError;
}
// The semaphore has been released so this thread is being // The semaphore has been released so this thread is being
// terminated or has been terminated. Once the mutex has // terminated or has been terminated. Once the mutex has

View File

@ -77,7 +77,7 @@ uint32_t ipcrpc_call(uint32_t call_id, uint32_t args_num, ...)
MBED_ASSERT(status == CY_IPC_PIPE_SUCCESS); MBED_ASSERT(status == CY_IPC_PIPE_SUCCESS);
// Now wait for the response; // Now wait for the response;
msg_semaphore->wait(); msg_semaphore->acquire();
return message.result; return message.result;
} }

View File

@ -18,7 +18,6 @@
#define RTW_EMAC_H #define RTW_EMAC_H
#include "EMAC.h" #include "EMAC.h"
#include "rtos/Semaphore.h"
#include "rtos/Mutex.h" #include "rtos/Mutex.h"
#include "netif.h" #include "netif.h"
#include "EMACMemoryManager.h" #include "EMACMemoryManager.h"

View File

@ -21,7 +21,6 @@
#include "EMACInterface.h" #include "EMACInterface.h"
#include "WiFiInterface.h" #include "WiFiInterface.h"
#include "rtos/Semaphore.h"
#include "rtos/Mutex.h" #include "rtos/Mutex.h"
class WICED_EMAC : public EMAC { class WICED_EMAC : public EMAC {

View File

@ -63,7 +63,7 @@ void AsyncOp::wait(rtos::Mutex *host_mutex, uint32_t milliseconds)
return; return;
} }
if (sem.wait(milliseconds) == 1) { if (sem.try_acquire_for(milliseconds)) {
// Operation completion signaled semaphore // Operation completion signaled semaphore
return; return;
} }

View File

@ -79,7 +79,7 @@ void TaskBase::wait()
_flush_sem = &sem; _flush_sem = &sem;
core_util_critical_section_exit(); core_util_critical_section_exit();
sem.wait(); sem.acquire();
} }
bool TaskBase::ready() bool TaskBase::ready()