home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / aix / 11388 < prev    next >
Encoding:
Text File  |  1992-11-08  |  1.8 KB  |  52 lines

  1. Newsgroups: comp.unix.aix
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!torn!utzoo!utdoe!torag!jrh!jrh
  3. From: jrh@jrh.uucp (James R. Hamilton)
  4. Subject: Re: swap instruction in assembler?
  5. Message-ID: <1992Nov08.025005.194878@jrh.uucp>
  6. Organization: private system, Toronto, Ontario
  7. References: <471@bertha.HyperDesk.com>
  8. Date: Sun, 08 Nov 1992 02:50:05 GMT
  9. Lines: 41
  10.  
  11. In article <471@bertha.HyperDesk.com> andy_d@hyperdesk.com writes:
  12. >
  13. >I need to implement a semaphore-like test-and-set operation in assembler
  14. >on the RS/6000.  The functionality is: if an address contains a 0, set
  15. >it to 1 and return 1; if it contains a 1, return 0.
  16. >
  17. >Since the address is in shared memory, I need to worry about another
  18. >process getting the time slice between my test and my set, and setting
  19. >it before I get the chance to set it myself.
  20. >
  21. >What I'd like to do is:
  22. >     store 1 in register
  23. >     swap register with address
  24. >     if register contains 1, the semaphore belongs to somebody else,
  25. >          so return 0
  26. >     otherwise return 1
  27. >
  28. >My problem: I don't see a swap instruction in the manual.  Is there
  29. >another instruction that might do what I want?
  30. >
  31.     The POWER architecture doesn't implement an atomic hardware swap 
  32.     instruction.  Atomic compare and swaps are implemented via the
  33.     AIX system call cs (compare and swap).  The following will do what
  34.     you want (without leaving C):
  35.  
  36.         int *latch;
  37.             .
  38.             .
  39.             .
  40.         if (cs(latch, 0, 1) == 0 )
  41.             /* success: latch was set to 1 */
  42.         else
  43.             /* failed: latch was not changed (someone else has it) */
  44.  
  45.  
  46.     --jrh
  47. -- 
  48.  
  49. James R. Hamilton                              inet: jrh@jrh.gts.org
  50. telephone: +1 416 493 4162                     uunet: ...!uunet!jrh!jrh
  51. Toronto, Canada                                work: jrh@torolab6.vnet.ibm.com
  52.