xf86atomic.h revision a536a66c
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))
52424e9256Smrg# define atomic_inc_return(x) (__sync_add_and_fetch (&(x)->atomic, 1))
53424e9256Smrg# define atomic_dec_and_test(x) (__sync_add_and_fetch (&(x)->atomic, -1) == 0)
5422944501Smrg# define atomic_add(x, v) ((void) __sync_add_and_fetch(&(x)->atomic, (v)))
5522944501Smrg# define atomic_dec(x, v) ((void) __sync_sub_and_fetch(&(x)->atomic, (v)))
5622944501Smrg# define atomic_cmpxchg(x, oldv, newv) __sync_val_compare_and_swap (&(x)->atomic, oldv, newv)
5722944501Smrg
5822944501Smrg#endif
5922944501Smrg
6022944501Smrg#if HAVE_LIB_ATOMIC_OPS
6122944501Smrg#include <atomic_ops.h>
6222944501Smrg
6322944501Smrg#define HAS_ATOMIC_OPS 1
6422944501Smrg
6522944501Smrgtypedef struct {
6622944501Smrg	AO_t atomic;
6722944501Smrg} atomic_t;
6822944501Smrg
6922944501Smrg# define atomic_read(x) AO_load_full(&(x)->atomic)
7022944501Smrg# define atomic_set(x, val) AO_store_full(&(x)->atomic, (val))
7122944501Smrg# define atomic_inc(x) ((void) AO_fetch_and_add1_full(&(x)->atomic))
72424e9256Smrg# define atomic_inc_return(x) (AO_fetch_and_add1_full(&(x)->atomic) + 1)
7322944501Smrg# define atomic_add(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, (v)))
7422944501Smrg# define atomic_dec(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, -(v)))
7522944501Smrg# define atomic_dec_and_test(x) (AO_fetch_and_sub1_full(&(x)->atomic) == 1)
7622944501Smrg# define atomic_cmpxchg(x, oldv, newv) AO_compare_and_swap_full(&(x)->atomic, oldv, newv)
7722944501Smrg
7822944501Smrg#endif
7922944501Smrg
80424e9256Smrg#if (defined(__sun) || defined(__NetBSD__)) && !defined(HAS_ATOMIC_OPS)  /* Solaris & OpenSolaris & NetBSD */
8122944501Smrg
8222944501Smrg#include <sys/atomic.h>
8322944501Smrg#define HAS_ATOMIC_OPS 1
8422944501Smrg
85424e9256Smrg#if defined(__NetBSD__)
86a536a66cSriastradh#define LIBDRM_ATOMIC_TYPE unsigned int
87424e9256Smrg#else
88424e9256Smrg#define LIBDRM_ATOMIC_TYPE uint_t
8922944501Smrg#endif
9022944501Smrg
91424e9256Smrgtypedef struct { LIBDRM_ATOMIC_TYPE atomic; } atomic_t;
92cdb439dfSmrg
93cdb439dfSmrg# define atomic_read(x) (int) ((x)->atomic)
94424e9256Smrg# define atomic_set(x, val) ((x)->atomic = (LIBDRM_ATOMIC_TYPE)(val))
95cdb439dfSmrg# define atomic_inc(x) (atomic_inc_uint (&(x)->atomic))
96c2de7496Smrg# define atomic_inc_return(x) (atomic_inc_uint_nv(&(x)->atomic))
976f15ca90Sriastradh# define atomic_dec_and_test(x) (atomic_dec_uint_nv(&(x)->atomic) == 0)
98cdb439dfSmrg# define atomic_add(x, v) (atomic_add_int(&(x)->atomic, (v)))
99cdb439dfSmrg# define atomic_dec(x, v) (atomic_add_int(&(x)->atomic, -(v)))
100cdb439dfSmrg# define atomic_cmpxchg(x, oldv, newv) atomic_cas_uint (&(x)->atomic, oldv, newv)
101cdb439dfSmrg
102cdb439dfSmrg#endif
103cdb439dfSmrg
10422944501Smrg#if ! HAS_ATOMIC_OPS
10522944501Smrg#error libdrm requires atomic operations, please define them for your CPU/compiler.
10622944501Smrg#endif
10722944501Smrg
108a884aba1Smrgstatic inline int atomic_add_unless(atomic_t *v, int add, int unless)
109a884aba1Smrg{
110a884aba1Smrg	int c, old;
111a884aba1Smrg	c = atomic_read(v);
112a884aba1Smrg	while (c != unless && (old = atomic_cmpxchg(v, c, c + add)) != c)
113a884aba1Smrg		c = old;
114a884aba1Smrg	return c == unless;
115a884aba1Smrg}
116a884aba1Smrg
11722944501Smrg#endif
118