diff --git a/libraries/AP_HAL/examples/BinarySem/BinarySem.cpp b/libraries/AP_HAL/examples/BinarySem/BinarySem.cpp new file mode 100644 index 0000000000..885933ded9 --- /dev/null +++ b/libraries/AP_HAL/examples/BinarySem/BinarySem.cpp @@ -0,0 +1,91 @@ +/* + test of HAL_BinarySemaphore + */ + +#include +#include + +#include + +void setup(); +void loop(); + +const AP_HAL::HAL& hal = AP_HAL::get_HAL(); + +class BinarySemTest { +public: + HAL_BinarySemaphore sem1{1}; + HAL_BinarySemaphore sem2{0}; + + void setup(void); + void thread1(void); + void thread2(void); + void update(bool ok); + + uint32_t ops, timeouts; + uint32_t last_print_us; + HAL_Semaphore mtx; +}; + +void BinarySemTest::setup(void) +{ + hal.scheduler->thread_create( + FUNCTOR_BIND_MEMBER(&BinarySemTest::thread1, void), "thd1", 2048, AP_HAL::Scheduler::PRIORITY_IO, 0); + hal.scheduler->thread_create( + FUNCTOR_BIND_MEMBER(&BinarySemTest::thread2, void), "thd2", 2048, AP_HAL::Scheduler::PRIORITY_IO, 0); + ::printf("Setup threads\n"); +} + +void BinarySemTest::thread2(void) +{ + while (true) { + bool ok = sem2.wait(50000); + sem1.signal(); + update(ok); + } +} + +void BinarySemTest::thread1(void) +{ + while (true) { + bool ok = sem1.wait(50000); + sem2.signal(); + update(ok); + } +} + +void BinarySemTest::update(bool ok) +{ + WITH_SEMAPHORE(mtx); + if (ok) { + ops++; + } else { + timeouts++; + } + uint32_t now_us = AP_HAL::micros(); + float dt = (now_us - last_print_us)*1.0e-6; + if (dt >= 1.0) { + last_print_us = now_us; + ::printf("tick %u %.3f ops/s %.3f timeouts/s\n", + unsigned(AP_HAL::millis()), + ops/dt, + timeouts/dt); + ops = 0; + timeouts = 0; + } +} + +static BinarySemTest *ct; + +void setup(void) +{ + ct = new BinarySemTest; + ct->setup(); +} + +void loop(void) +{ + hal.scheduler->delay(1000); +} + +AP_HAL_MAIN(); diff --git a/libraries/AP_HAL/examples/BinarySem/wscript b/libraries/AP_HAL/examples/BinarySem/wscript new file mode 100644 index 0000000000..719ec151ba --- /dev/null +++ b/libraries/AP_HAL/examples/BinarySem/wscript @@ -0,0 +1,7 @@ +#!/usr/bin/env python +# encoding: utf-8 + +def build(bld): + bld.ap_example( + use='ap', + )