8
8
*/
9
9
10
10
#include <hal.h>
11
+ #include <spinlock.h>
11
12
#include <lib/list.h>
12
13
#include <lib/malloc.h>
13
14
#include <sys/task.h>
@@ -32,6 +33,9 @@ static struct {
32
33
} timer_cache [4 ];
33
34
static uint8_t timer_cache_index = 0 ;
34
35
36
+ static spinlock_t timer_lock = SPINLOCK_INITIALIZER ;
37
+ static uint32_t timer_flags = 0 ;
38
+
35
39
/* Get a node from the pool, fall back to malloc if pool is empty */
36
40
static list_node_t * get_timer_node (void )
37
41
{
@@ -83,9 +87,9 @@ static int32_t timer_subsystem_init(void)
83
87
if (unlikely (timer_initialized ))
84
88
return ERR_OK ;
85
89
86
- NOSCHED_ENTER ( );
90
+ spin_lock_irqsave ( & timer_lock , & timer_flags );
87
91
if (timer_initialized ) {
88
- NOSCHED_LEAVE ( );
92
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
89
93
return ERR_OK ;
90
94
}
91
95
@@ -101,7 +105,7 @@ static int32_t timer_subsystem_init(void)
101
105
list_destroy (kcb -> timer_list );
102
106
kcb -> timer_list = NULL ;
103
107
}
104
- NOSCHED_LEAVE ( );
108
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
105
109
return ERR_FAIL ;
106
110
}
107
111
@@ -112,7 +116,7 @@ static int32_t timer_subsystem_init(void)
112
116
}
113
117
114
118
timer_initialized = true;
115
- NOSCHED_LEAVE ( );
119
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
116
120
return ERR_OK ;
117
121
}
118
122
@@ -294,7 +298,7 @@ int32_t mo_timer_create(void *(*callback)(void *arg),
294
298
if (unlikely (!t ))
295
299
return ERR_FAIL ;
296
300
297
- NOSCHED_ENTER ( );
301
+ spin_lock_irqsave ( & timer_lock , & timer_flags );
298
302
299
303
/* Initialize timer */
300
304
t -> id = next_id ++ ;
@@ -307,15 +311,15 @@ int32_t mo_timer_create(void *(*callback)(void *arg),
307
311
308
312
/* Insert into sorted all_timers_list */
309
313
if (unlikely (timer_insert_sorted_by_id (t ) != ERR_OK )) {
310
- NOSCHED_LEAVE ( );
314
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
311
315
free (t );
312
316
return ERR_FAIL ;
313
317
}
314
318
315
319
/* Add to cache */
316
320
cache_timer (t -> id , t );
317
321
318
- NOSCHED_LEAVE ( );
322
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
319
323
return t -> id ;
320
324
}
321
325
@@ -324,11 +328,11 @@ int32_t mo_timer_destroy(uint16_t id)
324
328
if (unlikely (!timer_initialized ))
325
329
return ERR_FAIL ;
326
330
327
- NOSCHED_ENTER ( );
331
+ spin_lock_irqsave ( & timer_lock , & timer_flags );
328
332
329
333
list_node_t * node = timer_find_node_by_id (id );
330
334
if (unlikely (!node )) {
331
- NOSCHED_LEAVE ( );
335
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
332
336
return ERR_FAIL ;
333
337
}
334
338
@@ -351,7 +355,7 @@ int32_t mo_timer_destroy(uint16_t id)
351
355
free (t );
352
356
return_timer_node (node );
353
357
354
- NOSCHED_LEAVE ( );
358
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
355
359
return ERR_OK ;
356
360
}
357
361
@@ -362,11 +366,11 @@ int32_t mo_timer_start(uint16_t id, uint8_t mode)
362
366
if (unlikely (!timer_initialized ))
363
367
return ERR_FAIL ;
364
368
365
- NOSCHED_ENTER ( );
369
+ spin_lock_irqsave ( & timer_lock , & timer_flags );
366
370
367
371
timer_t * t = timer_find_by_id_fast (id );
368
372
if (unlikely (!t )) {
369
- NOSCHED_LEAVE ( );
373
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
370
374
return ERR_FAIL ;
371
375
}
372
376
@@ -380,11 +384,11 @@ int32_t mo_timer_start(uint16_t id, uint8_t mode)
380
384
381
385
if (unlikely (timer_sorted_insert (t ) != ERR_OK )) {
382
386
t -> mode = TIMER_DISABLED ;
383
- NOSCHED_LEAVE ( );
387
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
384
388
return ERR_FAIL ;
385
389
}
386
390
387
- NOSCHED_LEAVE ( );
391
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
388
392
return ERR_OK ;
389
393
}
390
394
@@ -393,17 +397,17 @@ int32_t mo_timer_cancel(uint16_t id)
393
397
if (unlikely (!timer_initialized ))
394
398
return ERR_FAIL ;
395
399
396
- NOSCHED_ENTER ( );
400
+ spin_lock_irqsave ( & timer_lock , & timer_flags );
397
401
398
402
timer_t * t = timer_find_by_id_fast (id );
399
403
if (unlikely (!t || t -> mode == TIMER_DISABLED )) {
400
- NOSCHED_LEAVE ( );
404
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
401
405
return ERR_FAIL ;
402
406
}
403
407
404
408
timer_remove_item_by_data (kcb -> timer_list , t );
405
409
t -> mode = TIMER_DISABLED ;
406
410
407
- NOSCHED_LEAVE ( );
411
+ spin_unlock_irqrestore ( & timer_lock , timer_flags );
408
412
return ERR_OK ;
409
413
}
0 commit comments