linux/arch/tile/include/asm/atomic.h
<<
>>
Prefs
   1/*
   2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
   3 *
   4 *   This program is free software; you can redistribute it and/or
   5 *   modify it under the terms of the GNU General Public License
   6 *   as published by the Free Software Foundation, version 2.
   7 *
   8 *   This program is distributed in the hope that it will be useful, but
   9 *   WITHOUT ANY WARRANTY; without even the implied warranty of
  10 *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11 *   NON INFRINGEMENT.  See the GNU General Public License for
  12 *   more details.
  13 *
  14 * Atomic primitives.
  15 */
  16
  17#ifndef _ASM_TILE_ATOMIC_H
  18#define _ASM_TILE_ATOMIC_H
  19
  20#include <asm/cmpxchg.h>
  21
  22#ifndef __ASSEMBLY__
  23
  24#include <linux/compiler.h>
  25#include <linux/types.h>
  26
  27#define ATOMIC_INIT(i)  { (i) }
  28
  29/**
  30 * atomic_read - read atomic variable
  31 * @v: pointer of type atomic_t
  32 *
  33 * Atomically reads the value of @v.
  34 */
  35static inline int atomic_read(const atomic_t *v)
  36{
  37        return READ_ONCE(v->counter);
  38}
  39
  40/**
  41 * atomic_sub_return - subtract integer and return
  42 * @v: pointer of type atomic_t
  43 * @i: integer value to subtract
  44 *
  45 * Atomically subtracts @i from @v and returns @v - @i
  46 */
  47#define atomic_sub_return(i, v)         atomic_add_return((int)(-(i)), (v))
  48
  49#define atomic_fetch_sub(i, v)          atomic_fetch_add(-(int)(i), (v))
  50
  51/**
  52 * atomic_sub - subtract integer from atomic variable
  53 * @i: integer value to subtract
  54 * @v: pointer of type atomic_t
  55 *
  56 * Atomically subtracts @i from @v.
  57 */
  58#define atomic_sub(i, v)                atomic_add((int)(-(i)), (v))
  59
  60/**
  61 * atomic_sub_and_test - subtract value from variable and test result
  62 * @i: integer value to subtract
  63 * @v: pointer of type atomic_t
  64 *
  65 * Atomically subtracts @i from @v and returns true if the result is
  66 * zero, or false for all other cases.
  67 */
  68#define atomic_sub_and_test(i, v)       (atomic_sub_return((i), (v)) == 0)
  69
  70/**
  71 * atomic_inc_return - increment memory and return
  72 * @v: pointer of type atomic_t
  73 *
  74 * Atomically increments @v by 1 and returns the new value.
  75 */
  76#define atomic_inc_return(v)            atomic_add_return(1, (v))
  77
  78/**
  79 * atomic_dec_return - decrement memory and return
  80 * @v: pointer of type atomic_t
  81 *
  82 * Atomically decrements @v by 1 and returns the new value.
  83 */
  84#define atomic_dec_return(v)            atomic_sub_return(1, (v))
  85
  86/**
  87 * atomic_inc - increment atomic variable
  88 * @v: pointer of type atomic_t
  89 *
  90 * Atomically increments @v by 1.
  91 */
  92#define atomic_inc(v)                   atomic_add(1, (v))
  93
  94/**
  95 * atomic_dec - decrement atomic variable
  96 * @v: pointer of type atomic_t
  97 *
  98 * Atomically decrements @v by 1.
  99 */
 100#define atomic_dec(v)                   atomic_sub(1, (v))
 101
 102/**
 103 * atomic_dec_and_test - decrement and test
 104 * @v: pointer of type atomic_t
 105 *
 106 * Atomically decrements @v by 1 and returns true if the result is 0.
 107 */
 108#define atomic_dec_and_test(v)          (atomic_dec_return(v) == 0)
 109
 110/**
 111 * atomic_inc_and_test - increment and test
 112 * @v: pointer of type atomic_t
 113 *
 114 * Atomically increments @v by 1 and returns true if the result is 0.
 115 */
 116#define atomic_inc_and_test(v)          (atomic_inc_return(v) == 0)
 117
 118/**
 119 * atomic_xchg - atomically exchange contents of memory with a new value
 120 * @v: pointer of type atomic_t
 121 * @i: integer value to store in memory
 122 *
 123 * Atomically sets @v to @i and returns old @v
 124 */
 125static inline int atomic_xchg(atomic_t *v, int n)
 126{
 127        return xchg(&v->counter, n);
 128}
 129
 130/**
 131 * atomic_cmpxchg - atomically exchange contents of memory if it matches
 132 * @v: pointer of type atomic_t
 133 * @o: old value that memory should have
 134 * @n: new value to write to memory if it matches
 135 *
 136 * Atomically checks if @v holds @o and replaces it with @n if so.
 137 * Returns the old value at @v.
 138 */
 139static inline int atomic_cmpxchg(atomic_t *v, int o, int n)
 140{
 141        return cmpxchg(&v->counter, o, n);
 142}
 143
 144/**
 145 * atomic_add_negative - add and test if negative
 146 * @v: pointer of type atomic_t
 147 * @i: integer value to add
 148 *
 149 * Atomically adds @i to @v and returns true if the result is
 150 * negative, or false when result is greater than or equal to zero.
 151 */
 152#define atomic_add_negative(i, v)       (atomic_add_return((i), (v)) < 0)
 153
 154#endif /* __ASSEMBLY__ */
 155
 156#ifndef __tilegx__
 157#include <asm/atomic_32.h>
 158#else
 159#include <asm/atomic_64.h>
 160#endif
 161
 162#ifndef __ASSEMBLY__
 163
 164/**
 165 * atomic64_xchg - atomically exchange contents of memory with a new value
 166 * @v: pointer of type atomic64_t
 167 * @i: integer value to store in memory
 168 *
 169 * Atomically sets @v to @i and returns old @v
 170 */
 171static inline long long atomic64_xchg(atomic64_t *v, long long n)
 172{
 173        return xchg64(&v->counter, n);
 174}
 175
 176/**
 177 * atomic64_cmpxchg - atomically exchange contents of memory if it matches
 178 * @v: pointer of type atomic64_t
 179 * @o: old value that memory should have
 180 * @n: new value to write to memory if it matches
 181 *
 182 * Atomically checks if @v holds @o and replaces it with @n if so.
 183 * Returns the old value at @v.
 184 */
 185static inline long long atomic64_cmpxchg(atomic64_t *v, long long o,
 186                                        long long n)
 187{
 188        return cmpxchg64(&v->counter, o, n);
 189}
 190
 191static inline long long atomic64_dec_if_positive(atomic64_t *v)
 192{
 193        long long c, old, dec;
 194
 195        c = atomic64_read(v);
 196        for (;;) {
 197                dec = c - 1;
 198                if (unlikely(dec < 0))
 199                        break;
 200                old = atomic64_cmpxchg((v), c, dec);
 201                if (likely(old == c))
 202                        break;
 203                c = old;
 204        }
 205        return dec;
 206}
 207
 208#endif /* __ASSEMBLY__ */
 209
 210#endif /* _ASM_TILE_ATOMIC_H */
 211