mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-18 07:00:06 -05:00
spa: add spa_ratelimit
This commit is contained in:
parent
17bc9d520e
commit
dc07c2321b
8 changed files with 76 additions and 69 deletions
42
spa/include/spa/utils/ratelimit.h
Normal file
42
spa/include/spa/utils/ratelimit.h
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/* Ratelimit */
|
||||
/* SPDX-FileCopyrightText: Copyright © 2023 Wim Taymans */
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
#ifndef SPA_RATELIMIT_H
|
||||
#define SPA_RATELIMIT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct spa_ratelimit {
|
||||
uint64_t interval;
|
||||
uint64_t begin;
|
||||
unsigned burst;
|
||||
unsigned n_printed;
|
||||
unsigned n_missed;
|
||||
};
|
||||
|
||||
static inline int spa_ratelimit_test(struct spa_ratelimit *r, uint64_t now)
|
||||
{
|
||||
unsigned missed = 0;
|
||||
if (r->begin + r->interval < now) {
|
||||
missed = r->n_missed;
|
||||
r->begin = now;
|
||||
r->n_printed = 0;
|
||||
r->n_missed = 0;
|
||||
} else if (r->n_printed >= r->burst) {
|
||||
r->n_missed++;
|
||||
return -1;
|
||||
}
|
||||
r->n_printed++;
|
||||
return missed;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* SPA_RATELIMIT_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue