blob: adc80e29168ea0aa7f744dc12d36c105814daf33 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
John Stultz1f5a2472010-12-09 12:02:18 -08002#ifndef _LINUX_TIMERQUEUE_H
3#define _LINUX_TIMERQUEUE_H
4
5#include <linux/rbtree.h>
6#include <linux/ktime.h>
7
8
9struct timerqueue_node {
10 struct rb_node node;
11 ktime_t expires;
12};
13
14struct timerqueue_head {
Davidlohr Bueso511885d2019-07-24 08:23:23 -070015 struct rb_root_cached rb_root;
John Stultz1f5a2472010-12-09 12:02:18 -080016};
17
18
Thomas Gleixnerc3206422015-04-14 21:08:46 +000019extern bool timerqueue_add(struct timerqueue_head *head,
20 struct timerqueue_node *node);
21extern bool timerqueue_del(struct timerqueue_head *head,
22 struct timerqueue_node *node);
John Stultz1f5a2472010-12-09 12:02:18 -080023extern struct timerqueue_node *timerqueue_iterate_next(
24 struct timerqueue_node *node);
25
Thomas Gleixner45f74262010-12-11 12:34:34 +010026/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030027 * timerqueue_getnext - Returns the timer with the earliest expiration time
Thomas Gleixner45f74262010-12-11 12:34:34 +010028 *
29 * @head: head of timerqueue
30 *
Davidlohr Bueso511885d2019-07-24 08:23:23 -070031 * Returns a pointer to the timer node that has the earliest expiration time.
Thomas Gleixner45f74262010-12-11 12:34:34 +010032 */
33static inline
34struct timerqueue_node *timerqueue_getnext(struct timerqueue_head *head)
35{
Davidlohr Bueso511885d2019-07-24 08:23:23 -070036 struct rb_node *leftmost = rb_first_cached(&head->rb_root);
37
Barnabás Pőczed5bf0252022-11-14 19:54:23 +000038 return rb_entry_safe(leftmost, struct timerqueue_node, node);
Thomas Gleixner45f74262010-12-11 12:34:34 +010039}
40
John Stultz1f5a2472010-12-09 12:02:18 -080041static inline void timerqueue_init(struct timerqueue_node *node)
42{
Michel Lespinasse4c199a92012-10-08 16:30:32 -070043 RB_CLEAR_NODE(&node->node);
John Stultz1f5a2472010-12-09 12:02:18 -080044}
45
Thomas Gleixner60bda032019-08-27 21:31:02 +020046static inline bool timerqueue_node_queued(struct timerqueue_node *node)
47{
48 return !RB_EMPTY_NODE(&node->node);
49}
50
51static inline bool timerqueue_node_expires(struct timerqueue_node *node)
52{
53 return node->expires;
54}
55
John Stultz1f5a2472010-12-09 12:02:18 -080056static inline void timerqueue_init_head(struct timerqueue_head *head)
57{
Davidlohr Bueso511885d2019-07-24 08:23:23 -070058 head->rb_root = RB_ROOT_CACHED;
John Stultz1f5a2472010-12-09 12:02:18 -080059}
60#endif /* _LINUX_TIMERQUEUE_H */