lib: percpu_counter_set
[deliverable/linux.git] / include / linux / percpu_counter.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_PERCPU_COUNTER_H
2#define _LINUX_PERCPU_COUNTER_H
3/*
4 * A simple "approximate counter" for use in ext2 and ext3 superblocks.
5 *
6 * WARNING: these things are HUGE. 4 kbytes per counter on 32-way P4.
7 */
8
1da177e4
LT
9#include <linux/spinlock.h>
10#include <linux/smp.h>
c67ad917 11#include <linux/list.h>
1da177e4
LT
12#include <linux/threads.h>
13#include <linux/percpu.h>
0216bfcf 14#include <linux/types.h>
1da177e4
LT
15
16#ifdef CONFIG_SMP
17
18struct percpu_counter {
19 spinlock_t lock;
0216bfcf 20 s64 count;
c67ad917
AM
21#ifdef CONFIG_HOTPLUG_CPU
22 struct list_head list; /* All percpu_counters are on a list */
23#endif
0216bfcf 24 s32 *counters;
1da177e4
LT
25};
26
27#if NR_CPUS >= 16
28#define FBC_BATCH (NR_CPUS*2)
29#else
30#define FBC_BATCH (NR_CPUS*4)
31#endif
32
c67ad917
AM
33void percpu_counter_init(struct percpu_counter *fbc, s64 amount);
34void percpu_counter_destroy(struct percpu_counter *fbc);
3a587f47 35void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
20e89767 36void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch);
0216bfcf 37s64 percpu_counter_sum(struct percpu_counter *fbc);
1da177e4 38
20e89767 39static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
252e0ba6
PZ
40{
41 __percpu_counter_add(fbc, amount, FBC_BATCH);
42}
43
0216bfcf 44static inline s64 percpu_counter_read(struct percpu_counter *fbc)
1da177e4
LT
45{
46 return fbc->count;
47}
48
49/*
50 * It is possible for the percpu_counter_read() to return a small negative
51 * number for some counter which should never be negative.
0216bfcf 52 *
1da177e4 53 */
0216bfcf 54static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
1da177e4 55{
0216bfcf 56 s64 ret = fbc->count;
1da177e4
LT
57
58 barrier(); /* Prevent reloads of fbc->count */
0216bfcf 59 if (ret >= 0)
1da177e4
LT
60 return ret;
61 return 1;
62}
63
64#else
65
66struct percpu_counter {
0216bfcf 67 s64 count;
1da177e4
LT
68};
69
0216bfcf 70static inline void percpu_counter_init(struct percpu_counter *fbc, s64 amount)
1da177e4 71{
0216bfcf 72 fbc->count = amount;
1da177e4
LT
73}
74
75static inline void percpu_counter_destroy(struct percpu_counter *fbc)
76{
77}
78
3a587f47
PZ
79static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
80{
81 fbc->count = amount;
82}
83
252e0ba6
PZ
84#define __percpu_counter_add(fbc, amount, batch) \
85 percpu_counter_add(fbc, amount)
86
1da177e4 87static inline void
20e89767 88percpu_counter_add(struct percpu_counter *fbc, s64 amount)
1da177e4
LT
89{
90 preempt_disable();
91 fbc->count += amount;
92 preempt_enable();
93}
94
0216bfcf 95static inline s64 percpu_counter_read(struct percpu_counter *fbc)
1da177e4
LT
96{
97 return fbc->count;
98}
99
0216bfcf 100static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
1da177e4
LT
101{
102 return fbc->count;
103}
104
0216bfcf 105static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
e2bab3d9
AM
106{
107 return percpu_counter_read_positive(fbc);
108}
109
1da177e4
LT
110#endif /* CONFIG_SMP */
111
112static inline void percpu_counter_inc(struct percpu_counter *fbc)
113{
aa0dff2d 114 percpu_counter_add(fbc, 1);
1da177e4
LT
115}
116
117static inline void percpu_counter_dec(struct percpu_counter *fbc)
118{
aa0dff2d 119 percpu_counter_add(fbc, -1);
1da177e4
LT
120}
121
3cb4f9fa
PZ
122static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
123{
124 percpu_counter_add(fbc, -amount);
125}
126
1da177e4 127#endif /* _LINUX_PERCPU_COUNTER_H */
This page took 0.3584 seconds and 5 git commands to generate.