| 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 | |
|---|
| 9 | #include <linux/smp.h> |
|---|
| 10 | #include <linux/threads.h> |
|---|
| 11 | #include <linux/percpu.h> |
|---|
| 12 | |
|---|
| 13 | #ifdef CONFIG_SMP |
|---|
| 14 | |
|---|
| 15 | struct percpu_counter { |
|---|
| 16 | spinlock_t lock; |
|---|
| 17 | long count; |
|---|
| 18 | long *counters; |
|---|
| 19 | }; |
|---|
| 20 | |
|---|
| 21 | #if NR_CPUS >= 16 |
|---|
| 22 | #define FBC_BATCH (NR_CPUS*2) |
|---|
| 23 | #else |
|---|
| 24 | #define FBC_BATCH (NR_CPUS*4) |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | static inline void percpu_counter_init(struct percpu_counter *fbc) |
|---|
| 28 | { |
|---|
| 29 | spin_lock_init(&fbc->lock); |
|---|
| 30 | fbc->count = 0; |
|---|
| 31 | fbc->counters = alloc_percpu(long); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | static inline void percpu_counter_destroy(struct percpu_counter *fbc) |
|---|
| 35 | { |
|---|
| 36 | free_percpu(fbc->counters); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | void percpu_counter_mod(struct percpu_counter *fbc, long amount); |
|---|
| 40 | |
|---|
| 41 | static inline long percpu_counter_read(struct percpu_counter *fbc) |
|---|
| 42 | { |
|---|
| 43 | return fbc->count; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /* |
|---|
| 47 | * It is possible for the percpu_counter_read() to return a small negative |
|---|
| 48 | * number for some counter which should never be negative. |
|---|
| 49 | */ |
|---|
| 50 | static inline long percpu_counter_read_positive(struct percpu_counter *fbc) |
|---|
| 51 | { |
|---|
| 52 | long ret = fbc->count; |
|---|
| 53 | |
|---|
| 54 | barrier(); /* Prevent reloads of fbc->count */ |
|---|
| 55 | if (ret > 0) |
|---|
| 56 | return ret; |
|---|
| 57 | return 1; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | #else |
|---|
| 61 | |
|---|
| 62 | struct percpu_counter { |
|---|
| 63 | long count; |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | static inline void percpu_counter_init(struct percpu_counter *fbc) |
|---|
| 67 | { |
|---|
| 68 | fbc->count = 0; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | static inline void percpu_counter_destroy(struct percpu_counter *fbc) |
|---|
| 72 | { |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | static inline void |
|---|
| 76 | percpu_counter_mod(struct percpu_counter *fbc, long amount) |
|---|
| 77 | { |
|---|
| 78 | preempt_disable(); |
|---|
| 79 | fbc->count += amount; |
|---|
| 80 | preempt_enable(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | static inline long percpu_counter_read(struct percpu_counter *fbc) |
|---|
| 84 | { |
|---|
| 85 | return fbc->count; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | static inline long percpu_counter_read_positive(struct percpu_counter *fbc) |
|---|
| 89 | { |
|---|
| 90 | return fbc->count; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | #endif /* CONFIG_SMP */ |
|---|
| 94 | |
|---|
| 95 | static inline void percpu_counter_inc(struct percpu_counter *fbc) |
|---|
| 96 | { |
|---|
| 97 | percpu_counter_mod(fbc, 1); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | static inline void percpu_counter_dec(struct percpu_counter *fbc) |
|---|
| 101 | { |
|---|
| 102 | percpu_counter_mod(fbc, -1); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | #endif /* _LINUX_PERCPU_COUNTER_H */ |
|---|