lib: percpu_counter_sub
[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);
aa0dff2d 35void percpu_counter_add(struct percpu_counter *fbc, s32 amount);
0216bfcf 36s64 percpu_counter_sum(struct percpu_counter *fbc);
1da177e4 37
0216bfcf 38static inline s64 percpu_counter_read(struct percpu_counter *fbc)
1da177e4
LT
39{
40 return fbc->count;
41}
42
43/*
44 * It is possible for the percpu_counter_read() to return a small negative
45 * number for some counter which should never be negative.
0216bfcf 46 *
1da177e4 47 */
0216bfcf 48static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
1da177e4 49{
0216bfcf 50 s64 ret = fbc->count;
1da177e4
LT
51
52 barrier(); /* Prevent reloads of fbc->count */
0216bfcf 53 if (ret >= 0)
1da177e4
LT
54 return ret;
55 return 1;
56}
57
58#else
59
60struct percpu_counter {
0216bfcf 61 s64 count;
1da177e4
LT
62};
63
0216bfcf 64static inline void percpu_counter_init(struct percpu_counter *fbc, s64 amount)
1da177e4 65{
0216bfcf 66 fbc->count = amount;
1da177e4
LT
67}
68
69static inline void percpu_counter_destroy(struct percpu_counter *fbc)
70{
71}
72
73static inline void
aa0dff2d 74percpu_counter_add(struct percpu_counter *fbc, s32 amount)
1da177e4
LT
75{
76 preempt_disable();
77 fbc->count += amount;
78 preempt_enable();
79}
80
0216bfcf 81static inline s64 percpu_counter_read(struct percpu_counter *fbc)
1da177e4
LT
82{
83 return fbc->count;
84}
85
0216bfcf 86static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
1da177e4
LT
87{
88 return fbc->count;
89}
90
0216bfcf 91static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
e2bab3d9
AM
92{
93 return percpu_counter_read_positive(fbc);
94}
95
1da177e4
LT
96#endif /* CONFIG_SMP */
97
98static inline void percpu_counter_inc(struct percpu_counter *fbc)
99{
aa0dff2d 100 percpu_counter_add(fbc, 1);
1da177e4
LT
101}
102
103static inline void percpu_counter_dec(struct percpu_counter *fbc)
104{
aa0dff2d 105 percpu_counter_add(fbc, -1);
1da177e4
LT
106}
107
3cb4f9fa
PZ
108static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
109{
110 percpu_counter_add(fbc, -amount);
111}
112
1da177e4 113#endif /* _LINUX_PERCPU_COUNTER_H */
This page took 0.408016 seconds and 5 git commands to generate.