blob: e53347fbf1da6bdc85aa45ef66ed1c47bdb65a08 [file] [log] [blame]
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001/*
2 * Generic C implementation of atomic counter operations
3 * Originally implemented for MN10300.
4 *
5 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public Licence
10 * as published by the Free Software Foundation; either version
11 * 2 of the Licence, or (at your option) any later version.
12 */
13#ifndef __ASM_GENERIC_ATOMIC_H
14#define __ASM_GENERIC_ATOMIC_H
15
16#ifdef CONFIG_SMP
17#error not SMP safe
18#endif
19
20/*
21 * Atomic operations that C can't guarantee us. Useful for
22 * resource counting etc..
23 */
24
25#define ATOMIC_INIT(i) { (i) }
26
27#ifdef __KERNEL__
28
29/**
30 * atomic_read - read atomic variable
31 * @v: pointer of type atomic_t
32 *
Peter Fritzsche37682172010-05-24 14:33:09 -070033 * Atomically reads the value of @v.
Arnd Bergmann3f7e2122009-05-13 22:56:35 +000034 */
Anton Blanchardf3d46f92010-05-17 14:33:53 +100035#define atomic_read(v) (*(volatile int *)&(v)->counter)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +000036
37/**
38 * atomic_set - set atomic variable
39 * @v: pointer of type atomic_t
40 * @i: required value
41 *
Peter Fritzsche37682172010-05-24 14:33:09 -070042 * Atomically sets the value of @v to @i.
Arnd Bergmann3f7e2122009-05-13 22:56:35 +000043 */
44#define atomic_set(v, i) (((v)->counter) = (i))
45
46#include <asm/system.h>
47
48/**
49 * atomic_add_return - add integer to atomic variable
50 * @i: integer value to add
51 * @v: pointer of type atomic_t
52 *
53 * Atomically adds @i to @v and returns the result
Arnd Bergmann3f7e2122009-05-13 22:56:35 +000054 */
55static inline int atomic_add_return(int i, atomic_t *v)
56{
57 unsigned long flags;
58 int temp;
59
Michal Simek9e581432010-08-09 17:18:24 -070060 raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
Arnd Bergmann3f7e2122009-05-13 22:56:35 +000061 temp = v->counter;
62 temp += i;
63 v->counter = temp;
Michal Simek9e581432010-08-09 17:18:24 -070064 raw_local_irq_restore(flags);
Arnd Bergmann3f7e2122009-05-13 22:56:35 +000065
66 return temp;
67}
68
69/**
70 * atomic_sub_return - subtract integer from atomic variable
71 * @i: integer value to subtract
72 * @v: pointer of type atomic_t
73 *
74 * Atomically subtracts @i from @v and returns the result
Arnd Bergmann3f7e2122009-05-13 22:56:35 +000075 */
76static inline int atomic_sub_return(int i, atomic_t *v)
77{
78 unsigned long flags;
79 int temp;
80
Michal Simek9e581432010-08-09 17:18:24 -070081 raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
Arnd Bergmann3f7e2122009-05-13 22:56:35 +000082 temp = v->counter;
83 temp -= i;
84 v->counter = temp;
Michal Simek9e581432010-08-09 17:18:24 -070085 raw_local_irq_restore(flags);
Arnd Bergmann3f7e2122009-05-13 22:56:35 +000086
87 return temp;
88}
89
90static inline int atomic_add_negative(int i, atomic_t *v)
91{
92 return atomic_add_return(i, v) < 0;
93}
94
95static inline void atomic_add(int i, atomic_t *v)
96{
97 atomic_add_return(i, v);
98}
99
100static inline void atomic_sub(int i, atomic_t *v)
101{
102 atomic_sub_return(i, v);
103}
104
105static inline void atomic_inc(atomic_t *v)
106{
107 atomic_add_return(1, v);
108}
109
110static inline void atomic_dec(atomic_t *v)
111{
112 atomic_sub_return(1, v);
113}
114
115#define atomic_dec_return(v) atomic_sub_return(1, (v))
116#define atomic_inc_return(v) atomic_add_return(1, (v))
117
118#define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
119#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
120#define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
121
122#define atomic_add_unless(v, a, u) \
123({ \
124 int c, old; \
125 c = atomic_read(v); \
126 while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
127 c = old; \
128 c != (u); \
129})
130
131#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
132
133static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
134{
135 unsigned long flags;
136
137 mask = ~mask;
Michal Simek9e581432010-08-09 17:18:24 -0700138 raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000139 *addr &= mask;
Michal Simek9e581432010-08-09 17:18:24 -0700140 raw_local_irq_restore(flags);
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000141}
142
143#define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
144#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
145
146#define cmpxchg_local(ptr, o, n) \
147 ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
148 (unsigned long)(n), sizeof(*(ptr))))
149
150#define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
151
152/* Assume that atomic operations are already serializing */
153#define smp_mb__before_atomic_dec() barrier()
154#define smp_mb__after_atomic_dec() barrier()
155#define smp_mb__before_atomic_inc() barrier()
156#define smp_mb__after_atomic_inc() barrier()
157
158#include <asm-generic/atomic-long.h>
159
160#endif /* __KERNEL__ */
161#endif /* __ASM_GENERIC_ATOMIC_H */