AW: [En-Nut-Discussion] Again Semaphore implementation

Oliver Schulz olischulz at
Wed Jun 2 21:40:43 CEST 2004

Hi Ole,

sorry to say that, but you didn't found the real bug. By enhancing the range
of SEM.value to negative numbers and decrementing the value now in the first
line of NutSemWait, you made a kind of workaround.

In fact, the bug was in NutSemPost, when for value == 0 NutEventPost was
If the semaphore is initialized with 0, the first call to NutSemPost will
also call NutEventPost. That call sets SEM.qhp to the signalled state if no
thread waits for the event.

After that, the first call to NutSemWait works like expected, but the next
call sees, that SEM.value carries 0 and calls NutEventWait. Because SEM.qhp
is still in signalled state, the call returns (nearly) without waiting and
the SEM.value is decremented again from 0 to 65535.

So I would have simply suggested to change in NutSemWait the call to
NutEventWait to NutEventWaitNext. This function ignores the signalled state
of the event queue.

But because you said your solution already works, I think we can keep it.
Just one thing: Can you please also adjust the comparison in NutSemTryWait,
so it can work appropriatly?


PS: Don't forget to change the Changelog.

> -----Ursprüngliche Nachricht-----
> Von: en-nut-discussion-bounces at
> [mailto:en-nut-discussion-bounces at]Im Auftrag von
> Ole Reinhardt
> Gesendet: Mittwoch, 2. Juni 2004 18:41
> An: Ethernut User Chat (English)
> Betreff: Re: [En-Nut-Discussion] Again Semaphore implementation
> Hi folks,
> I found and fixed the bug in the semaphore implementation.
> A semaphore initialized with zero could underrun
> (integer underflow). In the current implementation the semaphore
> value is a short (not u_short).
> The behavior in other cases should not have changed.
> Happy testing,
> Ole
> --
> kernel concepts    Tel: +49-271-771091-14
> Dreisbachstr. 24   Fax: +49-271-771091-19
> D-57250 Netphen    E+ : +49-177-7420433
> --
> _______________________________________________
> En-Nut-Discussion mailing list
> En-Nut-Discussion at

More information about the En-Nut-Discussion mailing list