[En-Nut-Discussion] Semaphores

Marek Pavlu marekpavlu at mybox.cz
Tue Feb 22 17:44:28 CET 2005


Hi Matthias,

Example:

void timer_call_back(HANDLE timer, void *arg)
{
    NutSemPost((SEM*)arg);
}

THREAD(...,...){
    SEM sem;
    NutSemInit(&sem);
    NutTimerStart(1000, timer_call_back,(void*)&sem,0))==0)

    while(1){
        NutSemWait(&sem);
        ...
    }
}

Here is call NutSemWait and:

Step1: NUT_SEM_WAIT_WRITE_FIRST_BYTE(sem->value);
Step2: NUT_SEM_WAIT_WRITE_SECOND_BYTE(sem->value);

But writing to sem->value is not protected(interrupt is enabled).
I think, that can happen in moment:

Step1: NUT_SEM_WAIT_WRITE_FIRST_BYTE(sem->value);

Step2: NutTimer0Intr!!!
       -Interrupt is not in state disabled in work time tSECONDs function,
        yes???

Step2.1: NutTimer0Intr:
         -this funtion call timer_call_back(...,...), but NutSemWait also
          write to sem->value in same time!!!

Step2.2: End NutTimer0Intr...

Step3: NUT_SEM_WAIT_WRITE_SECOND_BYTE(sem->value);


If NutTimer0Intr support (nested interrupt) == (interrupt is enabled):
(I think that yes) then can happen writing to sem->value is very danger!!!


Of course in case that semaphore use only for synchronize two thread then
semaphore is very reliable...
But I think, that my modification is useful for this example and permit use
semaphore for various application...


Enjoy,
	Marek Pavlu
---
avast! Antivirus: Odchozi zprava cista.
Virova databaze (VPS): 0508-0, 21.02.2005
Testovano: 22.2.2005 11:35:27
avast! (c) copyright 2000-2003 ALWIL Software.
http://www.avast.com







More information about the En-Nut-Discussion mailing list