Date: Tue, 26 Sep 2000 08:54:48 +1100 From: "Jan Mikkelsen" <janm@transactionsite.com> To: "Kevin Mills" <kmills@aventail.com>, "FreeBSD Hackers" <freebsd-hackers@FreeBSD.org> Subject: Re: atomic operations Message-ID: <00b701c0273b$39f7aaa0$0901a8c0@haym.transactionsite.com>
next in thread | raw e-mail | index | archive | help
Kevin Mills <kmills@aventail.com> wrote: >I found the atomic_* functions in <machine/atomic.h>, but noticed that they >have no return value. What I need is a function that increments/decrements >the given value *and* returns the new value in an atomic operation. I >suppose this is possible, yes? How would one modify the assembly to make >this work? Atomic decrement, in the Intel style: long atomic_decrement(volatile long* address) { asm { mov ecx, [address] mov eax, -1 lock xadd [ecx], eax dec eax } /* Return value in EAX */ } An untested conversion into the GNU/AT&T style: long atomic_decrement(volatile long* address) { asm("movl 8(%ebp),%ecx"); asm("movl $-1, %eax"); asm("lock xaddl %eax,(%ecx)"); asm("decl %eax"); /* Return value in %eax */ } Deriving increment is straightforward. I haven't looked at the GNU inline assembler notation for indicating register usage. I'd be curious to see what is should look like. Jan Mikkelsen To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?00b701c0273b$39f7aaa0$0901a8c0>