This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Counting and Binary semaphore, with timeout


I am having a problem getting the counting and binary semphores, with
timeout, to work for the following reason

The following code executes (in xxx::wait) (from sem_binary.cxx)
    // Set the timer _once_ outside the loop.
    self->set_timer( timeout, Cyg_Thread::TIMEOUT  );

The inline code (in thread.inl) executes the set_timer
inline void Cyg_Thread::set_timer(
    cyg_tick_count      trigger,
    cyg_reason          reason
)
{
#ifdef CYGFUN_KERNEL_THREADS_TIMER
    self()->sleep_reason = reason;
    self()->wake_reason = NONE;
    self()->timer.initialize( trigger);
#endif
}

The sleep_reason is set to TIMEOUT (as passed as the reason) in the 1st
executable line
The wake_reason is set to NONE in the 2nd executable line
The call to timer.initialize calls add_alarm (in clock.cxx)

The following line (in add_alarm) calls alarm (in thread.cxx)
        alarm->alarm(alarm, alarm->data);

The following code (in thrad.cxx) causes the wake reason to be set to
TIMEOUT because the sleep_reason was TIMEOUT
    Cyg_Thread::cyg_reason sleep_reason = thread->get_sleep_reason();
    
    switch( sleep_reason ) {
        
    case Cyg_Thread::DESTRUCT:
    case Cyg_Thread::BREAK:
    case Cyg_Thread::EXIT:
    case Cyg_Thread::NONE:
    case Cyg_Thread::WAIT:
    case Cyg_Thread::DONE:
        // Do nothing in any of these cases. Most are here to
        // keep the compiler happy.
        Cyg_Scheduler::unlock();
        CYG_REPORT_RETURN();
        return;

    case Cyg_Thread::DELAY:
        // The thread was simply delaying, unless it has been
        // woken up for some other reason, wake it now.
        thread->set_wake_reason(Cyg_Thread::DONE);
        break;

    case Cyg_Thread::TIMEOUT:
        // The thread has timed out, set the wake reason to
        // TIMEOUT and restart.
        thread->set_wake_reason(Cyg_Thread::TIMEOUT);
        break;

When we get back to the semaphore code the following check is made (these
line are from bin_sem.cxx)
    if( self->get_wake_reason() != Cyg_Thread::NONE && !state )
        result = false;

The wake_reason is TIMEOUT, so the first test is always true. I initialized
state=0 (I initialized counter in counting semaphore = 0), therefore
"result" is set to false.
Then, when the next statement is executed
    while ( !state && result ) {

the thread will not be blocked.

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]