Blame view

lib/timerqueue.c 2.46 KB
1a59d1b8e   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
87de5ac78   John Stultz   timers: Introduce...
2
  /*
1f5a24794   John Stultz   timers: Rename ti...
3
   *  Generic Timer-queue
87de5ac78   John Stultz   timers: Introduce...
4
   *
1f5a24794   John Stultz   timers: Rename ti...
5
   *  Manages a simple queue of timers, ordered by expiration time.
87de5ac78   John Stultz   timers: Introduce...
6
7
8
   *  Uses rbtrees for quick list adds and expiration.
   *
   *  NOTE: All of the following functions need to be serialized
25985edce   Lucas De Marchi   Fix common misspe...
9
   *  to avoid races. No locking is done by this library code.
87de5ac78   John Stultz   timers: Introduce...
10
   */
50af5ead3   Paul Gortmaker   bug.h: add includ...
11
  #include <linux/bug.h>
1f5a24794   John Stultz   timers: Rename ti...
12
  #include <linux/timerqueue.h>
87de5ac78   John Stultz   timers: Introduce...
13
  #include <linux/rbtree.h>
8bc3bcc93   Paul Gortmaker   lib: reduce the u...
14
  #include <linux/export.h>
87de5ac78   John Stultz   timers: Introduce...
15
16
  
  /**
1f5a24794   John Stultz   timers: Rename ti...
17
   * timerqueue_add - Adds timer to timerqueue.
87de5ac78   John Stultz   timers: Introduce...
18
   *
1f5a24794   John Stultz   timers: Rename ti...
19
   * @head: head of timerqueue
87de5ac78   John Stultz   timers: Introduce...
20
21
   * @node: timer node to be added
   *
9f4533cd7   Thomas Gleixner   timerqueue: Docum...
22
23
24
   * Adds the timer node to the timerqueue, sorted by the node's expires
   * value. Returns true if the newly added timer is the first expiring timer in
   * the queue.
87de5ac78   John Stultz   timers: Introduce...
25
   */
c320642e1   Thomas Gleixner   timerqueue: Let t...
26
  bool timerqueue_add(struct timerqueue_head *head, struct timerqueue_node *node)
87de5ac78   John Stultz   timers: Introduce...
27
  {
511885d70   Davidlohr Bueso   lib/timerqueue: R...
28
  	struct rb_node **p = &head->rb_root.rb_root.rb_node;
87de5ac78   John Stultz   timers: Introduce...
29
  	struct rb_node *parent = NULL;
511885d70   Davidlohr Bueso   lib/timerqueue: R...
30
31
  	struct timerqueue_node *ptr;
  	bool leftmost = true;
87de5ac78   John Stultz   timers: Introduce...
32
33
34
35
36
37
  
  	/* Make sure we don't add nodes that are already added */
  	WARN_ON_ONCE(!RB_EMPTY_NODE(&node->node));
  
  	while (*p) {
  		parent = *p;
1f5a24794   John Stultz   timers: Rename ti...
38
  		ptr = rb_entry(parent, struct timerqueue_node, node);
511885d70   Davidlohr Bueso   lib/timerqueue: R...
39
  		if (node->expires < ptr->expires) {
87de5ac78   John Stultz   timers: Introduce...
40
  			p = &(*p)->rb_left;
511885d70   Davidlohr Bueso   lib/timerqueue: R...
41
  		} else {
87de5ac78   John Stultz   timers: Introduce...
42
  			p = &(*p)->rb_right;
511885d70   Davidlohr Bueso   lib/timerqueue: R...
43
44
  			leftmost = false;
  		}
87de5ac78   John Stultz   timers: Introduce...
45
46
  	}
  	rb_link_node(&node->node, parent, p);
511885d70   Davidlohr Bueso   lib/timerqueue: R...
47
  	rb_insert_color_cached(&node->node, &head->rb_root, leftmost);
87de5ac78   John Stultz   timers: Introduce...
48

511885d70   Davidlohr Bueso   lib/timerqueue: R...
49
  	return leftmost;
87de5ac78   John Stultz   timers: Introduce...
50
  }
9bb99b147   John Stultz   timers: Fixup all...
51
  EXPORT_SYMBOL_GPL(timerqueue_add);
87de5ac78   John Stultz   timers: Introduce...
52
53
  
  /**
1f5a24794   John Stultz   timers: Rename ti...
54
   * timerqueue_del - Removes a timer from the timerqueue.
87de5ac78   John Stultz   timers: Introduce...
55
   *
1f5a24794   John Stultz   timers: Rename ti...
56
   * @head: head of timerqueue
87de5ac78   John Stultz   timers: Introduce...
57
58
   * @node: timer node to be removed
   *
9f4533cd7   Thomas Gleixner   timerqueue: Docum...
59
60
   * Removes the timer node from the timerqueue. Returns true if the queue is
   * not empty after the remove.
87de5ac78   John Stultz   timers: Introduce...
61
   */
c320642e1   Thomas Gleixner   timerqueue: Let t...
62
  bool timerqueue_del(struct timerqueue_head *head, struct timerqueue_node *node)
87de5ac78   John Stultz   timers: Introduce...
63
64
  {
  	WARN_ON_ONCE(RB_EMPTY_NODE(&node->node));
511885d70   Davidlohr Bueso   lib/timerqueue: R...
65
  	rb_erase_cached(&node->node, &head->rb_root);
87de5ac78   John Stultz   timers: Introduce...
66
  	RB_CLEAR_NODE(&node->node);
511885d70   Davidlohr Bueso   lib/timerqueue: R...
67
68
  
  	return !RB_EMPTY_ROOT(&head->rb_root.rb_root);
87de5ac78   John Stultz   timers: Introduce...
69
  }
9bb99b147   John Stultz   timers: Fixup all...
70
  EXPORT_SYMBOL_GPL(timerqueue_del);
87de5ac78   John Stultz   timers: Introduce...
71
72
  
  /**
1f5a24794   John Stultz   timers: Rename ti...
73
   * timerqueue_iterate_next - Returns the timer after the provided timer
87de5ac78   John Stultz   timers: Introduce...
74
75
76
77
78
79
80
   *
   * @node: Pointer to a timer.
   *
   * Provides the timer that is after the given node. This is used, when
   * necessary, to iterate through the list of timers in a timer list
   * without modifying the list.
   */
1f5a24794   John Stultz   timers: Rename ti...
81
  struct timerqueue_node *timerqueue_iterate_next(struct timerqueue_node *node)
87de5ac78   John Stultz   timers: Introduce...
82
83
84
85
86
87
88
89
  {
  	struct rb_node *next;
  
  	if (!node)
  		return NULL;
  	next = rb_next(&node->node);
  	if (!next)
  		return NULL;
1f5a24794   John Stultz   timers: Rename ti...
90
  	return container_of(next, struct timerqueue_node, node);
87de5ac78   John Stultz   timers: Introduce...
91
  }
9bb99b147   John Stultz   timers: Fixup all...
92
  EXPORT_SYMBOL_GPL(timerqueue_iterate_next);