Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

Atomic compare_and_swap function

Hi all,

According the header file (D:\altera5\kits\nios2_50\bin\eclipse\plugins\com.microtronix.nios2linux.uClibc_1.4.0\include\bits\atomicity.h),

the implemented function is only STUB (not really atomic!).

Does anyone know how to do it really atomic?

I thought that the simplest way is to disable the interrupt when doing this function and re-enable the interrupt afterwards.

So I need to know how to enable/disable interrupt ??

Regards,

Kwok Wong

5 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    originally posted by kwokwong@Nov 2 2006, 10:14 PM

    hi all,

    according the header file (d:\altera5\kits\nios2_50\bin\eclipse\plugins\com.microtronix.nios2linux.uclibc_1.4.0\include\bits\atomicity.h),

    the implemented function is only stub (not really atomic!).

    does anyone know how to do it really atomic?

    i thought that the simplest way is to disable the interrupt when doing this function and re-enable the interrupt afterwards.

    so i need to know how to enable/disable interrupt ??

    regards,

    kwok wong

    <div align='right'><{post_snapback}> (index.php?act=findpost&pid=19104)

    --- quote end ---

    --- Quote End ---

    It is not implemented, so we need a nios2 version in,

    uClibc/libc/sysdeps/linux/nios2/bits/atomicity.h

    Look at other arch, eg, arm,m68k,mips,i386, they use locked exchange instead of disable interrupt.

    Are there locked instructions in Nios2?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    originally posted by hippo+nov 2 2006, 11:52 pm--><div class='quotetop'>quote (hippo @ nov 2 2006, 11:52 pm)</div>

    --- quote start ---

    <!--quotebegin-kwokwong@Nov 2 2006, 10:14 PM

    hi all,

    according the header file (d:\altera5\kits\nios2_50\bin\eclipse\plugins\com.microtronix.nios2linux.uclibc_1.4.0\include\bits\atomicity.h),

    the implemented function is only stub (not really atomic!).

    does anyone know how to do it really atomic?

    i thought that the simplest way is to disable the interrupt when doing this function and re-enable the interrupt afterwards.

    so i need to know how to enable/disable interrupt ??

    regards,

    kwok wong

    <div align='right'><{post_snapback}> (index.php?act=findpost&pid=19104)

    --- quote end ---

    --- Quote End ---

    It is not implemented, so we need a nios2 version in,

    uClibc/libc/sysdeps/linux/nios2/bits/atomicity.h

    Look at other arch, eg, arm,m68k,mips,i386, they use locked exchange instead of disable interrupt.

    Are there locked instructions in Nios2?

    <div align='right'><{post_snapback}> (index.php?act=findpost&pid=19108)</div>

    [/b]

    --- Quote End ---

    $ find uClibc -name "atomic*.h"

    uClibc/libc/sysdeps/linux/sh/bits/atomic.h

    uClibc/libc/sysdeps/linux/arm/bits/atomicity.h

    uClibc/libc/sysdeps/linux/i386/bits/atomic.h

    uClibc/libc/sysdeps/linux/i386/bits/atomicity.h

    uClibc/libc/sysdeps/linux/m68k/bits/atomicity.h

    uClibc/libc/sysdeps/linux/mips/bits/atomic.h

    uClibc/libc/sysdeps/linux/mips/bits/atomicity.h

    uClibc/libc/sysdeps/linux/alpha/bits/atomic.h

    uClibc/libc/sysdeps/linux/alpha/bits/atomicity.h

    uClibc/libc/sysdeps/linux/h8300/bits/atomicity.h

    uClibc/libc/sysdeps/linux/sparc/bits/atomicity.h

    uClibc/libc/sysdeps/linux/common/bits/atomic.h

    uClibc/libc/sysdeps/linux/common/bits/atomicity.h

    uClibc/libc/sysdeps/linux/x86_64/bits/atomic.h

    uClibc/libc/sysdeps/linux/x86_64/bits/atomicity.h

    uClibc/libc/sysdeps/linux/powerpc/bits/atomic.h

    uClibc/libc/sysdeps/linux/powerpc/bits/atomicity.h

    uClibc/include/bits/atomic.h

    uClibc/include/bits/atomicity.h

    uClibc/include/atomic.h

    If we create uClibc/libc/sysdeps/linux/nios2/bits/atomicity.h ,then it will become uClibc/include/bits/atomicity.h . Otherwise, it will come from uClibc/libc/sysdeps/linux/common/bits/atomicity.h, which is a stub.

    If nios2 does not have lock, then we can use disable/enable interrupr, with inline assembly.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Look in include/asm (ie, asm-nios2nommu) /system.h

    Use,

    local_irq_save(x) // to disable global irq

    then,

    local_irq_restore(x) // to enable/restore global irq
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    originally posted by hippo@Nov 7 2006, 12:47 AM

    look in include/asm (ie, asm-nios2nommu) /system.h

    use,

    local_irq_save(x) // to disable global irq

    then,

    local_irq_restore(x) // to enable/restore global irq

    <div align='right'><{post_snapback}> (index.php?act=findpost&pid=19239)

    --- quote end ---

    --- Quote End ---

    Thank you, hippo!

    F.Y.I.

    Use the macro COMPARE_AND_SWAP in stead of the function compare_and_swap.

    E.g.

    static inline int __compare_and_swap(volatile int *p, int old_val, int new_val)

    {

    int result;

    unsigned long int_context;

    /* save int. and disable all interrupts */

    local_irq_save(int_context);

    result = compare_and_swap((volatile long*)p, old_val, new_val);

    /* re-enable interrupt */

    local_irq_restore(int_context);

    return result;

    }

    # define COMPARE_AND_SWAP(addr, old_val, new_val)

    __compare_and_swap(addr, old_val, new_val) http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif