xf86atomic.h revision 87bf8e7c
122944501Smrg/* 222944501Smrg * Copyright © 2009 Intel Corporation 322944501Smrg * 422944501Smrg * Permission is hereby granted, free of charge, to any person obtaining a 522944501Smrg * copy of this software and associated documentation files (the "Software"), 622944501Smrg * to deal in the Software without restriction, including without limitation 722944501Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 822944501Smrg * and/or sell copies of the Software, and to permit persons to whom the 922944501Smrg * Software is furnished to do so, subject to the following conditions: 1022944501Smrg * 1122944501Smrg * The above copyright notice and this permission notice (including the next 1222944501Smrg * paragraph) shall be included in all copies or substantial portions of the 1322944501Smrg * Software. 1422944501Smrg * 1522944501Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1622944501Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1722944501Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1822944501Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1922944501Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2022944501Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 2122944501Smrg * IN THE SOFTWARE. 2222944501Smrg * 2322944501Smrg * Authors: 2422944501Smrg * Chris Wilson <chris@chris-wilson.co.uk> 2522944501Smrg * 2622944501Smrg */ 2722944501Smrg 2822944501Smrg/** 2922944501Smrg * @file xf86atomics.h 3022944501Smrg * 3122944501Smrg * Private definitions for atomic operations 3222944501Smrg */ 3322944501Smrg 3422944501Smrg#ifndef LIBDRM_ATOMICS_H 3522944501Smrg#define LIBDRM_ATOMICS_H 3622944501Smrg 3722944501Smrg#if HAVE_LIBDRM_ATOMIC_PRIMITIVES 3822944501Smrg 3922944501Smrg#define HAS_ATOMIC_OPS 1 4022944501Smrg 4122944501Smrgtypedef struct { 4222944501Smrg int atomic; 4322944501Smrg} atomic_t; 4422944501Smrg 4522944501Smrg# define atomic_read(x) ((x)->atomic) 4622944501Smrg# define atomic_set(x, val) ((x)->atomic = (val)) 4722944501Smrg# define atomic_inc(x) ((void) __sync_fetch_and_add (&(x)->atomic, 1)) 48424e9256Smrg# define atomic_inc_return(x) (__sync_add_and_fetch (&(x)->atomic, 1)) 49424e9256Smrg# define atomic_dec_and_test(x) (__sync_add_and_fetch (&(x)->atomic, -1) == 0) 5022944501Smrg# define atomic_add(x, v) ((void) __sync_add_and_fetch(&(x)->atomic, (v))) 5122944501Smrg# define atomic_dec(x, v) ((void) __sync_sub_and_fetch(&(x)->atomic, (v))) 5222944501Smrg# define atomic_cmpxchg(x, oldv, newv) __sync_val_compare_and_swap (&(x)->atomic, oldv, newv) 5322944501Smrg 5422944501Smrg#endif 5522944501Smrg 5622944501Smrg#if HAVE_LIB_ATOMIC_OPS 5787bf8e7cSmrg#define AO_REQUIRE_CAS 5822944501Smrg#include <atomic_ops.h> 5922944501Smrg 6022944501Smrg#define HAS_ATOMIC_OPS 1 6122944501Smrg 6222944501Smrgtypedef struct { 6322944501Smrg AO_t atomic; 6422944501Smrg} atomic_t; 6522944501Smrg 6622944501Smrg# define atomic_read(x) AO_load_full(&(x)->atomic) 6722944501Smrg# define atomic_set(x, val) AO_store_full(&(x)->atomic, (val)) 6822944501Smrg# define atomic_inc(x) ((void) AO_fetch_and_add1_full(&(x)->atomic)) 69424e9256Smrg# define atomic_inc_return(x) (AO_fetch_and_add1_full(&(x)->atomic) + 1) 7022944501Smrg# define atomic_add(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, (v))) 7122944501Smrg# define atomic_dec(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, -(v))) 7222944501Smrg# define atomic_dec_and_test(x) (AO_fetch_and_sub1_full(&(x)->atomic) == 1) 7322944501Smrg# define atomic_cmpxchg(x, oldv, newv) AO_compare_and_swap_full(&(x)->atomic, oldv, newv) 7422944501Smrg 7522944501Smrg#endif 7622944501Smrg 77424e9256Smrg#if (defined(__sun) || defined(__NetBSD__)) && !defined(HAS_ATOMIC_OPS) /* Solaris & OpenSolaris & NetBSD */ 7822944501Smrg 7922944501Smrg#include <sys/atomic.h> 8022944501Smrg#define HAS_ATOMIC_OPS 1 8122944501Smrg 82424e9256Smrg#if defined(__NetBSD__) 83a536a66cSriastradh#define LIBDRM_ATOMIC_TYPE unsigned int 84424e9256Smrg#else 85424e9256Smrg#define LIBDRM_ATOMIC_TYPE uint_t 8622944501Smrg#endif 8722944501Smrg 88bd95bb6fSriastradhtypedef struct { volatile LIBDRM_ATOMIC_TYPE atomic; } atomic_t; 89cdb439dfSmrg 90cdb439dfSmrg# define atomic_read(x) (int) ((x)->atomic) 91424e9256Smrg# define atomic_set(x, val) ((x)->atomic = (LIBDRM_ATOMIC_TYPE)(val)) 92cdb439dfSmrg# define atomic_inc(x) (atomic_inc_uint (&(x)->atomic)) 93c2de7496Smrg# define atomic_inc_return(x) (atomic_inc_uint_nv(&(x)->atomic)) 946f15ca90Sriastradh# define atomic_dec_and_test(x) (atomic_dec_uint_nv(&(x)->atomic) == 0) 95cdb439dfSmrg# define atomic_add(x, v) (atomic_add_int(&(x)->atomic, (v))) 96cdb439dfSmrg# define atomic_dec(x, v) (atomic_add_int(&(x)->atomic, -(v))) 97cdb439dfSmrg# define atomic_cmpxchg(x, oldv, newv) atomic_cas_uint (&(x)->atomic, oldv, newv) 98cdb439dfSmrg 99cdb439dfSmrg#endif 100cdb439dfSmrg 1012b90624aSmrg#if !defined(HAS_ATOMIC_OPS) 10222944501Smrg#error libdrm requires atomic operations, please define them for your CPU/compiler. 10322944501Smrg#endif 10422944501Smrg 105a884aba1Smrgstatic inline int atomic_add_unless(atomic_t *v, int add, int unless) 106a884aba1Smrg{ 107a884aba1Smrg int c, old; 108a884aba1Smrg c = atomic_read(v); 109a884aba1Smrg while (c != unless && (old = atomic_cmpxchg(v, c, c + add)) != c) 110a884aba1Smrg c = old; 111a884aba1Smrg return c == unless; 112a884aba1Smrg} 113a884aba1Smrg 11422944501Smrg#endif 115