atomic.h revision 01b8bc018d83e757b0578723977b0a71e1e626f8
5cd4555ad444fd391002ae32450572054369fd42Rob Austein * Copyright (C) 2005 Internet Systems Consortium, Inc. ("ISC")
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * Permission to use, copy, modify, and distribute this software for any
d2bdd5b314d3ee2250c740fe5fff8b91ab3731b2Tinderbox User * purpose with or without fee is hereby granted, provided that the above
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * copyright notice and this permission notice appear in all copies.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * PERFORMANCE OF THIS SOFTWARE.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews/* $Id: atomic.h,v 1.4 2005/07/27 04:20:43 marka Exp $ */
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews * This routine atomically increments the value stored in 'p' by 'val', and
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews * returns the previous value.
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewsisc_atomic_xadd(isc_int32_t *p, isc_int32_t val) {
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews "xadd %0, %1"
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews * This routine atomically stores the value 'val' in 'p'.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsstatic inline void
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsisc_atomic_store(isc_int32_t *p, isc_int32_t val) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * xchg should automatically lock memory, but we add it
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * explicitly just in case (it at least doesn't harm)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "xchgl %1, %0"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein : "memory");
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * This routine atomically replaces the value in 'p' with 'val', if the
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * original value is equal to 'cmpval'. The original value is returned in any
8077efca7d2ec3b9bf0428386a1ec2fcbcdf437bAutomatic Updaterisc_atomic_cmpxchg(isc_int32_t *p, isc_int32_t cmpval, isc_int32_t val) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "cmpxchgl %1, %2"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein : "memory");
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * The followings are "generic" assembly code which implements the same
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * functionality in case the gcc extension cannot be used. It should be
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * better to avoid inlining below, since we directly refer to specific
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * positions of the stack frame, which would not actually point to the
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * intended address in the embedded mnemonic.
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinisc_atomic_xadd(isc_int32_t *p, isc_int32_t val) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "movl 8(%ebp), %ecx\n"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "movl 12(%ebp), %edx\n"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "xadd %edx, (%ecx)\n"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * set the return value directly in the register so that we
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * can avoid guessing the correct position in the stack for a
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * local variable.
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "movl %edx, %eax"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinisc_atomic_store(isc_int32_t *p, isc_int32_t val) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "movl 8(%ebp), %ecx\n"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "movl 12(%ebp), %edx\n"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "xchgl (%ecx), %edx\n"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinisc_atomic_cmpxchg(isc_int32_t *p, isc_int32_t cmpval, isc_int32_t val) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "movl 8(%ebp), %ecx\n"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "movl 12(%ebp), %eax\n" /* must be %eax for cmpxchgl */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "movl 16(%ebp), %edx\n"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * If (%ecx) == %eax then (%ecx) := %edx.
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein % %eax is set to old (%ecx), which will be the return value.
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "cmpxchgl %edx, (%ecx)"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#else /* !ISC_PLATFORM_USEGCCASM && !ISC_PLATFORM_USESTDASM */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#error "unsupported compiler. disable atomic ops by --disable-atomic"
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#endif /* ISC_ATOMIC_H */