1 | // -*- Mode: CFA -*- |
---|
2 | // |
---|
3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
4 | // |
---|
5 | // The contents of this file are covered under the licence agreement in the |
---|
6 | // file "LICENCE" distributed with Cforall. |
---|
7 | // |
---|
8 | // alarm.h -- |
---|
9 | // |
---|
10 | // Author : Thierry Delisle |
---|
11 | // Created On : Fri Jun 2 11:31:25 2017 |
---|
12 | // Last Modified By : Thierry Delisle |
---|
13 | // Last Modified On : -- |
---|
14 | // Update Count : 0 |
---|
15 | // |
---|
16 | |
---|
17 | #ifndef ALARM_H |
---|
18 | #define ALARM_H |
---|
19 | |
---|
20 | #include <stdbool.h> |
---|
21 | #include <stdint.h> |
---|
22 | |
---|
23 | #include "assert" |
---|
24 | |
---|
25 | struct thread_desc; |
---|
26 | struct processor; |
---|
27 | |
---|
28 | struct timespec; |
---|
29 | struct itimerval; |
---|
30 | |
---|
31 | //============================================================================================= |
---|
32 | // time type |
---|
33 | //============================================================================================= |
---|
34 | |
---|
35 | struct __cfa_time_t { |
---|
36 | uint64_t val; |
---|
37 | }; |
---|
38 | |
---|
39 | // ctors |
---|
40 | void ?{}( __cfa_time_t * this ); |
---|
41 | void ?{}( __cfa_time_t * this, zero_t zero ); |
---|
42 | void ?{}( __cfa_time_t * this, timespec * curr ); |
---|
43 | void ?{}( itimerval * this, __cfa_time_t * alarm ); |
---|
44 | |
---|
45 | __cfa_time_t ?=?( __cfa_time_t * this, zero_t rhs ); |
---|
46 | |
---|
47 | // logical ops |
---|
48 | static inline bool ?==?( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val == rhs.val; } |
---|
49 | static inline bool ?!=?( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val != rhs.val; } |
---|
50 | static inline bool ?>? ( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val > rhs.val; } |
---|
51 | static inline bool ?<? ( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val < rhs.val; } |
---|
52 | static inline bool ?>=?( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val >= rhs.val; } |
---|
53 | static inline bool ?<=?( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val <= rhs.val; } |
---|
54 | |
---|
55 | static inline bool ?==?( __cfa_time_t lhs, zero_t rhs ) { return lhs.val == rhs; } |
---|
56 | static inline bool ?!=?( __cfa_time_t lhs, zero_t rhs ) { return lhs.val != rhs; } |
---|
57 | static inline bool ?>? ( __cfa_time_t lhs, zero_t rhs ) { return lhs.val > rhs; } |
---|
58 | static inline bool ?<? ( __cfa_time_t lhs, zero_t rhs ) { return lhs.val < rhs; } |
---|
59 | static inline bool ?>=?( __cfa_time_t lhs, zero_t rhs ) { return lhs.val >= rhs; } |
---|
60 | static inline bool ?<=?( __cfa_time_t lhs, zero_t rhs ) { return lhs.val <= rhs; } |
---|
61 | |
---|
62 | // addition/substract |
---|
63 | static inline __cfa_time_t ?+?( __cfa_time_t lhs, __cfa_time_t rhs ) { |
---|
64 | __cfa_time_t ret; |
---|
65 | ret.val = lhs.val + rhs.val; |
---|
66 | return ret; |
---|
67 | } |
---|
68 | |
---|
69 | static inline __cfa_time_t ?-?( __cfa_time_t lhs, __cfa_time_t rhs ) { |
---|
70 | __cfa_time_t ret; |
---|
71 | ret.val = lhs.val - rhs.val; |
---|
72 | return ret; |
---|
73 | } |
---|
74 | |
---|
75 | __cfa_time_t from_s ( uint64_t ); |
---|
76 | __cfa_time_t from_ms( uint64_t ); |
---|
77 | __cfa_time_t from_us( uint64_t ); |
---|
78 | __cfa_time_t from_ns( uint64_t ); |
---|
79 | |
---|
80 | extern __cfa_time_t zero_time; |
---|
81 | |
---|
82 | //============================================================================================= |
---|
83 | // Clock logic |
---|
84 | //============================================================================================= |
---|
85 | |
---|
86 | __cfa_time_t __kernel_get_time(); |
---|
87 | void __kernel_set_timer( __cfa_time_t alarm ); |
---|
88 | |
---|
89 | //============================================================================================= |
---|
90 | // Alarm logic |
---|
91 | //============================================================================================= |
---|
92 | |
---|
93 | struct alarm_node_t { |
---|
94 | __cfa_time_t alarm; // time when alarm goes off |
---|
95 | __cfa_time_t period; // if > 0 => period of alarm |
---|
96 | alarm_node_t * next; // intrusive link list field |
---|
97 | |
---|
98 | union { |
---|
99 | thread_desc * thrd; // thrd who created event |
---|
100 | processor * proc; // proc who created event |
---|
101 | }; |
---|
102 | |
---|
103 | bool set :1; // whether or not the alarm has be registered |
---|
104 | bool kernel_alarm :1; // true if this is not a user defined alarm |
---|
105 | }; |
---|
106 | |
---|
107 | typedef alarm_node_t ** __alarm_it_t; |
---|
108 | |
---|
109 | void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ); |
---|
110 | void ?{}( alarm_node_t * this, processor * proc, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ); |
---|
111 | void ^?{}( alarm_node_t * this ); |
---|
112 | |
---|
113 | struct alarm_list_t { |
---|
114 | alarm_node_t * head; |
---|
115 | __alarm_it_t tail; |
---|
116 | }; |
---|
117 | |
---|
118 | static inline void ?{}( alarm_list_t * this ) { |
---|
119 | this->head = 0; |
---|
120 | this->tail = &this->head; |
---|
121 | } |
---|
122 | |
---|
123 | void insert( alarm_list_t * this, alarm_node_t * n ); |
---|
124 | alarm_node_t * pop( alarm_list_t * this ); |
---|
125 | |
---|
126 | void register_self ( alarm_node_t * this ); |
---|
127 | void unregister_self( alarm_node_t * this ); |
---|
128 | |
---|
129 | #endif |
---|
130 | |
---|
131 | // Local Variables: // |
---|
132 | // mode: CFA // |
---|
133 | // tab-width: 6 // |
---|
134 | // End: // |
---|