home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.aix
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!torn!utzoo!utdoe!torag!jrh!jrh
- From: jrh@jrh.uucp (James R. Hamilton)
- Subject: Re: swap instruction in assembler?
- Message-ID: <1992Nov08.025005.194878@jrh.uucp>
- Organization: private system, Toronto, Ontario
- References: <471@bertha.HyperDesk.com>
- Date: Sun, 08 Nov 1992 02:50:05 GMT
- Lines: 41
-
- In article <471@bertha.HyperDesk.com> andy_d@hyperdesk.com writes:
- >
- >I need to implement a semaphore-like test-and-set operation in assembler
- >on the RS/6000. The functionality is: if an address contains a 0, set
- >it to 1 and return 1; if it contains a 1, return 0.
- >
- >Since the address is in shared memory, I need to worry about another
- >process getting the time slice between my test and my set, and setting
- >it before I get the chance to set it myself.
- >
- >What I'd like to do is:
- > store 1 in register
- > swap register with address
- > if register contains 1, the semaphore belongs to somebody else,
- > so return 0
- > otherwise return 1
- >
- >My problem: I don't see a swap instruction in the manual. Is there
- >another instruction that might do what I want?
- >
- The POWER architecture doesn't implement an atomic hardware swap
- instruction. Atomic compare and swaps are implemented via the
- AIX system call cs (compare and swap). The following will do what
- you want (without leaving C):
-
- int *latch;
- .
- .
- .
- if (cs(latch, 0, 1) == 0 )
- /* success: latch was set to 1 */
- else
- /* failed: latch was not changed (someone else has it) */
-
-
- --jrh
- --
-
- James R. Hamilton inet: jrh@jrh.gts.org
- telephone: +1 416 493 4162 uunet: ...!uunet!jrh!jrh
- Toronto, Canada work: jrh@torolab6.vnet.ibm.com
-