Blame view

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