xf86atomic.h revision a884aba1
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#ifdef HAVE_CONFIG_H 3822944501Smrg#include "config.h" 3922944501Smrg#endif 4022944501Smrg 4122944501Smrg#if HAVE_LIBDRM_ATOMIC_PRIMITIVES 4222944501Smrg 4322944501Smrg#define HAS_ATOMIC_OPS 1 4422944501Smrg 4522944501Smrgtypedef struct { 4622944501Smrg int atomic; 4722944501Smrg} atomic_t; 4822944501Smrg 4922944501Smrg# define atomic_read(x) ((x)->atomic) 5022944501Smrg# define atomic_set(x, val) ((x)->atomic = (val)) 5122944501Smrg# define atomic_inc(x) ((void) __sync_fetch_and_add (&(x)->atomic, 1)) 5222944501Smrg# define atomic_dec_and_test(x) (__sync_fetch_and_add (&(x)->atomic, -1) == 1) 5322944501Smrg# define atomic_add(x, v) ((void) __sync_add_and_fetch(&(x)->atomic, (v))) 5422944501Smrg# define atomic_dec(x, v) ((void) __sync_sub_and_fetch(&(x)->atomic, (v))) 5522944501Smrg# define atomic_cmpxchg(x, oldv, newv) __sync_val_compare_and_swap (&(x)->atomic, oldv, newv) 5622944501Smrg 5722944501Smrg#endif 5822944501Smrg 5922944501Smrg#if HAVE_LIB_ATOMIC_OPS 6022944501Smrg#include <atomic_ops.h> 6122944501Smrg 6222944501Smrg#define HAS_ATOMIC_OPS 1 6322944501Smrg 6422944501Smrgtypedef struct { 6522944501Smrg AO_t atomic; 6622944501Smrg} atomic_t; 6722944501Smrg 6822944501Smrg# define atomic_read(x) AO_load_full(&(x)->atomic) 6922944501Smrg# define atomic_set(x, val) AO_store_full(&(x)->atomic, (val)) 7022944501Smrg# define atomic_inc(x) ((void) AO_fetch_and_add1_full(&(x)->atomic)) 7122944501Smrg# define atomic_add(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, (v))) 7222944501Smrg# define atomic_dec(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, -(v))) 7322944501Smrg# define atomic_dec_and_test(x) (AO_fetch_and_sub1_full(&(x)->atomic) == 1) 7422944501Smrg# define atomic_cmpxchg(x, oldv, newv) AO_compare_and_swap_full(&(x)->atomic, oldv, newv) 7522944501Smrg 7622944501Smrg#endif 7722944501Smrg 7822944501Smrg#if defined(__sun) && !defined(HAS_ATOMIC_OPS) /* Solaris & OpenSolaris */ 7922944501Smrg 8022944501Smrg#include <sys/atomic.h> 8122944501Smrg#define HAS_ATOMIC_OPS 1 8222944501Smrg 8322944501Smrgtypedef struct { uint_t atomic; } atomic_t; 8422944501Smrg 8522944501Smrg# define atomic_read(x) (int) ((x)->atomic) 8622944501Smrg# define atomic_set(x, val) ((x)->atomic = (uint_t)(val)) 8722944501Smrg# define atomic_inc(x) (atomic_inc_uint (&(x)->atomic)) 886f15ca90Sriastradh# define atomic_dec_and_test(x) (atomic_dec_uint_nv(&(x)->atomic) == 0) 89aaba2545Smrg# define atomic_add(x, v) (atomic_add_int(&(x)->atomic, (v))) 90aaba2545Smrg# define atomic_dec(x, v) (atomic_add_int(&(x)->atomic, -(v))) 9122944501Smrg# define atomic_cmpxchg(x, oldv, newv) atomic_cas_uint (&(x)->atomic, oldv, newv) 9222944501Smrg 9322944501Smrg#endif 9422944501Smrg 95cdb439dfSmrg#if defined(__NetBSD__) && !defined(HAS_ATOMIC_OPS) /* NetBSD */ 96cdb439dfSmrg 97cdb439dfSmrg#include <sys/atomic.h> 98cdb439dfSmrg#define HAS_ATOMIC_OPS 1 99cdb439dfSmrg 100cdb439dfSmrgtypedef struct { int atomic; } atomic_t; 101cdb439dfSmrg 102cdb439dfSmrg# define atomic_read(x) (int) ((x)->atomic) 103cdb439dfSmrg# define atomic_set(x, val) ((x)->atomic = (val)) 104cdb439dfSmrg# define atomic_inc(x) (atomic_inc_uint (&(x)->atomic)) 1056f15ca90Sriastradh# define atomic_dec_and_test(x) (atomic_dec_uint_nv(&(x)->atomic) == 0) 106cdb439dfSmrg# define atomic_add(x, v) (atomic_add_int(&(x)->atomic, (v))) 107cdb439dfSmrg# define atomic_dec(x, v) (atomic_add_int(&(x)->atomic, -(v))) 108cdb439dfSmrg# define atomic_cmpxchg(x, oldv, newv) atomic_cas_uint (&(x)->atomic, oldv, newv) 109cdb439dfSmrg 110cdb439dfSmrg#endif 111cdb439dfSmrg 11222944501Smrg#if ! HAS_ATOMIC_OPS 11322944501Smrg#error libdrm requires atomic operations, please define them for your CPU/compiler. 11422944501Smrg#endif 11522944501Smrg 116a884aba1Smrgstatic inline int atomic_add_unless(atomic_t *v, int add, int unless) 117a884aba1Smrg{ 118a884aba1Smrg int c, old; 119a884aba1Smrg c = atomic_read(v); 120a884aba1Smrg while (c != unless && (old = atomic_cmpxchg(v, c, c + add)) != c) 121a884aba1Smrg c = old; 122a884aba1Smrg return c == unless; 123a884aba1Smrg} 124a884aba1Smrg 12522944501Smrg#endif 126