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]

Re: Atomic operations


>>>>> "Boris" == Boris V Guzhov <borg@int.spb.ru> writes:

    Boris> Hi All!
    Boris> My target is pc386. There is a question.
    Boris> I want to make an atomic a little code sequence in a thread.
    Boris> For example:
    Boris> static int resource;

    Boris> if ( resource <0) {
    Boris>   return -1;
    Boris> }
    Boris> resource --;

    Boris> The resource variable can vary in other threads and in in interrupt
    Boris> handlers.

    Boris> I can use cyg_sheduler_(un)lock() or a mutex etc. But I
    Boris> think that these solutions are expensive for little code
    Boris> sequences.

    Boris> Is there in eCos a something similar to simple CPU masking?

This looks to me like a trywait() operation on a counting semaphore,
which is provided by the kernel.

Obviously the kernel implementation makes use of cyg_scheduler_lock()
and cyg_scheduler_unlock() internally (there is no need for a mutex).
However these are actually relatively cheap operations. Unless you are
in a critical path and it is borderline whether or not your
application is going to meet hard real-time constraints, it is not
usually worth worrying about the cost of cyg_scheduler_lock().

If you do need to go faster, you can try disabling and enabling
interrupts using cyg_interrupt_disable() and cyg_interrupt_enable().
Since there are only a few instructions involved for code like this,
it is unlikely that the maximum interrupt latency will be affected.

Alternatively there might be some x86 instructions which will do the
right thing without having to disable interrupts, and which you could
invoke using some inline assembler. It has been quite a while since I
did any x86 assembler programming, I cannot help there.

Bart Veer // eCos net maintainer

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