trylock inside event callback not working?

31 Jan 2018

Why does trylock always return 1 in the following code? I thought interruptIn.fall with a callback to eventqueue made it possible to use mutexs?

My example

#include "mbed.h"

static EventQueue buttonQueue(/* event count */ 8 * EVENTS_EVENT_SIZE);
Thread button_thread(osPriorityLow);

InterruptIn btn(p20);

Mutex btn_m;

void button_finnish()
{
    // do some stuff

    printf("Button is finnished");
    btn_m.unlock();

}
void button_press()
{
    if (!btn_m.trylock()) { // ERROR trylock always return 1
        printf("Button is already pressed");
        return;
    }

    printf("DEBUG trylock should now be false / 0 : %d ",(int)btn_m.trylock());

    printf("Button  is  pressed");

    buttonQueue.call_in(5000, button_finnish);
    // do some stuff
    printf("Disabling button for 5s (mutex is locked)");
}

int main()
{

    btn.fall(buttonQueue.event(button_press));
    button_thread.start(callback(&buttonQueue, &EventQueue::dispatch_forever));

}