Branch data Line data Source code
1 : : /***
2 : : This file is part of PulseAudio.
3 : :
4 : : Copyright 2009 Lennart Poettering
5 : :
6 : : PulseAudio is free software; you can redistribute it and/or modify
7 : : it under the terms of the GNU Lesser General Public License as
8 : : published by the Free Software Foundation; either version 2.1 of the
9 : : License, or (at your option) any later version.
10 : :
11 : : PulseAudio is distributed in the hope that it will be useful, but
12 : : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : General Public License for more details.
15 : :
16 : : You should have received a copy of the GNU Lesser General Public
17 : : License along with PulseAudio; if not, write to the Free Software
18 : : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 : : USA.
20 : : ***/
21 : :
22 : : #ifdef HAVE_CONFIG_H
23 : : #include <config.h>
24 : : #endif
25 : :
26 : : #include <pulse/rtclock.h>
27 : :
28 : : #include <pulsecore/log.h>
29 : : #include <pulsecore/mutex.h>
30 : :
31 : : #include "ratelimit.h"
32 : :
33 : : static pa_static_mutex mutex = PA_STATIC_MUTEX_INIT;
34 : :
35 : : /* Modelled after Linux' lib/ratelimit.c by Dave Young
36 : : * <hidave.darkstar@gmail.com>, which is licensed GPLv2. */
37 : :
38 : 0 : pa_bool_t pa_ratelimit_test(pa_ratelimit *r, pa_log_level_t t) {
39 : : pa_usec_t now;
40 : : pa_mutex *m;
41 : :
42 : 0 : now = pa_rtclock_now();
43 : :
44 : 0 : m = pa_static_mutex_get(&mutex, FALSE, FALSE);
45 : 0 : pa_mutex_lock(m);
46 : :
47 [ # # ]: 0 : pa_assert(r);
48 [ # # ]: 0 : pa_assert(r->interval > 0);
49 [ # # ]: 0 : pa_assert(r->burst > 0);
50 : :
51 [ # # ][ # # ]: 0 : if (r->begin <= 0 ||
52 : 0 : r->begin + r->interval < now) {
53 : :
54 [ # # ]: 0 : if (r->n_missed > 0)
55 : 0 : pa_logl(t, "%u events suppressed", r->n_missed);
56 : :
57 : 0 : r->begin = now;
58 : :
59 : : /* Reset counters */
60 : 0 : r->n_printed = 0;
61 : 0 : r->n_missed = 0;
62 : 0 : goto good;
63 : : }
64 : :
65 [ # # ]: 0 : if (r->n_printed <= r->burst)
66 : : goto good;
67 : :
68 : 0 : r->n_missed++;
69 : 0 : pa_mutex_unlock(m);
70 : 0 : return FALSE;
71 : :
72 : : good:
73 : 0 : r->n_printed++;
74 : 0 : pa_mutex_unlock(m);
75 : 0 : return TRUE;
76 : : }
|