V4L/DVB (11682): STV0900/STV0903: Add support for Silicon cut >= 3
[linux-2.6] / lib / ratelimit.c
1 /*
2  * ratelimit.c - Do something with rate limit.
3  *
4  * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
5  *
6  * 2008-05-01 rewrite the function and use a ratelimit_state data struct as
7  * parameter. Now every user can use their own standalone ratelimit_state.
8  *
9  * This file is released under the GPLv2.
10  *
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/jiffies.h>
15 #include <linux/module.h>
16
17 static DEFINE_SPINLOCK(ratelimit_lock);
18
19 /*
20  * __ratelimit - rate limiting
21  * @rs: ratelimit_state data
22  *
23  * This enforces a rate limit: not more than @rs->ratelimit_burst callbacks
24  * in every @rs->ratelimit_jiffies
25  */
26 int __ratelimit(struct ratelimit_state *rs)
27 {
28         unsigned long flags;
29
30         if (!rs->interval)
31                 return 1;
32
33         spin_lock_irqsave(&ratelimit_lock, flags);
34         if (!rs->begin)
35                 rs->begin = jiffies;
36
37         if (time_is_before_jiffies(rs->begin + rs->interval)) {
38                 if (rs->missed)
39                         printk(KERN_WARNING "%s: %d callbacks suppressed\n",
40                                 __func__, rs->missed);
41                 rs->begin = 0;
42                 rs->printed = 0;
43                 rs->missed = 0;
44         }
45         if (rs->burst && rs->burst > rs->printed)
46                 goto print;
47
48         rs->missed++;
49         spin_unlock_irqrestore(&ratelimit_lock, flags);
50         return 0;
51
52 print:
53         rs->printed++;
54         spin_unlock_irqrestore(&ratelimit_lock, flags);
55         return 1;
56 }
57 EXPORT_SYMBOL(__ratelimit);