public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] [Semaphore]Set_timer in Cyg_Counting_Semaphore::wait at cnt_sem.cxx
@ 2009-03-19 13:26 PATTAYA
  2009-04-03  9:55 ` PATTAYA
  0 siblings, 1 reply; 3+ messages in thread
From: PATTAYA @ 2009-03-19 13:26 UTC (permalink / raw)
  To: ecos-discuss

Dear developers,

	I have a question about usage of timer in 
cyg_bool Cyg_Counting_Semaphore::wait( cyg_tick_count timeout ).
If timeout is larger than current time and count is larger than 0, eCos
still set timer.
Is it necessary? Why not skip setting timer in this case for better
performance?

Actually, the self->set_timer will consume around 80us in my current ARM
platform.
Following is original source with my modification.
I have a brief test and the result is passed.
It seems workable and has a better performance.

Regards,
PATTAYA



cyg_bool
Cyg_Counting_Semaphore::wait( cyg_tick_count timeout )
{
    cyg_bool result = true;
    Cyg_Thread *self = Cyg_Thread::self();

/*** PATTAYA_add_begin ***/
    cyg_bool skip_timer = false;
/*** PATTAYA_add_end ***/

    // Prevent preemption
    Cyg_Scheduler::lock();

    CYG_INSTRUMENT_CNTSEM( CLAIM, this, count );

/*** PATTAYA_add_begin ***/
    if (count > 0)
    {
        // count > 0 , no need to set up timer.
        skip_timer = true;
    }
    else
    {
/*** PATTAYA_add_end ***/

        // Set the timer _once_ outside the loop.
        self->set_timer( timeout, Cyg_Thread::TIMEOUT  );

        // If the timeout is in the past, the wake reason will have been
        // set to something other than NONE already. If the count is zero,
        // set the result false to force an immediate return. If the count
        // is non-zero, then this wait will succeed anyway.

        if( self->get_wake_reason() != Cyg_Thread::NONE &&
            0 == count )
            result = false;

/*** PATTAYA_add_begin ***/
    }
/*** PATTAYA_add_end ***/
            
    while ( 0 == count && result ) {

        // must reset the sleep reason every time
        self->set_sleep_reason( Cyg_Thread::TIMEOUT );

        self->sleep();

        queue.enqueue( self );

        CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 );

        // Allow other threads to run
        Cyg_Scheduler::reschedule();

        CYG_INSTRUMENT_CNTSEM( WOKE, this, count );

        switch( self->get_wake_reason() )
        {
        case Cyg_Thread::TIMEOUT:
            result = false;
            CYG_INSTRUMENT_CNTSEM( TIMEOUT, this, count);
            break;

        case Cyg_Thread::DESTRUCT:
        case Cyg_Thread::BREAK:
            result = false;
            break;
            
        case Cyg_Thread::EXIT:            
            self->exit();
            break;

        default:
            break;
        }
    }

    // Clear the timeout. It is irrelevant whether the alarm has
    // actually gone off or not.

/*** PATTAYA_add_begin ***/
    if (skip_timer == false)
/*** PATTAYA_add_end ***/

        self->clear_timer();
        
    if ( result ) count--;

    // Unlock the scheduler and maybe switch threads
    Cyg_Scheduler::unlock();

    return result;
}






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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [ECOS] [Semaphore]Set_timer in Cyg_Counting_Semaphore::wait at cnt_sem.cxx
  2009-03-19 13:26 [ECOS] [Semaphore]Set_timer in Cyg_Counting_Semaphore::wait at cnt_sem.cxx PATTAYA
@ 2009-04-03  9:55 ` PATTAYA
  0 siblings, 0 replies; 3+ messages in thread
From: PATTAYA @ 2009-04-03  9:55 UTC (permalink / raw)
  To: ecos-discuss

Anyone can help me???

-----Original Message-----
From: ecos-discuss-owner@ecos.sourceware.org
[mailto:ecos-discuss-owner@ecos.sourceware.org] On Behalf Of PATTAYA
Sent: Thursday, March 19, 2009 6:02 PM
To: ecos-discuss@sourceware.org
Subject: [ECOS] [Semaphore]Set_timer in Cyg_Counting_Semaphore::wait at
cnt_sem.cxx

Dear developers,

	I have a question about usage of timer in 
cyg_bool Cyg_Counting_Semaphore::wait( cyg_tick_count timeout ).
If timeout is larger than current time and count is larger than 0, eCos
still set timer.
Is it necessary? Why not skip setting timer in this case for better
performance?

Actually, the self->set_timer will consume around 80us in my current ARM
platform.
Following is original source with my modification.
I have a brief test and the result is passed.
It seems workable and has a better performance.

Regards,
PATTAYA



