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