[En-Nut-Discussion] semaphore code

William Baker bbaker at priefert.com
Fri Oct 7 19:11:17 CEST 2005


I have a short program that does the following:

SEM sem;

main() {
    NutSemInit( &sem, 1 );
    printf( "t1=%d\n", NutSemTryWait( &sem );
    printf( "t2=%d\n", NutSemTryWait( &sem );
}

The output is:
t1=0;
(blocked)

Well, NutSemTryWait() is supposed to be non-blocking.  This appears to 
have been broken by rev 1.4 to nut/os/semaphore.c  It appears that 
sem->value ==  0 wasn't good enough, and sem->value < 0 appears to be 
broken.  Should this be <= ?

Revision 1.3 follows:

int NutSemTryWait(SEM * sem) {    
         if (sem->value == 0)
             return -1;
         else
             NutSemWait(sem);
    return 0;
}

Revision 1.4 follows:

int NutSemTryWait(SEM * sem) {
     if (sem->value < 0)
             return -1;
     else
        NutSemWait(sem);
    return 0;
}




More information about the En-Nut-Discussion mailing list