"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/isc/alpha/include/isc/atomic.h" (7 Sep 2020, 5256 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "atomic.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 
   13 /*
   14  * This code was written based on FreeBSD's kernel source whose copyright
   15  * follows:
   16  */
   17 
   18 /*-
   19  * Copyright (c) 1998 Doug Rabson
   20  * All rights reserved.
   21  *
   22  * Redistribution and use in source and binary forms, with or without
   23  * modification, are permitted provided that the following conditions
   24  * are met:
   25  * 1. Redistributions of source code must retain the above copyright
   26  *    notice, this list of conditions and the following disclaimer.
   27  * 2. Redistributions in binary form must reproduce the above copyright
   28  *    notice, this list of conditions and the following disclaimer in the
   29  *    documentation and/or other materials provided with the distribution.
   30  *
   31  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   41  * SUCH DAMAGE.
   42  *
   43  * $FreeBSD: src/sys/alpha/include/atomic.h,v 1.18.6.1 2004/09/13 21:52:04 wilko Exp $
   44  */
   45 
   46 #ifndef ISC_ATOMIC_H
   47 #define ISC_ATOMIC_H 1
   48 
   49 #include <inttypes.h>
   50 
   51 #include <isc/platform.h>
   52 #include <isc/types.h>
   53 
   54 #ifdef ISC_PLATFORM_USEOSFASM
   55 #include <c_asm.h>
   56 
   57 #pragma intrinsic(asm)
   58 
   59 /*
   60  * This routine atomically increments the value stored in 'p' by 'val', and
   61  * returns the previous value.  Memory access ordering around this function
   62  * can be critical, so we add explicit memory block instructions at the
   63  * beginning and the end of it (same for other functions).
   64  */
   65 static inline int32_t
   66 isc_atomic_xadd(int32_t *p, int32_t val) {
   67     return (asm("mb;"
   68             "1:"
   69             "ldl_l %t0, 0(%a0);"    /* load old value */
   70             "mov %t0, %v0;"     /* copy the old value */
   71             "addl %t0, %a1, %t0;"   /* calculate new value */
   72             "stl_c %t0, 0(%a0);"    /* attempt to store */
   73             "beq %t0, 1b;"      /* spin if failed */
   74             "mb;",
   75             p, val));
   76 }
   77 
   78 /*
   79  * This routine atomically stores the value 'val' in 'p'.
   80  */
   81 static inline void
   82 isc_atomic_store(int32_t *p, int32_t val) {
   83     (void)asm("mb;"
   84           "1:"
   85           "ldl_l %t0, 0(%a0);"      /* load old value */
   86           "mov %a1, %t0;"       /* value to store */
   87           "stl_c %t0, 0(%a0);"      /* attempt to store */
   88           "beq %t0, 1b;"        /* spin if failed */
   89           "mb;",
   90           p, val);
   91 }
   92 
   93 /*
   94  * This routine atomically replaces the value in 'p' with 'val', if the
   95  * original value is equal to 'cmpval'.  The original value is returned in any
   96  * case.
   97  */
   98 static inline int32_t
   99 isc_atomic_cmpxchg(int32_t *p, int32_t cmpval, int32_t val) {
  100 
  101     return(asm("mb;"
  102            "1:"
  103            "ldl_l %t0, 0(%a0);"     /* load old value */
  104            "mov %t0, %v0;"      /* copy the old value */
  105            "cmpeq %t0, %a1, %t0;"   /* compare */
  106            "beq %t0, 2f;"       /* exit if not equal */
  107            "mov %a2, %t0;"      /* value to store */
  108            "stl_c %t0, 0(%a0);"     /* attempt to store */
  109            "beq %t0, 1b;"       /* if it failed, spin */
  110            "2:"
  111            "mb;",
  112            p, cmpval, val));
  113 }
  114 #elif defined (ISC_PLATFORM_USEGCCASM)
  115 static inline int32_t
  116 isc_atomic_xadd(int32_t *p, int32_t val) {
  117     int32_t temp, prev;
  118 
  119     __asm__ volatile(
  120         "mb;"
  121         "1:"
  122         "ldl_l %0, %1;"         /* load old value */
  123         "mov %0, %2;"           /* copy the old value */
  124         "addl %0, %3, %0;"      /* calculate new value */
  125         "stl_c %0, %1;"         /* attempt to store */
  126         "beq %0, 1b;"           /* spin if failed */
  127         "mb;"
  128         : "=&r"(temp), "+m"(*p), "=&r"(prev)
  129         : "r"(val)
  130         : "memory");
  131 
  132     return (prev);
  133 }
  134 
  135 static inline void
  136 isc_atomic_store(int32_t *p, int32_t val) {
  137     int32_t temp;
  138 
  139     __asm__ volatile(
  140         "mb;"
  141         "1:"
  142         "ldl_l %0, %1;"         /* load old value */
  143         "mov %2, %0;"           /* value to store */
  144         "stl_c %0, %1;"         /* attempt to store */
  145         "beq %0, 1b;"           /* if it failed, spin */
  146         "mb;"
  147         : "=&r"(temp), "+m"(*p)
  148         : "r"(val)
  149         : "memory");
  150 }
  151 
  152 static inline int32_t
  153 isc_atomic_cmpxchg(int32_t *p, int32_t cmpval, int32_t val) {
  154     int32_t temp, prev;
  155 
  156     __asm__ volatile(
  157         "mb;"
  158         "1:"
  159         "ldl_l %0, %1;"         /* load old value */
  160         "mov %0, %2;"           /* copy the old value */
  161         "cmpeq %0, %3, %0;"     /* compare */
  162         "beq %0, 2f;"           /* exit if not equal */
  163         "mov %4, %0;"           /* value to store */
  164         "stl_c %0, %1;"         /* attempt to store */
  165         "beq %0, 1b;"           /* if it failed, spin */
  166         "2:"
  167         "mb;"
  168         : "=&r"(temp), "+m"(*p), "=&r"(prev)
  169         : "r"(cmpval), "r"(val)
  170         : "memory");
  171 
  172     return (prev);
  173 }
  174 #else
  175 
  176 #error "unsupported compiler.  disable atomic ops by --disable-atomic"
  177 
  178 #endif
  179 
  180 #endif /* ISC_ATOMIC_H */