Merge pull request #10596 from kjbracey-arm/sem_acquire

Introduce Semaphore::acquire methods
pull/10695/head
Martin Kojtal 2019-05-29 06:22:30 +01:00 committed by GitHub
commit 9cc1caa031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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(1, event_sem.wait(1000));
TEST_ASSERT_TRUE(event_sem.try_acquire_for(1000));
TEST_ASSERT_EQUAL(EV_TX_DONE, received_event);
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(1, event_sem.wait(1000));
TEST_ASSERT_TRUE(event_sem.try_acquire_for(1000));
// Nobody was sending to us so timeout is expected.
TEST_ASSERT_EQUAL(EV_RX_TIMEOUT, received_event);

View File

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

View File

@ -257,22 +257,20 @@ void test_multi_call_time(void)
void test_detach(void)
{
Ticker ticker;
int32_t ret;
bool ret;
const float ticker_time_s = 0.1f;
const uint32_t wait_time_ms = 500;
Semaphore sem(0, 1);
ticker.attach(callback(sem_release, &sem), ticker_time_s);
ret = sem.wait();
TEST_ASSERT_TRUE(ret > 0);
sem.acquire();
ret = sem.wait();
sem.acquire();
ticker.detach(); /* cancel */
TEST_ASSERT_TRUE(ret > 0);
ret = sem.wait(wait_time_ms);
TEST_ASSERT_EQUAL(0, ret);
ret = sem.try_acquire_for(wait_time_ms);
TEST_ASSERT_FALSE(ret);
}
/** 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);
int32_t sem_slots = sem.wait(0);
TEST_ASSERT_EQUAL(0, sem_slots);
bool acquired = sem.try_acquire();
TEST_ASSERT_FALSE(acquired);
sem_slots = sem.wait(TEST_DELAY_MS + 2);
TEST_ASSERT_EQUAL(1, sem_slots);
acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
TEST_ASSERT_TRUE(acquired);
sem_slots = sem.wait(TEST_DELAY_MS + 2);
TEST_ASSERT_EQUAL(0, sem_slots);
acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
TEST_ASSERT_FALSE(acquired);
timeout.detach();
}
@ -110,12 +110,12 @@ void test_cancel(void)
timeout.attach_callback(mbed::callback(sem_callback, &sem), 2.0f * TEST_DELAY_US);
int32_t sem_slots = sem.wait(TEST_DELAY_MS);
TEST_ASSERT_EQUAL(0, sem_slots);
bool acquired = sem.try_acquire_for(TEST_DELAY_MS);
TEST_ASSERT_FALSE(acquired);
timeout.detach();
sem_slots = sem.wait(TEST_DELAY_MS + 2);
TEST_ASSERT_EQUAL(0, sem_slots);
acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
TEST_ASSERT_FALSE(acquired);
}
/** 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);
int32_t sem_slots = sem1.wait(TEST_DELAY_MS);
TEST_ASSERT_EQUAL(0, sem_slots);
bool acquired = sem1.try_acquire_for(TEST_DELAY_MS);
TEST_ASSERT_FALSE(acquired);
timeout.attach_callback(mbed::callback(sem_callback, &sem2), 2.0f * TEST_DELAY_US);
sem_slots = sem2.wait(2 * TEST_DELAY_MS + 2);
TEST_ASSERT_EQUAL(1, sem_slots);
sem_slots = sem1.wait(0);
TEST_ASSERT_EQUAL(0, sem_slots);
acquired = sem2.try_acquire_for(2 * TEST_DELAY_MS + 2);
TEST_ASSERT_TRUE(acquired);
acquired = sem1.try_acquire();
TEST_ASSERT_FALSE(acquired);
timeout.detach();
}
@ -200,8 +200,8 @@ void test_no_wait(void)
T timeout;
timeout.attach_callback(mbed::callback(sem_callback, &sem), 0ULL);
int32_t sem_slots = sem.wait(0);
TEST_ASSERT_EQUAL(1, sem_slots);
bool acquired = sem.try_acquire();
TEST_ASSERT_TRUE(acquired);
timeout.detach();
}
@ -227,9 +227,8 @@ void test_delay_accuracy(void)
timer.start();
timeout.attach_callback(mbed::callback(sem_callback, &sem), delay_us);
int32_t sem_slots = sem.wait(osWaitForever);
sem.acquire();
timer.stop();
TEST_ASSERT_EQUAL(1, sem_slots);
TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.read_high_resolution_us());
timeout.detach();
@ -265,7 +264,7 @@ void test_sleep(void)
bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check();
TEST_ASSERT_FALSE_MESSAGE(deep_sleep_allowed, "Deep sleep should be disallowed");
while (sem.wait(0) != 1) {
while (!sem.try_acquire()) {
sleep();
}
timer.stop();
@ -324,7 +323,7 @@ void test_deepsleep(void)
bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check();
TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed, "Deep sleep should be allowed");
while (sem.wait(0) != 1) {
while (!sem.try_acquire()) {
sleep();
}
timer.stop();

View File

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

View File

@ -261,9 +261,9 @@ void test_error_hook()
}
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)

View File

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

View File

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

View File

@ -46,7 +46,7 @@ void test_thread(int const *delay)
{
const int thread_delay = *delay;
while (true) {
two_slots.wait();
two_slots.acquire();
sem_counter++;
const bool sem_lock_failed = sem_counter > SEMAPHORE_SLOTS;
if (sem_lock_failed) {
@ -96,8 +96,7 @@ struct thread_data {
void single_thread(struct thread_data *data)
{
int32_t cnt = data->sem->wait();
TEST_ASSERT_EQUAL(1, cnt);
data->sem->acquire();
data->data++;
}
@ -140,8 +139,8 @@ void test_single_thread()
void timeout_thread(Semaphore *sem)
{
int32_t cnt = sem->wait(30);
TEST_ASSERT_EQUAL(0, cnt);
bool acquired = sem->try_acquire_for(30);
TEST_ASSERT_FALSE(acquired);
}
/** Test timeout
@ -188,8 +187,8 @@ void test_no_timeout()
Timer timer;
timer.start();
int32_t cnt = sem.wait(0);
TEST_ASSERT_EQUAL(T, cnt);
bool acquired = sem.try_acquire();
TEST_ASSERT_EQUAL(T > 0, acquired);
TEST_ASSERT_UINT32_WITHIN(5000, 0, timer.read_us());
}
@ -205,8 +204,8 @@ void test_multiple_tokens_wait()
Semaphore sem(5);
for (int i = 5; i >= 0; i--) {
int32_t cnt = sem.wait(0);
TEST_ASSERT_EQUAL(i, cnt);
bool acquired = sem.try_acquire();
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)
{
sync->sem_parent.release();
sync->sem_child.wait();
sync->sem_child.acquire();
osEvent ev = Thread::signal_wait(signals, timeout);
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)
{
sync->sem_parent.release();
sync->sem_child.wait();
sync->sem_child.acquire();
int32_t ret = ThisThread::flags_clear(signals);
TEST_ASSERT_EQUAL(test_val, ret);
}
@ -92,7 +92,7 @@ void run_double_wait_clear(Sync *sync)
int32_t ret;
sync->sem_parent.release();
sync->sem_child.wait();
sync->sem_child.acquire();
ret = ThisThread::flags_clear(signals1);
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++) {
int32_t signal = 1 << i;
signals |= signal;
sync->sem_child.wait();
sync->sem_child.acquire();
int32_t ret = ThisThread::flags_clear(NO_SIGNALS);
TEST_ASSERT_EQUAL(signals, ret);
sync->sem_parent.release();
@ -128,7 +128,7 @@ void test_clear_no_signals(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE);
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);
sem_child.release();
t.join();
@ -163,7 +163,7 @@ void test_set_all(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_wait_clear<NO_SIGNALS, ALL_SIGNALS>, &sync));
sem_parent.wait();
sem_parent.acquire();
ret = t.signal_set(ALL_SIGNALS);
TEST_ASSERT_EQUAL(ALL_SIGNALS, ret);
@ -190,7 +190,7 @@ void test_set_prohibited(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_wait_clear<NO_SIGNALS, ALL_SIGNALS>, &sync));
sem_parent.wait();
sem_parent.acquire();
t.signal_set(ALL_SIGNALS);
#if !MBED_TRAP_ERRORS_ENABLED
@ -216,7 +216,7 @@ void test_clear_all(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE);
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);
sem_child.release();
t.join();
@ -245,7 +245,7 @@ void test_set_all_loop(void)
signals |= signal;
TEST_ASSERT_EQUAL(signals, ret);
sem_child.release();
sem_parent.wait();
sem_parent.acquire();
}
t.join();
}
@ -280,7 +280,7 @@ void test_wait_all_already_set(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE);
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());
t.signal_set(ALL_SIGNALS);
sem_child.release();
@ -300,7 +300,7 @@ void test_wait_all(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_release_signal_wait<ALL_SIGNALS, osWaitForever, osEventSignal>, &sem));
sem.wait();
sem.acquire();
TEST_ASSERT_EQUAL(Thread::WaitingThreadFlag, t.get_state());
t.signal_set(ALL_SIGNALS);
@ -321,7 +321,7 @@ void test_wait_all_loop(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE);
t.start(callback(run_release_signal_wait<ALL_SIGNALS, osWaitForever, osEventSignal>, &sem));
sem.wait();
sem.acquire();
TEST_ASSERT_EQUAL(Thread::WaitingThreadFlag, t.get_state());
for (int i = 0; i < MAX_FLAG_POS; i++) {
@ -348,7 +348,7 @@ void test_set_double(void)
Thread t(osPriorityNormal, TEST_STACK_SIZE);
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());
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.cancel_tick();
int32_t sem_slots = st.sem_wait((DELAY_US + DELAY_DELTA_US) / 1000ULL);
TEST_ASSERT_EQUAL_INT32(0, sem_slots);
bool acquired = st.sem_try_acquire((DELAY_US + DELAY_DELTA_US) / 1000ULL);
TEST_ASSERT_FALSE(acquired);
TEST_ASSERT_EQUAL_UINT32(0, st.get_tick());
}
@ -220,8 +220,8 @@ void test_schedule_zero(void)
SysTimerTest st;
st.schedule_tick(0UL);
int32_t sem_slots = st.sem_wait(0UL);
TEST_ASSERT_EQUAL_INT32(1, sem_slots);
bool acquired = st.sem_try_acquire(0);
TEST_ASSERT_TRUE(acquired);
}
/** Test handler called once
@ -238,20 +238,20 @@ void test_handler_called_once(void)
SysTimerTest st;
st.schedule_tick(TEST_TICKS);
us_timestamp_t t1 = st.get_time();
int32_t sem_slots = st.sem_wait(0);
TEST_ASSERT_EQUAL_INT32(0, sem_slots);
bool acquired = st.sem_try_acquire(0);
TEST_ASSERT_FALSE(acquired);
// Wait in a busy loop to prevent entering sleep or deepsleep modes.
while (sem_slots != 1) {
sem_slots = st.sem_wait(0);
while (!acquired) {
acquired = st.sem_try_acquire(0);
}
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_UINT64_WITHIN(DELAY_DELTA_US, DELAY_US, t2 - t1);
sem_slots = st.sem_wait((DELAY_US + DELAY_DELTA_US) / 1000ULL);
TEST_ASSERT_EQUAL_INT32(0, sem_slots);
acquired = st.sem_try_acquire((DELAY_US + DELAY_DELTA_US) / 1000ULL);
TEST_ASSERT_FALSE(acquired);
TEST_ASSERT_EQUAL_UINT32(1, st.get_tick());
}
@ -275,7 +275,7 @@ void test_sleep(void)
st.schedule_tick(TEST_TICKS);
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();
}
timer.stop();
@ -313,7 +313,7 @@ void test_deepsleep(void)
lptimer.start();
st.schedule_tick(TEST_TICKS);
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();
}
lptimer.stop();

View File

@ -647,7 +647,7 @@ void test_mutex()
void test_semaphore_thread(Semaphore *sem)
{
sem->wait();
sem->acquire();
}
/** 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));
TEST_ASSERT(err >= 0);
semaphore.wait();
semaphore.acquire();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, data.result);
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
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++) {

View File

@ -45,7 +45,7 @@ void ASYNCHRONOUS_DNS_NON_ASYNC_AND_ASYNC()
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
}
semaphore.wait(100);
semaphore.try_acquire_for(100);
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
for (unsigned int i = 0; i < count; i++) {
semaphore.wait();
semaphore.acquire();
}
// Print result

View File

@ -34,8 +34,6 @@ static void test_dns_query_ticker(void)
void SYNCHRONOUS_DNS_CACHE()
{
rtos::Semaphore semaphore;
Ticker ticker;
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);
#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) {
break;
}

View File

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

View File

@ -183,7 +183,7 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent);
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;
}
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
worker_loop_semaphore.wait();
worker_loop_semaphore.acquire();
#else
worker_loop_semaphore.wait(600 * SECOND_TO_MS);
worker_loop_semaphore.try_acquire_for(600 * SECOND_TO_MS);
#endif
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)
{
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;
while (true) {
status_semaphore.wait();
status_semaphore.acquire();
status = statuses[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]);
TEST_ASSERT(err >= 0);
semaphore.wait();
semaphore.acquire();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, data.result);
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);
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;
}
break;

View File

@ -474,7 +474,7 @@ TEST_F(TestAT_CellularContext, connect_disconnect_sync)
my_AT_CTX ctx1(at, &dev);
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
cell_callback_data_t data;

View File

@ -19,6 +19,7 @@
#include "Semaphore_stub.h"
int Semaphore_stub::wait_return_value = 0;
bool Semaphore_stub::acquire_return_value;
namespace rtos {
@ -47,6 +48,26 @@ int32_t Semaphore::wait_until(uint64_t millisec)
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)
{
return 0;

View File

@ -19,6 +19,7 @@
namespace Semaphore_stub {
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;
if (err == NSAPI_ERROR_IN_PROGRESS || err == NSAPI_ERROR_OK) {
if (_is_blocking) {
int sema_err = _semaphore.wait(get_timeout_for_operation(op)); // cellular network searching may take several minutes
if (sema_err != 1) {
int sema_acq = _semaphore.try_acquire_for(get_timeout_for_operation(op)); // cellular network searching may take several minutes
if (!sema_acq) {
tr_warning("No cellular connection");
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
_cp_opt_semaphore.wait(CP_OPT_NW_REPLY_TIMEOUT);
_cp_opt_semaphore.try_acquire_for(CP_OPT_NW_REPLY_TIMEOUT);
if (_cp_in_use) {
return NSAPI_ERROR_OK;

View File

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

View File

@ -16,6 +16,7 @@
#include "nsapi.h"
#include "mbed_interface.h"
#include "mbed_assert.h"
#include "Semaphore.h"
#include <stdio.h>
#include <stdbool.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)
{
LWIP *lwip = static_cast<LWIP *>(eh);
lwip->tcpip_inited.release();
static_cast<rtos::Semaphore *>(eh)->release();
sys_tcpip_thread_set();
}
@ -172,8 +172,10 @@ LWIP::LWIP()
}
lwip_init_tcp_isn(0, (u8_t *) &tcp_isn_secret);
tcpip_init(&LWIP::tcpip_init_irq, this);
tcpip_inited.wait(0);
rtos::Semaphore tcpip_inited;
tcpip_init(&LWIP::tcpip_init_irq, &tcpip_inited);
tcpip_inited.acquire();
// Zero out socket set
arena_init();

View File

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

View File

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

View File

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

View File

@ -78,9 +78,9 @@ nsapi_error_t Nanostack::EthernetInterface::bringup(bool dhcp, const char *ip,
}
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...
}
}
@ -112,7 +112,7 @@ nsapi_error_t Nanostack::EthernetInterface::bringdown()
}
if (_blocking) {
int32_t count = disconnect_semaphore.wait(30000);
int32_t count = disconnect_semaphore.try_acquire_for(30000);
if (count <= 0) {
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
_connect_status = NSAPI_STATUS_CONNECTING;
if (_blocking) {
int32_t count = connect_semaphore.wait(osWaitForever);
if (count <= 0) {
return NSAPI_ERROR_DHCP_FAILURE; // sort of...
}
connect_semaphore.acquire();
}
return 0;
}

View File

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

View File

@ -25,7 +25,6 @@
#include "netsocket/TCPSocket.h"
#include "netsocket/NetworkStack.h"
#include "netsocket/NetworkInterface.h"
#include "rtos/Semaphore.h"
/** 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) */
if (xTXDCountSem.wait(10) == 0) {
if (!xTXDCountSem.try_acquire_for(10)) {
memory_manager->free(buf);
return false;
}

View File

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

View File

@ -18,7 +18,6 @@
#define NUMAKER_EMAC_H_
#include "EMAC.h"
#include "rtos/Semaphore.h"
#include "rtos/Mutex.h"
class NUMAKER_EMAC : public EMAC {
@ -161,7 +160,6 @@ private:
mbed_rtos_storage_thread_t thread_cb;
osThreadId_t thread; /**< Processing thread */
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_state_change_cb_t emac_link_state_cb; /**< Link state change callback */
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));
/* 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);
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. */
/* THIS WILL BLOCK UNTIL THERE ARE ENOUGH DESCRIPTORS AVAILABLE */
for (int32_t count = 0; count < dn; count++) {
xTXDCountSem.wait();
xTXDCountSem.acquire();
}
MBED_ASSERT(dn <= lpc_tx_ready());
@ -655,7 +655,7 @@ void LPC17_EMAC::packet_tx(void* pvParameters)
while (1) {
/* 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
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. */
if (xTXDCountSem.wait(0) == 0) {
if (!xTXDCountSem.try_acquire()) {
memory_manager->free(buf);
return false;
}

View File

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

View File

@ -17,7 +17,6 @@
#define STM32_EMAC_H_
#include "EMAC.h"
#include "rtos/Semaphore.h"
#include "rtos/Mutex.h"
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.");
tx_sem.wait();
tx_sem.acquire();
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 */
int32_t stat = tx_sem.wait(100);
if (stat <= 0) {
if (!tx_sem.try_acquire_for(100)) {
tr_warn("TX process didn't complete within 100ms");
memory_manager->free(buf);
return false;

View File

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

View File

@ -22,6 +22,7 @@
#include "rtos/Semaphore.h"
#include "rtos/Kernel.h"
#include "platform/mbed_assert.h"
#include "platform/mbed_error.h"
#include <string.h>
@ -46,7 +47,17 @@ void Semaphore::constructor(int32_t count, uint16_t max_count)
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);
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)
{
uint64_t now = Kernel::get_ms_count();
if (now >= millisec) {
return wait(0);
return _wait(0);
} else if (millisec - now >= osWaitForever) {
// API permits early return
return wait(osWaitForever - 1);
return _wait(osWaitForever - 1);
} 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 "mbed_rtos1_types.h"
#include "mbed_rtos_storage.h"
#include "platform/mbed_toolchain.h"
#include "platform/NonCopyable.h"
namespace rtos {
@ -60,14 +61,21 @@ public:
Semaphore(int32_t count, uint16_t max_count);
/** 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).
@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.
*/
MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with acquire, try_acquire() and try_acquire_for() functions")
int32_t wait(uint32_t millisec = osWaitForever);
/** 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()
@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
@ -77,9 +85,44 @@ public:
@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);
/** 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:
@a osOK the token has been correctly released.
@a osErrorResource the maximum token count has been reached.
@ -98,6 +141,8 @@ public:
private:
void constructor(int32_t count, uint16_t max_count);
int32_t _wait(uint32_t millisec);
osSemaphoreId_t _id;
mbed_rtos_storage_semaphore_t _obj_mem;
};

View File

@ -148,10 +148,7 @@ osStatus Thread::terminate()
osStatus Thread::join()
{
int32_t ret = _join_sem.wait();
if (ret < 0) {
return osError;
}
_join_sem.acquire();
// The semaphore has been released so this thread is being
// 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);
// Now wait for the response;
msg_semaphore->wait();
msg_semaphore->acquire();
return message.result;
}

View File

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

View File

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

View File

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

View File

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