Link to home
Start Free TrialLog in
Avatar of shiningram

asked on

Portable Atomic operation

These things are very important. Can some point me neccesary changes to make it portable?
1. What type it works on ? Let me explicitly state that.
   a. Does it work on int32
   b  Does it work on uint32
   c. Does it work on int64
   d  Does it work on uint64
   e. Does it work on unsigned long?
   f. Does it work on signed long?
2. What architecture 32 or 64 bit.?
   a. Will same code work on both if not then what changes are required?
3. What is the return value (old or updated one)? It must return a value. So that old and new value can be compare later.
4. What OS it will support? If not then what changes or API to use if available.
   a. Will it work on windows 32 and 64 bit?
   b. Will it work linux 32 or 64 bit
   c. Will it work Sun Solaris
   d. will it work on HP AIX
int32 func()
   int32 val = 1;
    asm volatile ("lock; xaddl %0,%1"
              : "=r" (val), "=m" (*mem)
              : "0" (val), "m" (*mem)
              : "memory", "cc");
 return val;

Open in new window

Avatar of sdcz

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sdcz

Just a clarification - it seems that only insignificant part of libatomic_ops is under gpl, most likely you'll don't need that for your purposes, to cite from its documentation:

Note that much of the content of this library is in the header files.

However two small libraries are built and installed:

- libatomic_ops.a is a support library, which is not needed on some platforms.
  This is intended to be usable, under some mild restrictions, in free or
  proprietary code, as are all the header files.  See doc/LICENSING.txt.
- libatomic_ops_gpl.a contains some higher level facilities.  This code is
  currently covered by the GPL.  The contents currently correspond to
  the headers atomic_ops_stack.h and atomic_ops_malloc.h.

So if you need to write portable propietary code, it should be fine.
Avatar of shiningram


Thank you. I dont want to use any GPL available code. I want to write my own. So gathering the knowledge how different OS or architecture differ from each other in terms of atomic operations.
1. What value a function returns? Is it updated or old.
2. Like wise xadd is there xdec or something else available on all other platforms. If they differ then what are those opcode. As per my application i will use the underneath architecture opcode to write my own atomic ops. Can any one similar opcodes on different OSes for atomic_increment() and atomic_decrement(), atomic_subtract() and atomic_addition() functions.
1. always returns updated value. code just for atomic read is lock; mov ...
There is actually no xsub on x86. As long you stay on ia32/amd-64 you dont need different opcodes for other operating systems (only cosmetic changes if you use VC++ on windows etc).

Your code should return original value, the linux one returns after addition (it just adds the two together at the end of the function) - sorry i didn't make that clear.

2. just pass a negative value to atomic_add, and you have atomic_sub :)
int atomic_sub(int v, int *p)
   return atomic_add(-v, p);
There is no xsub opcode on x86, that would be probably the case on different cpus as well, so i'd not depend on it, negating the value before should work everywhere.