]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/asm-generic/atomic.h
2 * Generic C implementation of atomic counter operations
3 * Originally implemented for MN10300.
5 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
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.
13 #ifndef __ASM_GENERIC_ATOMIC_H
14 #define __ASM_GENERIC_ATOMIC_H
21 * Atomic operations that C can't guarantee us. Useful for
22 * resource counting etc..
25 #define ATOMIC_INIT(i) { (i) }
30 * atomic_read - read atomic variable
31 * @v: pointer of type atomic_t
33 * Atomically reads the value of @v.
35 #define atomic_read(v) (*(volatile int *)&(v)->counter)
38 * atomic_set - set atomic variable
39 * @v: pointer of type atomic_t
42 * Atomically sets the value of @v to @i.
44 #define atomic_set(v, i) (((v)->counter) = (i))
46 #include <linux/irqflags.h>
47 #include <asm/system.h>
50 * atomic_add_return - add integer to atomic variable
51 * @i: integer value to add
52 * @v: pointer of type atomic_t
54 * Atomically adds @i to @v and returns the result
56 static inline int atomic_add_return(int i
, atomic_t
*v
)
61 raw_local_irq_save(flags
); /* Don't trace it in an irqsoff handler */
65 raw_local_irq_restore(flags
);
71 * atomic_sub_return - subtract integer from atomic variable
72 * @i: integer value to subtract
73 * @v: pointer of type atomic_t
75 * Atomically subtracts @i from @v and returns the result
77 static inline int atomic_sub_return(int i
, atomic_t
*v
)
82 raw_local_irq_save(flags
); /* Don't trace it in an irqsoff handler */
86 raw_local_irq_restore(flags
);
91 static inline int atomic_add_negative(int i
, atomic_t
*v
)
93 return atomic_add_return(i
, v
) < 0;
96 static inline void atomic_add(int i
, atomic_t
*v
)
98 atomic_add_return(i
, v
);
101 static inline void atomic_sub(int i
, atomic_t
*v
)
103 atomic_sub_return(i
, v
);
106 static inline void atomic_inc(atomic_t
*v
)
108 atomic_add_return(1, v
);
111 static inline void atomic_dec(atomic_t
*v
)
113 atomic_sub_return(1, v
);
116 #define atomic_dec_return(v) atomic_sub_return(1, (v))
117 #define atomic_inc_return(v) atomic_add_return(1, (v))
119 #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
120 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
121 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
123 #define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
124 #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
126 #define cmpxchg_local(ptr, o, n) \
127 ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
128 (unsigned long)(n), sizeof(*(ptr))))
130 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
132 static inline int __atomic_add_unless(atomic_t
*v
, int a
, int u
)
136 while (c
!= u
&& (old
= atomic_cmpxchg(v
, c
, c
+ a
)) != c
)
141 static inline void atomic_clear_mask(unsigned long mask
, unsigned long *addr
)
146 raw_local_irq_save(flags
); /* Don't trace it in a irqsoff handler */
148 raw_local_irq_restore(flags
);
151 /* Assume that atomic operations are already serializing */
152 #define smp_mb__before_atomic_dec() barrier()
153 #define smp_mb__after_atomic_dec() barrier()
154 #define smp_mb__before_atomic_inc() barrier()
155 #define smp_mb__after_atomic_inc() barrier()
157 #include <asm-generic/atomic-long.h>
159 #endif /* __KERNEL__ */
160 #endif /* __ASM_GENERIC_ATOMIC_H */