cyg_bool
Cyg_Counting_Semaphore::wait( cyg_tick_count timeout )
{
    cyg_bool result = true;
    Cyg_Thread *self = Cyg_Thread::self();

/*** PATTAYA_add_begin ***/
    cyg_bool skip_timer = false;
/*** PATTAYA_add_end ***/

    // Prevent preemption
    Cyg_Scheduler::lock();

    CYG_INSTRUMENT_CNTSEM( CLAIM, this, count );

/*** PATTAYA_add_begin ***/
    if (count > 0)
    {
        // count > 0 , no need to set up timer.
        skip_timer = true;
    }
    else
    {
/*** PATTAYA_add_end ***/

        // Set the timer _once_ outside the loop.
        self->set_timer( timeout, Cyg_Thread::TIMEOUT  );

        // If the timeout is in the past, the wake reason will have been
        // set to something other than NONE already. If the count is zero,
        // set the result false to force an immediate return. If the count
        // is non-zero, then this wait will succeed anyway.

        if( self->get_wake_reason() != Cyg_Thread::NONE &&
            0 == count )
            result = false;

/*** PATTAYA_add_begin ***/
    }
/*** PATTAYA_add_end ***/
            
    while ( 0 == count && result ) {

        // must reset the sleep reason every time
        self->set_sleep_reason( Cyg_Thread::TIMEOUT );

        self->sleep();

        queue.enqueue( self );

        CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 );

        // Allow other threads to run
        Cyg_Scheduler::reschedule();

        CYG_INSTRUMENT_CNTSEM( WOKE, this, count );

        switch( self->get_wake_reason() )
        {
        case Cyg_Thread::TIMEOUT:
            result = false;
            CYG_INSTRUMENT_CNTSEM( TIMEOUT, this, count);
            break;

        case Cyg_Thread::DESTRUCT:
        case Cyg_Thread::BREAK:
            result = false;
            break;
            
        case Cyg_Thread::EXIT:            
            self->exit();
            break;

        default:
            break;
        }
    }

    // Clear the timeout. It is irrelevant whether the alarm has
    // actually gone off or not.

/*** PATTAYA_add_begin ***/
    if (skip_timer == false)
/*** PATTAYA_add_end ***/

        self->clear_timer();
        
    if ( result ) count--;

    // Unlock the scheduler and maybe switch threads
    Cyg_Scheduler::unlock();

    return result;
}






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


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ECOS] [Semaphore]Set_timer in Cyg_Counting_Semaphore::wait at cnt_sem.cxx
@ 2009-03-19 10:47 PattayaTsao
  0 siblings, 0 replies; 3+ messages in thread
From: PattayaTsao @ 2009-03-19 10:47 UTC (permalink / raw)
  To: ecos-discuss

Dear developers,

	I have a question about usage of timer in 
cyg_bool Cyg_Counting_Semaphore::wait( cyg_tick_count timeout ).
If timeout is larger than current time and count is larger than 0, eCos
still set timer.
Is it necessary? Why not skip setting timer in this case for better
performance?

Actually, the self->set_timer will consume around 80us in my current ARM
platform.
Following is original source with my modification.
I have a brief test and the result is passed.
It seems workable and has a better performance.

Regards,
PATTAYA



cyg_bool
Cyg_Counting_Semaphore::wait( cyg_tick_count timeout )
{
    cyg_bool result = true;
    Cyg_Thread *self = Cyg_Thread::self();

/*** PATTAYA_add_begin ***/
    cyg_bool skip_timer = false;
/*** PATTAYA_add_end ***/

    // Prevent preemption
    Cyg_Scheduler::lock();

    CYG_INSTRUMENT_CNTSEM( CLAIM, this, count );

/*** PATTAYA_add_begin ***/
    if (count > 0)
    {
        // count > 0 , no need to set up timer.
        skip_timer = true;
    }
    else
    {
/*** PATTAYA_add_end ***/

        // Set the timer _once_ outside the loop.
        self->set_timer( timeout, Cyg_Thread::TIMEOUT  );

        // If the timeout is in the past, the wake reason will have been
        // set to something other than NONE already. If the count is zero,
        // set the result false to force an immediate return. If the count
        // is non-zero, then this wait will succeed anyway.

        if( self->get_wake_reason() != Cyg_Thread::NONE &&
            0 == count )
            result = false;

/*** PATTAYA_add_begin ***/
    }
/*** PATTAYA_add_end ***/
            
    while ( 0 == count && result ) {

        // must reset the sleep reason every time
        self->set_sleep_reason( Cyg_Thread::TIMEOUT );

        self->sleep();

        queue.enqueue( self );

        CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 );

        // Allow other threads to run
        Cyg_Scheduler::reschedule();

        CYG_INSTRUMENT_CNTSEM( WOKE, this, count );

        switch( self->get_wake_reason() )
        {
        case Cyg_Thread::TIMEOUT:
            result = false;
            CYG_INSTRUMENT_CNTSEM( TIMEOUT, this, count);
            break;

        case Cyg_Thread::DESTRUCT:
        case Cyg_Thread::BREAK:
            result = false;
            break;
            
        case Cyg_Thread::EXIT:            
            self->exit();
            break;

        default:
            break;
        }
    }

    // Clear the timeout. It is irrelevant whether the alarm has
    // actually gone off or not.

/*** PATTAYA_add_begin ***/
    if (skip_timer == false)
/*** PATTAYA_add_end ***/

        self->clear_timer();
        
    if ( result ) count--;

    // Unlock the scheduler and maybe switch threads
    Cyg_Scheduler::unlock();

    return result;
}






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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-04-03  9:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-19 13:26 [ECOS] [Semaphore]Set_timer in Cyg_Counting_Semaphore::wait at cnt_sem.cxx PATTAYA
2009-04-03  9:55 ` PATTAYA
  -- strict thread matches above, loose matches on Subject: below --
2009-03-19 10:47 PattayaTsao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).