xref: /OK3568_Linux_fs/kernel/lib/timerqueue.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  Generic Timer-queue
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Manages a simple queue of timers, ordered by expiration time.
6*4882a593Smuzhiyun  *  Uses rbtrees for quick list adds and expiration.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *  NOTE: All of the following functions need to be serialized
9*4882a593Smuzhiyun  *  to avoid races. No locking is done by this library code.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/bug.h>
13*4882a593Smuzhiyun #include <linux/timerqueue.h>
14*4882a593Smuzhiyun #include <linux/rbtree.h>
15*4882a593Smuzhiyun #include <linux/export.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * timerqueue_add - Adds timer to timerqueue.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * @head: head of timerqueue
21*4882a593Smuzhiyun  * @node: timer node to be added
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Adds the timer node to the timerqueue, sorted by the node's expires
24*4882a593Smuzhiyun  * value. Returns true if the newly added timer is the first expiring timer in
25*4882a593Smuzhiyun  * the queue.
26*4882a593Smuzhiyun  */
timerqueue_add(struct timerqueue_head * head,struct timerqueue_node * node)27*4882a593Smuzhiyun bool timerqueue_add(struct timerqueue_head *head, struct timerqueue_node *node)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	struct rb_node **p = &head->rb_root.rb_root.rb_node;
30*4882a593Smuzhiyun 	struct rb_node *parent = NULL;
31*4882a593Smuzhiyun 	struct timerqueue_node *ptr;
32*4882a593Smuzhiyun 	bool leftmost = true;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	/* Make sure we don't add nodes that are already added */
35*4882a593Smuzhiyun 	WARN_ON_ONCE(!RB_EMPTY_NODE(&node->node));
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	while (*p) {
38*4882a593Smuzhiyun 		parent = *p;
39*4882a593Smuzhiyun 		ptr = rb_entry(parent, struct timerqueue_node, node);
40*4882a593Smuzhiyun 		if (node->expires < ptr->expires) {
41*4882a593Smuzhiyun 			p = &(*p)->rb_left;
42*4882a593Smuzhiyun 		} else {
43*4882a593Smuzhiyun 			p = &(*p)->rb_right;
44*4882a593Smuzhiyun 			leftmost = false;
45*4882a593Smuzhiyun 		}
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun 	rb_link_node(&node->node, parent, p);
48*4882a593Smuzhiyun 	rb_insert_color_cached(&node->node, &head->rb_root, leftmost);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	return leftmost;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(timerqueue_add);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun  * timerqueue_del - Removes a timer from the timerqueue.
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * @head: head of timerqueue
58*4882a593Smuzhiyun  * @node: timer node to be removed
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * Removes the timer node from the timerqueue. Returns true if the queue is
61*4882a593Smuzhiyun  * not empty after the remove.
62*4882a593Smuzhiyun  */
timerqueue_del(struct timerqueue_head * head,struct timerqueue_node * node)63*4882a593Smuzhiyun bool timerqueue_del(struct timerqueue_head *head, struct timerqueue_node *node)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	WARN_ON_ONCE(RB_EMPTY_NODE(&node->node));
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	rb_erase_cached(&node->node, &head->rb_root);
68*4882a593Smuzhiyun 	RB_CLEAR_NODE(&node->node);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	return !RB_EMPTY_ROOT(&head->rb_root.rb_root);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(timerqueue_del);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /**
75*4882a593Smuzhiyun  * timerqueue_iterate_next - Returns the timer after the provided timer
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * @node: Pointer to a timer.
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  * Provides the timer that is after the given node. This is used, when
80*4882a593Smuzhiyun  * necessary, to iterate through the list of timers in a timer list
81*4882a593Smuzhiyun  * without modifying the list.
82*4882a593Smuzhiyun  */
timerqueue_iterate_next(struct timerqueue_node * node)83*4882a593Smuzhiyun struct timerqueue_node *timerqueue_iterate_next(struct timerqueue_node *node)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	struct rb_node *next;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	if (!node)
88*4882a593Smuzhiyun 		return NULL;
89*4882a593Smuzhiyun 	next = rb_next(&node->node);
90*4882a593Smuzhiyun 	if (!next)
91*4882a593Smuzhiyun 		return NULL;
92*4882a593Smuzhiyun 	return container_of(next, struct timerqueue_node, node);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(timerqueue_iterate_next);
95