Test the thread switching time by implementing semaphore ping-pong between two threads.

Measure thread switching time by implementing semaphore ping-pong between two threads

This mbed program starts two threads with realtime-priority. Each of them is waiting for the other to give its semaphore and gives the other thread's semaphore as a reaction. Once started, this algorithm results in fast thread switches, enforced by the semaphores.

To allow measurement, one thread sets a GPIO output, while to other one clears it.

Measurements are performed on a NUCLEO-F767ZI board https://os.mbed.com/platforms/ST-Nucleo-F767ZI/.

mbed-os-5.12.3

Measured gpio_out output frequency, whitch contains actually two thread switches:

  • 6.5 - 7.0 usec (154 kHz - 143 kHz)

/media/uploads/stefanwaldschmidt/2019-05-10-semaphore_ping_pong.gif

Committer:
stefanwaldschmidt
Date:
Sat May 11 13:58:02 2019 +0000
Revision:
0:ea879e5f3ce5
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stefanwaldschmidt 0:ea879e5f3ce5 1 /*
stefanwaldschmidt 0:ea879e5f3ce5 2 Test thread switching time using semaphore ping-pong
stefanwaldschmidt 0:ea879e5f3ce5 3
stefanwaldschmidt 0:ea879e5f3ce5 4 Hardware: NUCLEO-F767ZI
stefanwaldschmidt 0:ea879e5f3ce5 5 Libraries:
stefanwaldschmidt 0:ea879e5f3ce5 6 mbed-os-5.12.3
stefanwaldschmidt 0:ea879e5f3ce5 7 Measured gpio_out output frequency,
stefanwaldschmidt 0:ea879e5f3ce5 8 which contains actually two thread switches:
stefanwaldschmidt 0:ea879e5f3ce5 9 6.5 - 7.0 usec (154 kHz - 143 kHz)
stefanwaldschmidt 0:ea879e5f3ce5 10 */
stefanwaldschmidt 0:ea879e5f3ce5 11
stefanwaldschmidt 0:ea879e5f3ce5 12 #include "mbed.h"
stefanwaldschmidt 0:ea879e5f3ce5 13
stefanwaldschmidt 0:ea879e5f3ce5 14 Semaphore semaphore_on(0);
stefanwaldschmidt 0:ea879e5f3ce5 15 Semaphore semaphore_off(0);
stefanwaldschmidt 0:ea879e5f3ce5 16
stefanwaldschmidt 0:ea879e5f3ce5 17 DigitalOut gpio_out(PC_8);
stefanwaldschmidt 0:ea879e5f3ce5 18
stefanwaldschmidt 0:ea879e5f3ce5 19 void run_on()
stefanwaldschmidt 0:ea879e5f3ce5 20 {
stefanwaldschmidt 0:ea879e5f3ce5 21 while (true) {
stefanwaldschmidt 0:ea879e5f3ce5 22 semaphore_on.wait();
stefanwaldschmidt 0:ea879e5f3ce5 23 gpio_out = 1;
stefanwaldschmidt 0:ea879e5f3ce5 24 semaphore_off.release();
stefanwaldschmidt 0:ea879e5f3ce5 25 }
stefanwaldschmidt 0:ea879e5f3ce5 26 }
stefanwaldschmidt 0:ea879e5f3ce5 27
stefanwaldschmidt 0:ea879e5f3ce5 28 void run_off()
stefanwaldschmidt 0:ea879e5f3ce5 29 {
stefanwaldschmidt 0:ea879e5f3ce5 30 while (true) {
stefanwaldschmidt 0:ea879e5f3ce5 31 semaphore_off.wait();
stefanwaldschmidt 0:ea879e5f3ce5 32 gpio_out = 0;
stefanwaldschmidt 0:ea879e5f3ce5 33 semaphore_on.release();
stefanwaldschmidt 0:ea879e5f3ce5 34 }
stefanwaldschmidt 0:ea879e5f3ce5 35 }
stefanwaldschmidt 0:ea879e5f3ce5 36
stefanwaldschmidt 0:ea879e5f3ce5 37 int main()
stefanwaldschmidt 0:ea879e5f3ce5 38 {
stefanwaldschmidt 0:ea879e5f3ce5 39 Thread *thread_on = new Thread(osPriorityRealtime);
stefanwaldschmidt 0:ea879e5f3ce5 40 Thread *thread_off = new Thread(osPriorityRealtime);
stefanwaldschmidt 0:ea879e5f3ce5 41
stefanwaldschmidt 0:ea879e5f3ce5 42 thread_on->start(run_on);
stefanwaldschmidt 0:ea879e5f3ce5 43 thread_off->start(run_off);
stefanwaldschmidt 0:ea879e5f3ce5 44
stefanwaldschmidt 0:ea879e5f3ce5 45 semaphore_on.release();
stefanwaldschmidt 0:ea879e5f3ce5 46
stefanwaldschmidt 0:ea879e5f3ce5 47 while (true) {
stefanwaldschmidt 0:ea879e5f3ce5 48 wait(0.1); // 100 ns
stefanwaldschmidt 0:ea879e5f3ce5 49 }
stefanwaldschmidt 0:ea879e5f3ce5 50 }