-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.c
More file actions
814 lines (732 loc) · 25.7 KB
/
Copy pathtime.c
File metadata and controls
814 lines (732 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#include "autoconf.h"
#include "libc/signal.h"
#include "libc/time.h"
#include "libc/errno.h"
#include "libc/sync.h"
#include "libc/list.h"
#include "libc/nostd.h"
#include "libc/stdio.h"
#include "libc/semaphore.h"
#include "libc/string.h"
#include "errno.h"
/* FIXME: get_current_ctx_id() should not be there */
#include "arch/cores/armv7-m/m4_syscall.h"
#ifdef CONFIG_STD_POSIX_TIMER
#define TIME_DEBUG 0
/*
* 5 timers max per task. By now EwoK only handle one timer at a time... :-/
*/
#define STD_POSIX_TIMER_MAXNUM 10
uint32_t timer_mutex = 1;
typedef struct {
timer_t id; /* Timer identifier (uint64_t), set at timer creation time. differ from 'key', upgraded with next timer trigger timetstamp
MUST be aligned on 8 bytes to avoid strd usage fault (compiler bug) */
uint32_t duration_ms; /* duration in ms (at least for initial, it periodic) */
sigev_notify_function_t sigev_notify_function;
__sigval_t sigev_value;
int sigev_notify; /* notify mode */
bool set; /* is timer active (timer_settime() has been executed at least once with
non-null it_value content */
bool postponed; /* timer has been postponed by another timer_settime(). A new node has been created.
for this node, the timer_handler() should not call the notify function. */
bool periodic; /* when setting a timer with it_interval, the timer is executed periodicaly until a new
timer_settime() reconfigure it. */
struct timespec period; /* period (interval) specification, is periodic == true */
} timer_infos_t;
list_t timer_list;
list_t unset_timer_list;
uint8_t dangling_timers;
void timer_handler(uint32_t ms);
/******************************************************************
* Local static utility functions, that interact with list backend
*/
/*
* Create a new timer node using the given key as timer identifier
*/
static int timer_create_node(struct sigevent *sevp, timer_t key, bool periodic)
{
int errcode = 0;
/* create an unset timer node in the list of timers. There is two main differences between
* unset timers and set timers:
* 1. the key used by unset timers is the full systick time of the timer_create() (not trunkated
* at milisecond multiple)
* 2. The set flag is set at false.
*
* Timer id is also used for the node identifier (node->data->id).
*/
timer_infos_t* info = NULL;
if (wmalloc((void**)&info, sizeof(timer_infos_t), ALLOC_NORMAL) != 0) {
errcode = -1;
__libstd_set_errno(ENOMEM);
goto err;
}
info->sigev_notify_function = sevp->sigev_notify_function;
info->sigev_value = sevp->sigev_value;
info->sigev_notify = sevp->sigev_notify;
info->id = key;
//memcpy(&info->id, &key, sizeof(uint64_t));
info->set = false;
info->postponed = false;
info->periodic = periodic;
info->duration_ms = 0;
#if TIME_DEBUG
printf("creating timer with id:\n");
hexdump((uint8_t*)&key, 8);
#endif
switch (list_insert(&unset_timer_list, info, key)) {
case MBED_ERROR_INVPARAM:
/* should not happen */
wfree((void**)&info);
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
break;
case MBED_ERROR_BUSY:
/* should not happen */
wfree((void**)&info);
errcode = -1;
__libstd_set_errno(EAGAIN);
goto err;
break;
case MBED_ERROR_NOMEM:
wfree((void**)&info);
errcode = -1;
__libstd_set_errno(ENOMEM);
goto err;
break;
default:
break;
}
err:
return errcode;
}
/*
* SetTime case: Set a node in a list (newly set or already set) and
* handle alarm backend.
*/
int timer_setnode(timer_t id, const struct timespec *ts, bool interval, const struct timespec *interval_ts, struct itimerspec *old)
{
int errcode = 0;
uint8_t ret;
timer_infos_t *info = NULL;
/* 1. get back timer node based on key... */
struct list_node *node = unset_timer_list.head;
/* foreach node, get back its id....
* As we have locked the timer lock, we can read the list manualy here */
/*calculating new key */
uint64_t key;
ret = sys_get_systick(&key, PREC_MICRO);
if (ret != SYS_E_DONE) {
/* calling task must be allowed to measure cycle-level timestamping */
errcode = -1;
__libstd_set_errno(EPERM);
goto err;
}
/* key is in milisecond */
uint32_t period_ms = 0;
period_ms = (ts->tv_sec * 1000);
period_ms += (ts->tv_nsec / 1000000);
/* key is in us to avoid collision */
key += (period_ms*1000);
#if TIME_DEBUG
printf("searching timer with id in unset list:\n");
hexdump((uint8_t*)&id, 8);
#endif
/* searching for node.... */
while (node != NULL) {
//if (memcmp(&((timer_infos_t*)node->data)->id, &id, sizeof(uint64_t)) == 0) {
if (((timer_infos_t*)node->data)->id == id) {
break;
}
node = node->next;
}
if (node == NULL) {
/* timer not found in unset. Is it already set ? */
goto timer_set;
}
/* handling unset timers */
if (period_ms == 0) {
/* unsetting a previously alread not set timer has no meaning... */
goto err;
}
/* for unset timers, if old, exists, set it with zeros */
if (old) {
old->it_value.tv_sec = 0;
old->it_value.tv_nsec = 0;
old->it_interval.tv_sec = 0;
old->it_interval.tv_nsec = 0;
}
/* simple case: timer is not yet set, update, and re-insert with alarm launched */
/*@ assert \valid(node); */
info = (timer_infos_t*)node->data;
/* We can remove the node without risk. Should not fail */
list_remove(&unset_timer_list, (void**)&info, node->key);
/* update node infos */
info->set = true;
info->duration_ms = period_ms;
if (interval == true) {
info->periodic = true;
if (interval_ts) {
info->period.tv_sec = interval_ts->tv_sec;
info->period.tv_nsec = interval_ts->tv_nsec;
}
}
/* and add it back with new key */
if ((ret = list_insert(&timer_list, info, key)) != MBED_ERROR_NONE) {
wfree((void**)&info);
errcode = -1;
__libstd_set_errno(ENOMEM);
goto err;
}
/* finished, we can call kernel alarm */
if (info->sigev_notify != SIGEV_THREAD) {
/* no thread to notify */
goto err;
}
goto call_alarm;
timer_set:
#if TIME_DEBUG
printf("... not found. searching in set list.\n");
#endif
node = timer_list.head;
while (node != NULL) {
//if (memcmp(&((timer_infos_t*)node->data)->id, &id, sizeof(uint64_t)) == 0) {
if (((timer_infos_t*)node->data)->id == id) {
break;
}
node = node->next;
}
if (node == NULL) {
/* not in timer_list neither... not found then*/
#if TIME_DEBUG
printf("timer not found in set timers\n");
#endif
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
info = (timer_infos_t*)node->data;
/* when timer already set and 'old' is non-null, set the previously
* configured values to it */
if (old) {
old->it_value.tv_sec = info->duration_ms / 1000;
old->it_value.tv_nsec = (info->duration_ms % 1000) * 1000000;
if (info->periodic == false) {
old->it_interval.tv_sec = 0;
old->it_interval.tv_nsec = 0;
} else {
old->it_interval.tv_sec = info->period.tv_sec;
old->it_interval.tv_nsec = info->period.tv_nsec;
}
}
/* 3. the node is already set*/
/* for all nodes having the same id (including that one),
* mark them as 'postponed'. */
info->postponed = true;
while (node != NULL) {
//if (memcmp(&((timer_infos_t*)node->data)->id, &id, sizeof(uint64_t)) == 0) {
if (((timer_infos_t*)node->data)->id == id) {
/* old timer configs, even if triggered by kernel, will
* not execute upper hanlder, neither generate a new
* timer period */
((timer_infos_t*)node->data)->postponed = true;
((timer_infos_t*)node->data)->periodic = false;
}
node = node->next;
}
if (period_ms == 0) {
/* we require to set a node with NULL value, meaning cleaning a timer. Previous instances has been
* postponed, we can leave now... */
goto err;
}
/* all previously set nodes are set as postponed. When
* corresponding alarm will be triggered by the kernel,
* the handler will not execute upper handler. */
/* create a new node for currently set timer */
timer_infos_t *new_info = NULL;
if (wmalloc((void**)&new_info, sizeof(timer_infos_t), ALLOC_NORMAL) != 0) {
errcode = -1;
__libstd_set_errno(ENOMEM);
goto err;
}
new_info->sigev_notify_function = info->sigev_notify_function;
new_info->sigev_value = info->sigev_value;
new_info->sigev_notify = info->sigev_notify;
new_info->id = info->id;
//memcpy(&new_info->id, &info->id, sizeof(uint64_t));
new_info->set = true;
new_info->postponed = false;
if (interval == true) {
new_info->periodic = true;
}
/* insert the new node with the new key */
if ((ret = list_insert(&timer_list, new_info, key)) != MBED_ERROR_NONE) {
wfree((void**)&new_info);
errcode = -1;
__libstd_set_errno(ENOMEM);
goto err;
}
/* finished, we can call kernel alarm */
if (new_info->sigev_notify != SIGEV_THREAD) {
/* no thread to notify */
goto err;
}
call_alarm:
/* call sigalarm() */
switch (sys_alarm(period_ms, timer_handler)) {
case SYS_E_DONE:
goto err;
case SYS_E_DENIED:
/* TODO: remove node */
errcode = -1;
__libstd_set_errno(EPERM);
goto err;
break;
default:
/* TODO: remove node */
errcode = -1;
__libstd_set_errno(EAGAIN);
goto err;
break;
}
err:
return errcode;
}
/* timer handler that is effectively called by the kernel */
void timer_handler(uint32_t ms)
{
uint64_t key;
mbed_error_t errcode;
ms = ms;
int ret;
if (mutex_trylock(&timer_mutex) == false) {
/* didn't manage to get mutex lock without blocking (another thread is blocking it ?) */
set_u8_with_membarrier(&dangling_timers, dangling_timers + 1);
goto err_nolock;
}
if (timer_list.size == 0) {
/* this should **not** happen! */
goto err;
}
struct list_node *node = timer_list.head;
/*@ assert \valid(timer_list->head); */
timer_infos_t *info = NULL;
if (dangling_timers > 0) {
/* one or more previously executed list accesses were locked by another thread.
* This prevent the handler from properly accessing & removing the timer node.
* In that case, for each such 'dangling' timer node, remove it here */
if (timer_list.size < dangling_timers) {
/* this should not happen, as only this function remove nodes */
goto err;
}
for (uint8_t i = 0; i < dangling_timers; ++i) {
errcode = list_remove(&timer_list, (void**)&info, timer_list.head->key);
wfree((void**)&info);
set_u8_with_membarrier(&dangling_timers, dangling_timers - 1);
}
/*@ assert dangling_timers == 0; */
/* All dangling removed */
}
/* when coming back from the kernel, we don't have the timer key with us. To be sure to get
* back the correct timer node, the ordered list of timer is using a timer timeout timestamp based
* key to ensure an ordered listing.
* As a consequence, when the handler is called, the terminated timer is **always** the first node of the list.
*/
//memcpy(&key, &node->key, sizeof(uint64_t));
key = node->key;
/* remove from activated timer list */
errcode = list_remove(&timer_list, (void**)&info, key);
switch (errcode) {
case MBED_ERROR_INVPARAM:
/* should not happen here */
goto err;
break;
case MBED_ERROR_NOSTORAGE:
/* should not happen, checked before */
goto err;
break;
case MBED_ERROR_BUSY:
/* should not happen as timer lock is set */
goto err;
break;
case MBED_ERROR_NOTFOUND:
/* timer not found! */
goto err;
break;
default:
break;
}
struct sigevent sevp = { 0 };
sevp.sigev_notify_function = info->sigev_notify_function;
sevp.sigev_notify = info->sigev_notify;
sevp.sigev_value = info->sigev_value;
if (info->postponed == true) {
/* the current timer node has been postponed. Another timer node exists (or has existed)
* and has been/will be executed in order to execute the associated callback. By now,
* just do nothing. */
struct list_node *next_node = node;
bool not_the_last = false;
/* is there other future timers (postponed or not) for the same timer id ? */
while (next_node && next_node->next) {
next_node = next_node->next;
if (((timer_infos_t*)(next_node->data))->id == info->id) {
/* future timers with the same timer id will trig */
not_the_last = true;
}
}
if (not_the_last == false) {
/* current postponed timer is the last one for this id, no more timer trigger. We push back the
* timer id to the inactive timer list */
if ((ret = timer_create_node(&sevp, info->id, false)) == -1) {
#if TIME_DEBUG
printf("[handler] failed to rearm timer (creation)\n");
#endif
wfree((void**)&info);
goto err;
}
}
} else {
if (info->sigev_notify == SIGEV_THREAD) {
/* upper thread execution is requested. The callback **must** be set as it has been checked
* at creation time. */
info->sigev_notify_function(info->sigev_value);
if (info->periodic == true) {
/* reinsert a new timer, by creating a new one with the same key.... */
struct timespec ts = { 0 };
ts.tv_nsec = info->period.tv_nsec;
ts.tv_sec = info->period.tv_sec;
if ((ret = timer_create_node(&sevp, info->id, true)) == -1) {
#if TIME_DEBUG
printf("[handler] failed to rearm timer (creation)\n");
#endif
wfree((void**)&info);
goto err;
}
if ((ret = timer_setnode(info->id, &ts, true, &info->period, NULL)) == -1) {
#if TIME_DEBUG
printf("[handler] failed to rearm timer (set)\n");
#endif
wfree((void**)&info);
goto err;
}
} else {
/* not periodic, keep the timerid as created but not set for future timer_settime() */
if ((ret = timer_create_node(&sevp, info->id, false)) == -1) {
#if TIME_DEBUG
printf("[handler] failed to keep timer id (creation)\n");
#endif
wfree((void**)&info);
goto err;
}
}
}
}
wfree((void**)&info);
err:
mutex_unlock(&timer_mutex);
err_nolock:
return;
}
/**************************************************************************
* Exported functions part 1; timers
*/
/* zeroify function, called at task preinit.*/
void timer_initialize(void)
{
list_create(STD_POSIX_TIMER_MAXNUM, &timer_list);
list_create(STD_POSIX_TIMER_MAXNUM, &unset_timer_list);
#if TIME_DEBUG
printf("timer list:\n");
printf("max: %d\n", timer_list.max);
printf("size: %d\n", timer_list.size);
printf("head: %x\n", timer_list.head);
printf("mutex: %x\n", timer_list.lock);
#endif
dangling_timers = 0;
}
/*
* Create a timer (timer is not activated here).
*/
int timer_create(clockid_t clockid, struct sigevent *sevp, timer_t *timerid)
{
int errcode = 0;
uint8_t ret;
/* by now, CLOCK_REALTIME not supported */
if (clockid == CLOCK_REALTIME) {
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
/* other input params sanitation */
if (clockid != CLOCK_MONOTONIC || sevp == NULL || timerid == NULL) {
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
/* by now, SIGEV_SIGNAL is not supported */
if (sevp->sigev_notify == SIGEV_SIGNAL) {
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
/* other input params sanitation */
if (sevp->sigev_notify != SIGEV_THREAD && sevp->sigev_notify != SIGEV_NONE) {
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
/* SIGEV_THREAD case: check notify function */
if (sevp->sigev_notify == SIGEV_THREAD && sevp->sigev_notify_function == NULL) {
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
/* timer identifier is the current cycle id. To avoid collision in case ot SMP
* system, we use a semaphore to lock a shared ressource between getting current
* timestamp, then unlock the semaphore. Doing this, even in SMP systems, concurrent
* timer_create() will have separated timer_id.
*/
if (get_current_ctx_id() == CTX_ISR) {
/* ISR thread: trying nonblocking lock... */
if (mutex_trylock(&timer_mutex) == false) {
/* didn't manage to get mutex lock without blocking (another thread is blocking it ?) */
errcode = -1;
__libstd_set_errno(EAGAIN);
goto err;
}
} else {
mutex_lock(&timer_mutex);
}
ret = sys_get_systick(timerid, PREC_CYCLE);
if (ret != SYS_E_DONE) {
/* calling task must be allowed to measure cycle-level timestamping */
errcode = -1;
__libstd_set_errno(EPERM);
goto err;
}
errcode = timer_create_node(sevp, *timerid, false);
mutex_unlock(&timer_mutex);
err:
return errcode;
}
/*
* Activate timer
*
* At settime():
* if the node set at create() time is not yet active:
* - it is get back (using timerid==key)
* - upgraded by setting the key properly with the new itimespec informations
* - re-added correctly in the list accordingly
* if the node set at create() time is already active (a previous create() has been done)
* - all potential existing nodes are flagued as 'postponed' (the handler will not call the upper layer)
* - a new node is created using the itimespec information and set in the list correctly (it may be **before** a previously postoned
* timer if the time is reduced
*
* The alarm request is sent to the kernel.
*/
int timer_settime(timer_t timerid, int flags __attribute__((unused)), const struct itimerspec *new_value, struct itimerspec *old_value __attribute__((unused)))
{
int errcode = 0;
const struct timespec *ts;
bool interval = false;
bool cleaning = false;
/* Sanitize first. old_value is allowed to be NULL */
if (new_value == NULL) {
errcode = -1;
__libstd_set_errno(EFAULT);
goto err;
}
/* select type of setting (value or interval) */
/*TODO: FIX1: if it_value fields are 0 -> unset timer */
ts = &new_value->it_value;
if (new_value->it_value.tv_sec == 0 && new_value->it_value.tv_nsec == 0) {
/* simply clean previously set timer */
cleaning = true;
}
/* if both interval and value are non-null, this is a periodic timer */
if ((new_value->it_interval.tv_sec != 0 || new_value->it_interval.tv_nsec != 0) &&
(new_value->it_value.tv_sec != 0 || new_value->it_value.tv_nsec != 0)) {
/* an periodic interval is requested after first trigger (set by it_value)*/
interval = true;
}
/* when not unsetting a timer, timer specs must be large enough */
if (cleaning == false) {
if (ts->tv_sec == 0 && ts->tv_nsec < 1000000) {
/* too short timer step */
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
if (ts->tv_nsec > 999999999) {
/* nsec bigger than 1 sec (POSIX compliance) */
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
if (interval == true && new_value->it_interval.tv_sec == 0 && new_value->it_interval.tv_nsec < 1000000) {
/* too short interval */
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
}
if (get_current_ctx_id() == CTX_ISR) {
/* ISR thread: trying nonblocking lock... */
if (mutex_trylock(&timer_mutex) == false) {
/* didn't manage to get mutex lock without blocking (another thread is blocking it ?) */
errcode = -1;
__libstd_set_errno(EAGAIN);
goto err;
}
} else {
mutex_lock(&timer_mutex);
}
errcode = timer_setnode(timerid, &new_value->it_value, interval, &new_value->it_interval, old_value);
mutex_unlock(&timer_mutex);
err:
return errcode;
}
int timer_gettime(timer_t timerid, struct itimerspec *curr_value)
{
uint64_t timer_us;
uint64_t now_us;
uint64_t eta_us;
int errcode = 0;
timer_infos_t *info = NULL;
struct list_node *node = NULL;
uint8_t ret;
/* Sanitize first */
if (curr_value == NULL) {
errcode = -1;
__libstd_set_errno(EFAULT);
goto err;
}
if (get_current_ctx_id() == CTX_ISR) {
/* ISR thread: trying nonblocking lock... */
if (mutex_trylock(&timer_mutex) == false) {
/* didn't manage to get mutex lock without blocking (another thread is blocking it ?) */
errcode = -1;
__libstd_set_errno(EAGAIN);
goto err;
}
} else {
mutex_lock(&timer_mutex);
}
if (timer_list.size == 0) {
/* no timer set */
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
node = timer_list.head;
/*@ assert \valid(node);*/
/* searching no set timers first */
while (node != NULL) {
info = node->data;
//if (memcmp(&info->id, &timerid, sizeof(uint64_t)) == 0) {
if (info->id == timerid) {
if (&info->postponed == false) {
break;
}
}
node = node->next;
}
if (node == NULL) {
/* timer not found. Is timer unset ? */
node = unset_timer_list.head;
while (node != NULL) {
info = node->data;
// if (memcmp(&info->id, &timerid, sizeof(uint64_t)) == 0) {
if (info->id == timerid) {
break;
}
node = node->next;
}
if (node == NULL) {
errcode = -1;
__libstd_set_errno(EINVAL);
goto err_lock;
}
}
/*@ assert \valid(node); */
/*@ assert \valid(info); */
timer_us = info->id;
if (info->set == false) {
/* unset timer */
curr_value->it_value.tv_sec = 0;
curr_value->it_value.tv_nsec = 0;
goto err_lock;
}
eta_us = (timer_us + (info->duration_ms*1000)) - now_us;
/* timer id is the time (in us) when the timer has been set */
//memcpy(&timer_us, &info->id, sizeof(uint64_t));
timer_us = info->id;
ret = sys_get_systick(&now_us, PREC_MICRO);
if (ret != SYS_E_DONE) {
/* calling task must be allowed to measure cycle-level timestamping */
errcode = -1;
__libstd_set_errno(EPERM);
goto err;
}
if (info->periodic == true) {
curr_value->it_interval.tv_nsec = (info->duration_ms % 1000) * 1000000;
curr_value->it_interval.tv_sec = info->duration_ms / 1000;
}
curr_value->it_value.tv_nsec = (eta_us % 1000000) * 1000;
curr_value->it_value.tv_sec = eta_us / 1000000;
err_lock:
mutex_unlock(&timer_mutex);
err:
return errcode;
}
/**************************************************************************
* Exported functions part 1; clock
*/
#if 0
/* TODO or not TODO, that is the question... */
int clock_getres(clockid_t clockid, struct timespec *res)
{
/* On Ework the precision of timers is in ms, depending on the
* CONFIG_SCHED_PERIOD value for inccuracy. Usual scheduling period
* is about 10 ms but can be more or less.
*/
}
#endif
int clock_gettime(clockid_t clockid, struct timespec *tp)
{
int errcode = 0;
uint64_t time;
/* sanitation */
if (tp == NULL) {
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
/* by now, no support for RTC clock. For boards with RTC clocks, add a config
* option to allow CLOCK_REALTIME */
if (clockid != CLOCK_MONOTONIC) {
errcode = -1;
__libstd_set_errno(EINVAL);
goto err;
}
/* as we can't check the level of permission we have (depend on the task),
* we first try the most precise measurement, and continue upto the less
* precise one.
* The first valid measurement makes the function returns */
if (sys_get_systick(&time, PREC_MICRO) == SYS_E_DONE) {
tp->tv_nsec = (time % 1000000) * 1000;
tp->tv_sec = (time / 1000000);
goto err;
}
if (sys_get_systick(&time, PREC_MILLI) == SYS_E_DONE) {
tp->tv_nsec = (time % 1000) * 1000000;
tp->tv_sec = (time / 1000);
goto err;
}
/* EPERM is not a POSIX defined return value, but time measurement is controled on EwoK */
errcode = -1;
__libstd_set_errno(EPERM);
err:
return errcode;
}
#endif