blob: 8942cdc676a70fc8cd1c4c1c6adae8221bf322c2 [file] [log] [blame]
Arnd Bergmann72099ed2009-05-13 22:56:29 +00001#ifndef _ASM_GENERIC_ATOMIC_LONG_H
2#define _ASM_GENERIC_ATOMIC_LONG_H
Christoph Lameterd3cb4872006-01-06 00:11:20 -08003/*
4 * Copyright (C) 2005 Silicon Graphics, Inc.
Christoph Lametercde53532008-07-04 09:59:22 -07005 * Christoph Lameter
Christoph Lameterd3cb4872006-01-06 00:11:20 -08006 *
7 * Allows to provide arch independent atomic definitions without the need to
8 * edit all arch specific atomic.h files.
9 */
10
Andrew Morton5998bf12006-01-08 01:00:29 -080011#include <asm/types.h>
Christoph Lameterd3cb4872006-01-06 00:11:20 -080012
13/*
14 * Suppport for atomic_long_t
15 *
16 * Casts for parameters are avoided for existing atomic functions in order to
17 * avoid issues with cast-as-lval under gcc 4.x and other limitations that the
18 * macros of a platform may have.
19 */
20
21#if BITS_PER_LONG == 64
22
23typedef atomic64_t atomic_long_t;
24
25#define ATOMIC_LONG_INIT(i) ATOMIC64_INIT(i)
Will Deacon586b6102015-08-06 17:54:38 +010026#define ATOMIC_LONG_PFX(x) atomic64 ## x
Christoph Lameterd3cb4872006-01-06 00:11:20 -080027
Will Deacon586b6102015-08-06 17:54:38 +010028#else
Christoph Lameterd3cb4872006-01-06 00:11:20 -080029
30typedef atomic_t atomic_long_t;
31
32#define ATOMIC_LONG_INIT(i) ATOMIC_INIT(i)
Will Deacon586b6102015-08-06 17:54:38 +010033#define ATOMIC_LONG_PFX(x) atomic ## x
34
35#endif
36
Will Deacon6d79ef22015-08-06 17:54:39 +010037#define ATOMIC_LONG_READ_OP(mo) \
Peter Zijlstrae3e72ab2015-09-18 13:22:52 +020038static inline long atomic_long_read##mo(const atomic_long_t *l) \
Will Deacon6d79ef22015-08-06 17:54:39 +010039{ \
40 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
41 \
42 return (long)ATOMIC_LONG_PFX(_read##mo)(v); \
Christoph Lameterd3cb4872006-01-06 00:11:20 -080043}
Will Deacon6d79ef22015-08-06 17:54:39 +010044ATOMIC_LONG_READ_OP()
45ATOMIC_LONG_READ_OP(_acquire)
Peter Zijlstrae3e72ab2015-09-18 13:22:52 +020046ATOMIC_LONG_READ_OP(_ctrl)
Christoph Lameterd3cb4872006-01-06 00:11:20 -080047
Will Deacon6d79ef22015-08-06 17:54:39 +010048#undef ATOMIC_LONG_READ_OP
Christoph Lameterd3cb4872006-01-06 00:11:20 -080049
Will Deacon6d79ef22015-08-06 17:54:39 +010050#define ATOMIC_LONG_SET_OP(mo) \
51static inline void atomic_long_set##mo(atomic_long_t *l, long i) \
52{ \
53 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
54 \
55 ATOMIC_LONG_PFX(_set##mo)(v, i); \
Christoph Lameterd3cb4872006-01-06 00:11:20 -080056}
Will Deacon6d79ef22015-08-06 17:54:39 +010057ATOMIC_LONG_SET_OP()
58ATOMIC_LONG_SET_OP(_release)
59
60#undef ATOMIC_LONG_SET_OP
61
62#define ATOMIC_LONG_ADD_SUB_OP(op, mo) \
63static inline long \
64atomic_long_##op##_return##mo(long i, atomic_long_t *l) \
65{ \
66 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
67 \
68 return (long)ATOMIC_LONG_PFX(_##op##_return##mo)(i, v); \
69}
70ATOMIC_LONG_ADD_SUB_OP(add,)
71ATOMIC_LONG_ADD_SUB_OP(add, _relaxed)
72ATOMIC_LONG_ADD_SUB_OP(add, _acquire)
73ATOMIC_LONG_ADD_SUB_OP(add, _release)
74ATOMIC_LONG_ADD_SUB_OP(sub,)
75ATOMIC_LONG_ADD_SUB_OP(sub, _relaxed)
76ATOMIC_LONG_ADD_SUB_OP(sub, _acquire)
77ATOMIC_LONG_ADD_SUB_OP(sub, _release)
78
79#undef ATOMIC_LONG_ADD_SUB_OP
80
81#define atomic_long_cmpxchg_relaxed(l, old, new) \
82 (ATOMIC_LONG_PFX(_cmpxchg_relaxed)((ATOMIC_LONG_PFX(_t) *)(l), \
83 (old), (new)))
84#define atomic_long_cmpxchg_acquire(l, old, new) \
85 (ATOMIC_LONG_PFX(_cmpxchg_acquire)((ATOMIC_LONG_PFX(_t) *)(l), \
86 (old), (new)))
87#define atomic_long_cmpxchg_release(l, old, new) \
88 (ATOMIC_LONG_PFX(_cmpxchg_release)((ATOMIC_LONG_PFX(_t) *)(l), \
89 (old), (new)))
90#define atomic_long_cmpxchg(l, old, new) \
91 (ATOMIC_LONG_PFX(_cmpxchg)((ATOMIC_LONG_PFX(_t) *)(l), (old), (new)))
92
93#define atomic_long_xchg_relaxed(v, new) \
94 (ATOMIC_LONG_PFX(_xchg_relaxed)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
95#define atomic_long_xchg_acquire(v, new) \
96 (ATOMIC_LONG_PFX(_xchg_acquire)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
97#define atomic_long_xchg_release(v, new) \
98 (ATOMIC_LONG_PFX(_xchg_release)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
99#define atomic_long_xchg(v, new) \
100 (ATOMIC_LONG_PFX(_xchg)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800101
102static inline void atomic_long_inc(atomic_long_t *l)
103{
Will Deacon586b6102015-08-06 17:54:38 +0100104 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800105
Will Deacon586b6102015-08-06 17:54:38 +0100106 ATOMIC_LONG_PFX(_inc)(v);
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800107}
108
109static inline void atomic_long_dec(atomic_long_t *l)
110{
Will Deacon586b6102015-08-06 17:54:38 +0100111 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800112
Will Deacon586b6102015-08-06 17:54:38 +0100113 ATOMIC_LONG_PFX(_dec)(v);
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800114}
115
Peter Zijlstra90fe6512015-09-18 15:04:59 +0200116#define ATOMIC_LONG_OP(op) \
117static inline void \
118atomic_long_##op(long i, atomic_long_t *l) \
119{ \
120 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
121 \
122 ATOMIC_LONG_PFX(_##op)(i, v); \
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800123}
124
Peter Zijlstra90fe6512015-09-18 15:04:59 +0200125ATOMIC_LONG_OP(add)
126ATOMIC_LONG_OP(sub)
127ATOMIC_LONG_OP(and)
128ATOMIC_LONG_OP(or)
129ATOMIC_LONG_OP(xor)
130ATOMIC_LONG_OP(andnot)
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800131
Peter Zijlstra90fe6512015-09-18 15:04:59 +0200132#undef ATOMIC_LONG_OP
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800133
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700134static inline int atomic_long_sub_and_test(long i, atomic_long_t *l)
135{
Will Deacon586b6102015-08-06 17:54:38 +0100136 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700137
Will Deacon586b6102015-08-06 17:54:38 +0100138 return ATOMIC_LONG_PFX(_sub_and_test)(i, v);
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700139}
140
141static inline int atomic_long_dec_and_test(atomic_long_t *l)
142{
Will Deacon586b6102015-08-06 17:54:38 +0100143 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700144
Will Deacon586b6102015-08-06 17:54:38 +0100145 return ATOMIC_LONG_PFX(_dec_and_test)(v);
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700146}
147
148static inline int atomic_long_inc_and_test(atomic_long_t *l)
149{
Will Deacon586b6102015-08-06 17:54:38 +0100150 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700151
Will Deacon586b6102015-08-06 17:54:38 +0100152 return ATOMIC_LONG_PFX(_inc_and_test)(v);
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700153}
154
155static inline int atomic_long_add_negative(long i, atomic_long_t *l)
156{
Will Deacon586b6102015-08-06 17:54:38 +0100157 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700158
Will Deacon586b6102015-08-06 17:54:38 +0100159 return ATOMIC_LONG_PFX(_add_negative)(i, v);
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700160}
161
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700162static inline long atomic_long_inc_return(atomic_long_t *l)
163{
Will Deacon586b6102015-08-06 17:54:38 +0100164 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700165
Will Deacon586b6102015-08-06 17:54:38 +0100166 return (long)ATOMIC_LONG_PFX(_inc_return)(v);
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700167}
168
169static inline long atomic_long_dec_return(atomic_long_t *l)
170{
Will Deacon586b6102015-08-06 17:54:38 +0100171 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700172
Will Deacon586b6102015-08-06 17:54:38 +0100173 return (long)ATOMIC_LONG_PFX(_dec_return)(v);
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700174}
175
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700176static inline long atomic_long_add_unless(atomic_long_t *l, long a, long u)
177{
Will Deacon586b6102015-08-06 17:54:38 +0100178 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700179
Will Deacon586b6102015-08-06 17:54:38 +0100180 return (long)ATOMIC_LONG_PFX(_add_unless)(v, a, u);
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700181}
Mathieu Desnoyersbb2382c2007-05-08 00:34:19 -0700182
Will Deacon586b6102015-08-06 17:54:38 +0100183#define atomic_long_inc_not_zero(l) \
184 ATOMIC_LONG_PFX(_inc_not_zero)((ATOMIC_LONG_PFX(_t) *)(l))
Adrian Bunk4b358e22006-12-06 20:40:28 -0800185
Arnd Bergmann72099ed2009-05-13 22:56:29 +0000186#endif /* _ASM_GENERIC_ATOMIC_LONG_H */