We help IT Professionals succeed at work.

Portable Atomic operation

shiningram
shiningram asked
on
546 Views
Last Modified: 2012-08-14
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

Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
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.

Author

Commented:
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.

Commented:
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.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.