atomic increment is not working properly..

Reply

Join Date: Sep 2008
Posts: 1
Reputation: akmittal is an unknown quantity at this point 
Solved Threads: 0
akmittal akmittal is offline Offline
Newbie Poster

atomic increment is not working properly..

 
0
  #1
Sep 15th, 2008
i am new to assembly programming,can anybody help me out to solve this problem -

i have a assembly code which does the atomic increment
  1. Atomic_Add (volatile unsigned int *addr, int additive)
  2. {
  3. register unsigned int previous;
  4.  
  5. __asm__ volatile (
  6. "pushf;"
  7. "cli;"
  8. "movl (%2), %0;" /* previous = *addr; */
  9. "add %0, %1;" /* *addr = previous + additive; */
  10. "movl %1, (%2);"
  11. "popf"
  12. : "=&r"(previous)
  13. : "r"(additive), "r"(addr) : "cc");
  14. return previous;
  15. }
here if addr is allocated on the stack(local variable) this function works fine and increment the addr variable correctly by additive, but if i addr is allocated on heap or is from gss, addr is not getting incremented correctly. and one more thing, if i merge the last two instruction the code works fine for all the scenarios.
  1. Atomic_Add (volatile unsigned int *addr, int additive)
  2. {
  3. register unsigned int previous;
  4.  
  5. __asm__ volatile (
  6. "pushf;"
  7. "cli;"
  8. "movl (%2), %0;" /* previous = *addr; */
  9. "add %1, (%2);" /* *addr = previous + additive; */
  10. "popf"
  11. : "=&r"(previous)
  12. : "r"(additive), "r"(addr) : "cc");
  13. return previous;
  14. }
Last edited by cscgal; Sep 15th, 2008 at 11:24 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: atomic increment is not working properly..

 
0
  #2
Sep 15th, 2008
So is the "not working properly" related to the "add" part of the problem or the "lock" part of the problem?

cli isn't going to do anything useful at all if you have multiple cores. AFAIK, it's a "per core" thing, so disabling it on one doesn't stop the other from messing with the variable.

The use case you quote of locals good, globals bad makes it seem like a cache coherency problem. Or it could be down to out-of-order execution of instructions meaning the popf happens beforre the memory write. IIRC, the instruction to stop this is 'sync'
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Assembly Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC