Skip to content

Commit b4547d0

Browse files
committed
sched/hrtimer: use uint8_t for hrtimer state
Use uint8_t to represent the hrtimer state to improve performance. Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
1 parent 3db0955 commit b4547d0

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

include/nuttx/hrtimer.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,10 @@ enum hrtimer_mode_e
5757
* Callers must not modify the state directly.
5858
*/
5959

60-
enum hrtimer_state_e
61-
{
62-
HRTIMER_STATE_INACTIVE = 0, /* Timer is inactive and not queued */
63-
HRTIMER_STATE_ARMED, /* Timer is armed and waiting for expiry */
64-
HRTIMER_STATE_RUNNING, /* Timer callback is currently executing */
65-
HRTIMER_STATE_CANCELED /* Timer canceled (callback may be running) */
66-
};
60+
#define HRTIMER_STATE_INACTIVE 0 /* Timer is inactive and not queued */
61+
#define HRTIMER_STATE_ARMED 1 /* Timer is armed and waiting for expiry */
62+
#define HRTIMER_STATE_RUNNING 2 /* Timer callback is currently executing */
63+
#define HRTIMER_STATE_CANCELED 3 /* Timer canceled (callback may be running) */
6764

6865
/* Forward declarations */
6966

@@ -97,12 +94,12 @@ struct hrtimer_node_s
9794

9895
struct hrtimer_s
9996
{
100-
hrtimer_node_t node; /* RB-tree node for sorted insertion */
101-
enum hrtimer_state_e state; /* Current timer state */
102-
hrtimer_cb func; /* Expiration callback function */
103-
FAR void *arg; /* Argument passed to callback */
104-
uint64_t expired; /* Absolute expiration time (ns) */
105-
uint8_t cpus; /* Number of cpus that are running the timer */
97+
hrtimer_node_t node; /* RB-tree node for sorted insertion */
98+
hrtimer_cb func; /* Expiration callback function */
99+
FAR void *arg; /* Argument passed to callback */
100+
uint64_t expired; /* Absolute expiration time (ns) */
101+
uint8_t state; /* Current timer state */
102+
uint8_t cpus; /* Number of cpus that are running the timer */
106103
};
107104

108105
/****************************************************************************

sched/hrtimer/hrtimer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <nuttx/clock.h>
3333
#include <nuttx/hrtimer.h>
3434

35+
#ifdef CONFIG_HRTIMER
3536
/****************************************************************************
3637
* Public Data
3738
****************************************************************************/
@@ -154,7 +155,7 @@ int hrtimer_starttimer(uint64_t ns)
154155

155156
static inline_function
156157
int hrtimer_compare(FAR const hrtimer_node_t *a,
157-
FAR const hrtimer_node_t *b)
158+
FAR const hrtimer_node_t *b)
158159
{
159160
FAR const hrtimer_t *atimer = (FAR const hrtimer_t *)a;
160161
FAR const hrtimer_t *btimer = (FAR const hrtimer_t *)b;
@@ -173,4 +174,6 @@ int hrtimer_compare(FAR const hrtimer_node_t *a,
173174

174175
RB_PROTOTYPE(hrtimer_tree_s, hrtimer_node_s, entry, hrtimer_compare);
175176

177+
#endif /* CONFIG_HRTIMER */
178+
176179
#endif /* __SCHED_HRTIMER_HRTIMER_H */

sched/hrtimer/hrtimer_cancel.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
#include <nuttx/config.h>
2828
#include <nuttx/arch.h>
2929
#include <nuttx/clock.h>
30-
#include "sched/sched.h"
3130

3231
#include <errno.h>
3332

33+
#include "sched/sched.h"
3434
#include "hrtimer/hrtimer.h"
3535

3636
/****************************************************************************
@@ -41,9 +41,6 @@
4141

4242
#define HRTIMER_CANCEL_SYNC_DELAY_MS 5
4343

44-
#define hrtimer_is_free(hrtimer) (((hrtimer)->state == HRTIMER_STATE_INACTIVE) && \
45-
((hrtimer)->cpus == 0))
46-
4744
/****************************************************************************
4845
* Public Functions
4946
****************************************************************************/
@@ -107,7 +104,15 @@ int hrtimer_cancel(FAR hrtimer_t *hrtimer)
107104
/* Remove the timer from the active tree */
108105

109106
RB_REMOVE(hrtimer_tree_s, &g_hrtimer_tree, &hrtimer->node);
110-
hrtimer->state = HRTIMER_STATE_INACTIVE;
107+
108+
if (hrtimer->cpus == 0)
109+
{
110+
hrtimer->state = HRTIMER_STATE_INACTIVE;
111+
}
112+
else
113+
{
114+
hrtimer->state = HRTIMER_STATE_CANCELED;
115+
}
111116
break;
112117
}
113118

@@ -200,7 +205,7 @@ int hrtimer_cancel_sync(FAR hrtimer_t *hrtimer)
200205
* and the state becomes inactive.
201206
*/
202207

203-
while (!hrtimer_is_free(hrtimer))
208+
while (hrtimer->state != HRTIMER_STATE_INACTIVE)
204209
{
205210
if (cansleep)
206211
{

sched/hrtimer/hrtimer_process.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ void hrtimer_process(uint64_t now)
7070
{
7171
FAR hrtimer_t *hrtimer;
7272
uint32_t period = 0;
73+
uint64_t expired;
74+
hrtimer_cb func;
7375
irqstate_t flags;
7476

7577
/* Lock the hrtimer RB-tree to protect access */
@@ -84,7 +86,10 @@ void hrtimer_process(uint64_t now)
8486
{
8587
/* Check if the timer has expired */
8688

87-
if (!clock_compare(hrtimer->expired, now))
89+
expired = hrtimer->expired;
90+
func = hrtimer->func;
91+
92+
if (!clock_compare(expired, now))
8893
{
8994
break;
9095
}
@@ -110,7 +115,10 @@ void hrtimer_process(uint64_t now)
110115

111116
/* Execute the timer callback */
112117

113-
period = hrtimer->func(hrtimer);
118+
if (func == hrtimer->func)
119+
{
120+
period = func(hrtimer);
121+
}
114122

115123
/* Re-enter critical section to update timer state */
116124

@@ -124,7 +132,7 @@ void hrtimer_process(uint64_t now)
124132
{
125133
/* Timer callback completed normally */
126134

127-
if (period > 0)
135+
if ((period > 0) && (hrtimer->expired == expired))
128136
{
129137
/* Periodic timer: re-arm with the next expiration */
130138

0 commit comments

Comments
 (0)