"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/isc/sparc64/include/isc/atomic.h" (7 Sep 2020, 3671 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  * Copyright (c) 2001 Jake Burkholder.
   21  * All rights reserved.
   22  *
   23  * Redistribution and use in source and binary forms, with or without
   24  * modification, are permitted provided that the following conditions
   25  * are met:
   26  * 1. Redistributions of source code must retain the above copyright
   27  *    notice, this list of conditions and the following disclaimer.
   28  * 2. Redistributions in binary form must reproduce the above copyright
   29  *    notice, this list of conditions and the following disclaimer in the
   30  *    documentation and/or other materials provided with the distribution.
   31  *
   32  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   42  * SUCH DAMAGE.
   43  *
   44  *  from: FreeBSD: src/sys/i386/include/atomic.h,v 1.20 2001/02/11
   45  * $FreeBSD: src/sys/sparc64/include/atomic.h,v 1.8 2004/05/22 00:52:16 marius Exp $
   46  */
   47 
   48 #ifndef ISC_ATOMIC_H
   49 #define ISC_ATOMIC_H 1
   50 
   51 #include <inttypes.h>
   52 
   53 #include <isc/platform.h>
   54 #include <isc/types.h>
   55 
   56 #define ASI_P   0x80        /* Primary Address Space Identifier */
   57 
   58 #ifdef ISC_PLATFORM_USEGCCASM
   59 
   60 /*
   61  * This routine atomically increments the value stored in 'p' by 'val', and
   62  * returns the previous value.
   63  */
   64 static inline int32_t
   65 isc_atomic_xadd(int32_t *p, int32_t val) {
   66     int32_t prev, swapped;
   67 
   68     for (prev = *(volatile int32_t *)p; ; prev = swapped) {
   69         swapped = prev + val;
   70         __asm__ volatile(
   71             "casa [%2] %3, %4, %0"
   72             : "+r"(swapped), "=m"(*p)
   73             : "r"(p), "n"(ASI_P), "r"(prev), "m"(*p));
   74         if (swapped == prev)
   75             break;
   76     }
   77 
   78     return (prev);
   79 }
   80 
   81 /*
   82  * This routine atomically stores the value 'val' in 'p'.
   83  */
   84 static inline void
   85 isc_atomic_store(int32_t *p, int32_t val) {
   86     int32_t prev, swapped;
   87 
   88     for (prev = *(volatile int32_t *)p; ; prev = swapped) {
   89         swapped = val;
   90         __asm__ volatile(
   91             "casa [%2] %3, %4, %0"
   92             : "+r"(swapped), "=m"(*p)
   93             : "r"(p), "n"(ASI_P), "r"(prev), "m"(*p));
   94         if (swapped == prev)
   95             break;
   96     }
   97 }
   98 
   99 /*
  100  * This routine atomically replaces the value in 'p' with 'val', if the
  101  * original value is equal to 'cmpval'.  The original value is returned in any
  102  * case.
  103  */
  104 static inline int32_t
  105 isc_atomic_cmpxchg(int32_t *p, int32_t cmpval, int32_t val) {
  106     int32_t temp = val;
  107 
  108     __asm__ volatile(
  109         "casa [%2] %3, %4, %0"
  110         : "+r"(temp), "=m"(*p)
  111         : "r"(p), "n"(ASI_P), "r"(cmpval), "m"(*p));
  112 
  113     return (temp);
  114 }
  115 
  116 #else  /* ISC_PLATFORM_USEGCCASM */
  117 
  118 #error "unsupported compiler.  disable atomic ops by --disable-atomic"
  119 
  120 #endif /* ISC_PLATFORM_USEGCCASM */
  121 
  122 #endif /* ISC_ATOMIC_H */