ff2d95be3d5e0350c5b457582cdb08869fc17789Tinderbox User * Copyright (C) 2005, 2007, 2008, 2015-2017 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
cbf0854acc9f5d11142dba30b1ab23e0532baaf2Automatic Updater/* $Id: atomic.h,v 1.10 2008/01/24 23:47:00 tbox Exp $ */
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * This routine atomically increments the value stored in 'p' by 'val', and
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * returns the previous value.
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉isc_atomic_xadd(isc_int32_t *p, isc_int32_t val) {
1c3ed2a83d176d9023b51b60dfc96c133f678362Tatuya JINMEI 神明達哉isc_atomic_xaddq(isc_int64_t *p, isc_int64_t val) {
1c3ed2a83d176d9023b51b60dfc96c133f678362Tatuya JINMEI 神明達哉 "xaddq %0, %1"
1c3ed2a83d176d9023b51b60dfc96c133f678362Tatuya JINMEI 神明達哉#endif /* ISC_PLATFORM_HAVEXADDQ */
821ff5e8fa1df2e09fea157ee3e298eef6cf4ec9Evan Hunt * This routine atomically stores the value 'val' in 'p' (32-bit version).
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉isc_atomic_store(isc_int32_t *p, isc_int32_t val) {
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * xchg should automatically lock memory, but we add it
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * explicitly just in case (it at least doesn't harm)
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 "xchgl %1, %0"
821ff5e8fa1df2e09fea157ee3e298eef6cf4ec9Evan Hunt * This routine atomically stores the value 'val' in 'p' (64-bit version).
821ff5e8fa1df2e09fea157ee3e298eef6cf4ec9Evan Huntisc_atomic_storeq(isc_int64_t *p, isc_int64_t val) {
821ff5e8fa1df2e09fea157ee3e298eef6cf4ec9Evan Hunt * xchg should automatically lock memory, but we add it
821ff5e8fa1df2e09fea157ee3e298eef6cf4ec9Evan Hunt * explicitly just in case (it at least doesn't harm)
821ff5e8fa1df2e09fea157ee3e298eef6cf4ec9Evan Hunt "xchgq %1, %0"
821ff5e8fa1df2e09fea157ee3e298eef6cf4ec9Evan Hunt : "memory");
821ff5e8fa1df2e09fea157ee3e298eef6cf4ec9Evan Hunt#endif /* ISC_PLATFORM_HAVEATOMICSTOREQ */
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * This routine atomically replaces the value in 'p' with 'val', if the
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * original value is equal to 'cmpval'. The original value is returned in any
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉isc_atomic_cmpxchg(isc_int32_t *p, isc_int32_t cmpval, isc_int32_t val) {
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 "cmpxchgl %1, %2"
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 * The followings are "generic" assembly code which implements the same
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 * functionality in case the gcc extension cannot be used. It should be
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 * better to avoid inlining below, since we directly refer to specific
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 * positions of the stack frame, which would not actually point to the
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 * intended address in the embedded mnemonic.
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉isc_atomic_xadd(isc_int32_t *p, isc_int32_t val) {
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 "movl 8(%ebp), %ecx\n"
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 "movl 12(%ebp), %edx\n"
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 "xadd %edx, (%ecx)\n"
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 * set the return value directly in the register so that we
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 * can avoid guessing the correct position in the stack for a
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 * local variable.
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 "movl %edx, %eax"
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉isc_atomic_store(isc_int32_t *p, isc_int32_t val) {
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 "movl 8(%ebp), %ecx\n"
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 "movl 12(%ebp), %edx\n"
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 "xchgl (%ecx), %edx\n"
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉isc_atomic_cmpxchg(isc_int32_t *p, isc_int32_t cmpval, isc_int32_t val) {
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 "movl 8(%ebp), %ecx\n"
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 "movl 12(%ebp), %eax\n" /* must be %eax for cmpxchgl */
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 "movl 16(%ebp), %edx\n"
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 * If (%ecx) == %eax then (%ecx) := %edx.
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 % %eax is set to old (%ecx), which will be the return value.
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉 "cmpxchgl %edx, (%ecx)"
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉#else /* !ISC_PLATFORM_USEGCCASM && !ISC_PLATFORM_USESTDASM */
c528bd698637d84a0081d26a58813607c7f52bb7Tatuya JINMEI 神明達哉#error "unsupported compiler. disable atomic ops by --disable-atomic"
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉#endif /* ISC_ATOMIC_H */