Back to index

libdrm  2.4.37
xf86atomic.h
Go to the documentation of this file.
00001 /*
00002  * Copyright © 2009 Intel Corporation
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice (including the next
00012  * paragraph) shall be included in all copies or substantial portions of the
00013  * Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00020  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
00021  * IN THE SOFTWARE.
00022  *
00023  * Authors:
00024  *    Chris Wilson <chris@chris-wilson.co.uk>
00025  *
00026  */
00027 
00034 #ifndef LIBDRM_ATOMICS_H
00035 #define LIBDRM_ATOMICS_H
00036 
00037 #ifdef HAVE_CONFIG_H
00038 #include "config.h"
00039 #endif
00040 
00041 #if HAVE_LIBDRM_ATOMIC_PRIMITIVES
00042 
00043 #define HAS_ATOMIC_OPS 1
00044 
00045 typedef struct {
00046        int atomic;
00047 } atomic_t;
00048 
00049 # define atomic_read(x) ((x)->atomic)
00050 # define atomic_set(x, val) ((x)->atomic = (val))
00051 # define atomic_inc(x) ((void) __sync_fetch_and_add (&(x)->atomic, 1))
00052 # define atomic_dec_and_test(x) (__sync_fetch_and_add (&(x)->atomic, -1) == 1)
00053 # define atomic_add(x, v) ((void) __sync_add_and_fetch(&(x)->atomic, (v)))
00054 # define atomic_dec(x, v) ((void) __sync_sub_and_fetch(&(x)->atomic, (v)))
00055 # define atomic_cmpxchg(x, oldv, newv) __sync_val_compare_and_swap (&(x)->atomic, oldv, newv)
00056 
00057 #endif
00058 
00059 #if HAVE_LIB_ATOMIC_OPS
00060 #include <atomic_ops.h>
00061 
00062 #define HAS_ATOMIC_OPS 1
00063 
00064 typedef struct {
00065        AO_t atomic;
00066 } atomic_t;
00067 
00068 # define atomic_read(x) AO_load_full(&(x)->atomic)
00069 # define atomic_set(x, val) AO_store_full(&(x)->atomic, (val))
00070 # define atomic_inc(x) ((void) AO_fetch_and_add1_full(&(x)->atomic))
00071 # define atomic_add(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, (v)))
00072 # define atomic_dec(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, -(v)))
00073 # define atomic_dec_and_test(x) (AO_fetch_and_sub1_full(&(x)->atomic) == 1)
00074 # define atomic_cmpxchg(x, oldv, newv) AO_compare_and_swap_full(&(x)->atomic, oldv, newv)
00075 
00076 #endif
00077 
00078 #if defined(__sun) && !defined(HAS_ATOMIC_OPS)  /* Solaris & OpenSolaris */
00079 
00080 #include <sys/atomic.h>
00081 #define HAS_ATOMIC_OPS 1
00082 
00083 typedef struct { uint_t atomic; } atomic_t;
00084 
00085 # define atomic_read(x) (int) ((x)->atomic)
00086 # define atomic_set(x, val) ((x)->atomic = (uint_t)(val))
00087 # define atomic_inc(x) (atomic_inc_uint (&(x)->atomic))
00088 # define atomic_dec_and_test(x) (atomic_dec_uint_nv(&(x)->atomic) == 1)
00089 # define atomic_add(x, v) (atomic_add_int(&(x)->atomic, (v)))
00090 # define atomic_dec(x, v) (atomic_add_int(&(x)->atomic, -(v)))
00091 # define atomic_cmpxchg(x, oldv, newv) atomic_cas_uint (&(x)->atomic, oldv, newv)
00092 
00093 #endif
00094 
00095 #if ! HAS_ATOMIC_OPS
00096 #error libdrm requires atomic operations, please define them for your CPU/compiler.
00097 #endif
00098 
00099 #endif