Hung up with a semaphore (Shared resources)

08 Sep 2010 . Edited: 08 Sep 2010

I read a suggestion for a semaphore in http://mbed.org/forum/mbed/topic/181/#comment-799

I think that's great idea for guard a shared resources between interrupt and general thread.

But it make hung up.

How do I guard shared resources?

The program is TestProgramForBugTopic1074

08 Sep 2010

My guess - without fully understanding how the semaphore class is implemented - is that it's possible for a byte to arrive on the serial input while the printf() statement is executing. This would cause func_serial_interrupt() to attempt to take the semaphore - and presumably wait until it can. However, the semaphore cannot be released because the serial interrupt has control of the CPU, i.e., the printf() is never able to finish executing.

This explanation would be feasible if the program only occasionally hangs. You should be able to fix this by using take(false) instead of take() to avoid blocking - although the program may require some other changes.

08 Sep 2010

tja, i just realised the problem at my end.
steven is right!
what's needed is a try_enter and in case it is successfull the operation can be completed.

08 Sep 2010

class Semaphore {
public:
Semaphore(): s(SemFree) {}

bool try_enter() {
int oldval = __ldrex(&s);
if (oldval == SemTaken)
{
return false;
}

__strex(SemTaken, &s);

return true;
}

void release() {
__strex(SemFree, &s);
s = SemFree;
}

private:
enum { SemFree, SemTaken };
volatile int s;
};

usage:

if ( lock.try_enter()) {
if ( !buffer.bufferFull()) {
buffer.putElement(t_period);
toggle_led( dataled);
lock.release();
} else {
toggle_led( rubbishled);
}
t1.reset();                         // Reset timer and wait for next interrupt
}

open for comments.

09 Sep 2010

Dear Mr.Steven Blair

Thank you for your comments.

Yeah. You are right.

The function are consist from many instruction codes.

In this case, I took 2 problems.

  • Serial function broken. (See a updated program : TestProgramForBugTopic1074)
  • Ticker function broken. (I don't know how can I create the situation now.)

I think it's a bug of the mbed library.

 

And ...

Dear Mr.robert bouwens

Thank you for your practical advices.

 

Best regards,

Shin. :)

09 Sep 2010

dear mr. shin,

no not a bug within mbed - inpropper use of semaphore ;-)
when an interrupt happens and the main loop has successfully aquired a semphore you will find yourself in an uncomfortable situation.

but for me it was a starting point - realizing that the mbed platform has it limits.
it would be great it there would be a simple scheduler with semaphores.

regards

robert

09 Sep 2010
user avatar robert bouwens wrote:
it would be great it there would be a simple scheduler with semaphores.

Seems like we are doing a merry dance around the semaphore; see the thread here. Various people have mentioned simple schedulers but I've not seen any code - anybody care to share?

Regards
Daniel

09 Sep 2010

Dear Mr.Robert Bouwens

Thank you for your messages.

I understood it.

Dear Mr.Daniel Peter

I found a simple scheduler at http://mbed.org/users/Stanislaus/programs/Scheduler/latest .

Regards

Shin.

09 Sep 2010

dear mr. shin,

my mbed stuff is on my desk at home.
i'm just looking how to create simple things.

but in essence - not so simple ;-)
i noticed a few things in my own program.
but the keyboard type test is nice. i'm just decoding the ddc signal using mbed hw to learn...

today there is no play time.
so expect my response somewhat later.

regards

robert

09 Sep 2010

user avatar Daniel Peter wrote:
user avatar robert bouwens wrote:
it would be great it there would be a simple scheduler with semaphores.

Seems like we are doing a merry dance around the semaphore; see the thread here. Various people have mentioned simple schedulers but I've not seen any code - anybody care to share?

Regards
Daniel

hi daniel,

i looked at the code - rather minimalistic and a shame.
the license is abs. wrong - the gpl license forces the resulting derivative work to be published in source code!
the same issue as openocd has with the ftdi libs.

but it would be great to see it as opensource :)
the other dance should be solved in a 'few' hours of work.
the serial stuff has also a few problems to be solved.

regards

robert

14 Sep 2010

user avatar Shinichiro Nakamura wrote:

Dear Mr.Robert Bouwens

Thank you for your messages.

I understood it.

Dear Mr.Daniel Peter

I found a simple scheduler at http://mbed.org/users/Stanislaus/programs/Scheduler/latest .

Regards

Shin.

ok, http://mbed.org/users/roberto_b/programs/TestSemaphoreProgram/latest

i should stop writing sw late in the evening ;)

regards

robert

14 Sep 2010

Dear Mr. Robert-san

Oh. It's a great sample program.

regards

Shin. :)