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