xf86atomic.h revision cdb439df
11.168Sthorpej/* 21.48Sitojun * Copyright © 2009 Intel Corporation 31.48Sitojun * 41.48Sitojun * Permission is hereby granted, free of charge, to any person obtaining a 51.48Sitojun * copy of this software and associated documentation files (the "Software"), 61.94Sitojun * to deal in the Software without restriction, including without limitation 71.48Sitojun * the rights to use, copy, modify, merge, publish, distribute, sublicense, 81.48Sitojun * and/or sell copies of the Software, and to permit persons to whom the 91.48Sitojun * Software is furnished to do so, subject to the following conditions: 101.48Sitojun * 111.48Sitojun * The above copyright notice and this permission notice (including the next 121.48Sitojun * paragraph) shall be included in all copies or substantial portions of the 131.48Sitojun * Software. 141.48Sitojun * 151.48Sitojun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 161.48Sitojun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 171.48Sitojun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 181.94Sitojun * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 191.48Sitojun * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 201.48Sitojun * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 211.48Sitojun * IN THE SOFTWARE. 221.48Sitojun * 231.48Sitojun * Authors: 241.48Sitojun * Chris Wilson <chris@chris-wilson.co.uk> 251.48Sitojun * 261.48Sitojun */ 271.48Sitojun 281.48Sitojun/** 291.48Sitojun * @file xf86atomics.h 301.48Sitojun * 311.14Scgd * Private definitions for atomic operations 321.1Scgd */ 331.44Sthorpej 341.13Smycroft#ifndef LIBDRM_ATOMICS_H 351.1Scgd#define LIBDRM_ATOMICS_H 361.1Scgd 371.1Scgd#ifdef HAVE_CONFIG_H 381.1Scgd#include "config.h" 391.1Scgd#endif 401.1Scgd 411.1Scgd#if HAVE_LIBDRM_ATOMIC_PRIMITIVES 421.1Scgd 431.1Scgd#define HAS_ATOMIC_OPS 1 441.104Sagc 451.1Scgdtypedef struct { 461.1Scgd int atomic; 471.1Scgd} atomic_t; 481.1Scgd 491.1Scgd# define atomic_read(x) ((x)->atomic) 501.1Scgd# define atomic_set(x, val) ((x)->atomic = (val)) 511.1Scgd# define atomic_inc(x) ((void) __sync_fetch_and_add (&(x)->atomic, 1)) 521.1Scgd# define atomic_dec_and_test(x) (__sync_fetch_and_add (&(x)->atomic, -1) == 1) 531.1Scgd# define atomic_add(x, v) ((void) __sync_add_and_fetch(&(x)->atomic, (v))) 541.1Scgd# define atomic_dec(x, v) ((void) __sync_sub_and_fetch(&(x)->atomic, (v))) 551.1Scgd# define atomic_cmpxchg(x, oldv, newv) __sync_val_compare_and_swap (&(x)->atomic, oldv, newv) 561.1Scgd 571.1Scgd#endif 581.1Scgd 591.1Scgd#if HAVE_LIB_ATOMIC_OPS 601.44Sthorpej#include <atomic_ops.h> 611.1Scgd 621.91Slukem#define HAS_ATOMIC_OPS 1 631.91Slukem 641.168Sthorpejtypedef struct { 651.50Sthorpej AO_t atomic; 661.77Ssoda} atomic_t; 671.50Sthorpej 681.78Sthorpej# define atomic_read(x) AO_load_full(&(x)->atomic) 691.64Sws# define atomic_set(x, val) AO_store_full(&(x)->atomic, (val)) 701.101Smartin# define atomic_inc(x) ((void) AO_fetch_and_add1_full(&(x)->atomic)) 711.1Scgd# define atomic_add(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, (v))) 721.5Smycroft# define atomic_dec(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, -(v))) 731.5Smycroft# define atomic_dec_and_test(x) (AO_fetch_and_sub1_full(&(x)->atomic) == 1) 741.5Smycroft# define atomic_cmpxchg(x, oldv, newv) AO_compare_and_swap_full(&(x)->atomic, oldv, newv) 751.5Smycroft 761.5Smycroft#endif 771.5Smycroft 781.13Smycroft#if defined(__sun) && !defined(HAS_ATOMIC_OPS) /* Solaris & OpenSolaris */ 791.5Smycroft 801.27Schristos#include <sys/atomic.h> 811.27Schristos#define HAS_ATOMIC_OPS 1 821.53Sitojun 831.27Schristostypedef struct { uint_t atomic; } atomic_t; 841.1Scgd 851.5Smycroft# define atomic_read(x) (int) ((x)->atomic) 861.5Smycroft# define atomic_set(x, val) ((x)->atomic = (uint_t)(val)) 871.1Scgd# define atomic_inc(x) (atomic_inc_uint (&(x)->atomic)) 881.5Smycroft# define atomic_dec_and_test(x) (atomic_dec_uint_nv(&(x)->atomic) == 1) 891.5Smycroft# define atomic_add(x, v) (atomic_add_uint(&(x)->atomic, (v))) 901.15Scgd# define atomic_dec(x, v) (atomic_dec_uint(&(x)->atomic, (v))) 911.5Smycroft# define atomic_cmpxchg(x, oldv, newv) atomic_cas_uint (&(x)->atomic, oldv, newv) 921.5Smycroft 931.5Smycroft#endif 941.5Smycroft 951.5Smycroft#if defined(__NetBSD__) && !defined(HAS_ATOMIC_OPS) /* NetBSD */ 961.5Smycroft 971.166Sthorpej#include <sys/atomic.h> 981.1Scgd#define HAS_ATOMIC_OPS 1 991.53Sitojun 1001.53Sitojuntypedef struct { int atomic; } atomic_t; 1011.53Sitojun 1021.53Sitojun# define atomic_read(x) (int) ((x)->atomic) 1031.167Sthorpej# define atomic_set(x, val) ((x)->atomic = (val)) 1041.53Sitojun# define atomic_inc(x) (atomic_inc_uint (&(x)->atomic)) 1051.53Sitojun# define atomic_dec_and_test(x) (atomic_dec_uint_nv(&(x)->atomic) == 1) 1061.168Sthorpej# define atomic_add(x, v) (atomic_add_int(&(x)->atomic, (v))) 1071.146Srpaulo# define atomic_dec(x, v) (atomic_add_int(&(x)->atomic, -(v))) 1081.53Sitojun# define atomic_cmpxchg(x, oldv, newv) atomic_cas_uint (&(x)->atomic, oldv, newv) 1091.53Sitojun 1101.53Sitojun#endif 1111.53Sitojun 1121.53Sitojun#if ! HAS_ATOMIC_OPS 1131.53Sitojun#error libdrm requires atomic operations, please define them for your CPU/compiler. 1141.53Sitojun#endif 1151.76Sitojun 1161.76Sitojun#endif 1171.76Sitojun