Blame view

lib/timerqueue.c 2.29 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

798172b13   Peter Zijlstra   rbtree, timerqueu...
16
17
18
19
20
21
22
  #define __node_2_tq(_n) \
  	rb_entry((_n), struct timerqueue_node, node)
  
  static inline bool __timerqueue_less(struct rb_node *a, const struct rb_node *b)
  {
  	return __node_2_tq(a)->expires < __node_2_tq(b)->expires;
  }
87de5ac78   John Stultz   timers: Introduce...
23
  /**
1f5a24794   John Stultz   timers: Rename ti...
24
   * timerqueue_add - Adds timer to timerqueue.
87de5ac78   John Stultz   timers: Introduce...
25
   *
1f5a24794   John Stultz   timers: Rename ti...
26
   * @head: head of timerqueue
87de5ac78   John Stultz   timers: Introduce...
27
28
   * @node: timer node to be added
   *
9f4533cd7   Thomas Gleixner   timerqueue: Docum...
29
30
31
   * 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...
32
   */
c320642e1   Thomas Gleixner   timerqueue: Let t...
33
  bool timerqueue_add(struct timerqueue_head *head, struct timerqueue_node *node)
87de5ac78   John Stultz   timers: Introduce...
34
  {
87de5ac78   John Stultz   timers: Introduce...
35
36
  	/* Make sure we don't add nodes that are already added */
  	WARN_ON_ONCE(!RB_EMPTY_NODE(&node->node));
798172b13   Peter Zijlstra   rbtree, timerqueu...
37
  	return rb_add_cached(&node->node, &head->rb_root, __timerqueue_less);
87de5ac78   John Stultz   timers: Introduce...
38
  }
9bb99b147   John Stultz   timers: Fixup all...
39
  EXPORT_SYMBOL_GPL(timerqueue_add);
87de5ac78   John Stultz   timers: Introduce...
40
41
  
  /**
1f5a24794   John Stultz   timers: Rename ti...
42
   * timerqueue_del - Removes a timer from the timerqueue.
87de5ac78   John Stultz   timers: Introduce...
43
   *
1f5a24794   John Stultz   timers: Rename ti...
44
   * @head: head of timerqueue
87de5ac78   John Stultz   timers: Introduce...
45
46
   * @node: timer node to be removed
   *
9f4533cd7   Thomas Gleixner   timerqueue: Docum...
47
48
   * Removes the timer node from the timerqueue. Returns true if the queue is
   * not empty after the remove.
87de5ac78   John Stultz   timers: Introduce...
49
   */
c320642e1   Thomas Gleixner   timerqueue: Let t...
50
  bool timerqueue_del(struct timerqueue_head *head, struct timerqueue_node *node)
87de5ac78   John Stultz   timers: Introduce...
51
52
  {
  	WARN_ON_ONCE(RB_EMPTY_NODE(&node->node));
511885d70   Davidlohr Bueso   lib/timerqueue: R...
53
  	rb_erase_cached(&node->node, &head->rb_root);
87de5ac78   John Stultz   timers: Introduce...
54
  	RB_CLEAR_NODE(&node->node);
511885d70   Davidlohr Bueso   lib/timerqueue: R...
55
56
  
  	return !RB_EMPTY_ROOT(&head->rb_root.rb_root);
87de5ac78   John Stultz   timers: Introduce...
57
  }
9bb99b147   John Stultz   timers: Fixup all...
58
  EXPORT_SYMBOL_GPL(timerqueue_del);
87de5ac78   John Stultz   timers: Introduce...
59
60
  
  /**
1f5a24794   John Stultz   timers: Rename ti...
61
   * timerqueue_iterate_next - Returns the timer after the provided timer
87de5ac78   John Stultz   timers: Introduce...
62
63
64
65
66
67
68
   *
   * @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...
69
  struct timerqueue_node *timerqueue_iterate_next(struct timerqueue_node *node)
87de5ac78   John Stultz   timers: Introduce...
70
71
72
73
74
75
76
77
  {
  	struct rb_node *next;
  
  	if (!node)
  		return NULL;
  	next = rb_next(&node->node);
  	if (!next)
  		return NULL;
1f5a24794   John Stultz   timers: Rename ti...
78
  	return container_of(next, struct timerqueue_node, node);
87de5ac78   John Stultz   timers: Introduce...
79
  }
9bb99b147   John Stultz   timers: Fixup all...
80
  EXPORT_SYMBOL_GPL(timerqueue_iterate_next);