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.c --
|
---|
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 : Fri Jul 21 22:35:18 2017
|
---|
13 | // Update Count : 1
|
---|
14 | //
|
---|
15 |
|
---|
16 | extern "C" {
|
---|
17 | #include <errno.h>
|
---|
18 | #include <stdio.h>
|
---|
19 | #include <string.h>
|
---|
20 | #include <unistd.h>
|
---|
21 | #include <sys/time.h>
|
---|
22 | }
|
---|
23 |
|
---|
24 | #include "alarm.h"
|
---|
25 | #include "kernel_private.h"
|
---|
26 | #include "preemption.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | static inline void ?{}( itimerval & this, __cfa_time_t * alarm ) with( this ) {
|
---|
30 | it_value.tv_sec = alarm->val / (1`cfa_s).val; // seconds
|
---|
31 | it_value.tv_usec = max( (alarm->val % (1`cfa_s).val) / (1`cfa_us).val, 1000 ); // microseconds
|
---|
32 | it_interval.tv_sec = 0;
|
---|
33 | it_interval.tv_usec = 0;
|
---|
34 | }
|
---|
35 |
|
---|
36 | static inline void ?{}( __cfa_time_t & this, timespec * curr ) {
|
---|
37 | uint64_t secs = curr->tv_sec;
|
---|
38 | uint64_t nsecs = curr->tv_nsec;
|
---|
39 | this.val = from_s(secs).val + nsecs;
|
---|
40 | }
|
---|
41 |
|
---|
42 | //=============================================================================================
|
---|
43 | // Clock logic
|
---|
44 | //=============================================================================================
|
---|
45 |
|
---|
46 | __cfa_time_t __kernel_get_time() {
|
---|
47 | timespec curr;
|
---|
48 | clock_gettime( CLOCK_REALTIME, &curr );
|
---|
49 | return (__cfa_time_t){ &curr };
|
---|
50 | }
|
---|
51 |
|
---|
52 | void __kernel_set_timer( __cfa_time_t alarm ) {
|
---|
53 | itimerval val = { &alarm };
|
---|
54 | setitimer( ITIMER_REAL, &val, NULL );
|
---|
55 | }
|
---|
56 |
|
---|
57 | //=============================================================================================
|
---|
58 | // Alarm logic
|
---|
59 | //=============================================================================================
|
---|
60 |
|
---|
61 | void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = 0`cfa_s, __cfa_time_t period = 0`cfa_s ) with( this ) {
|
---|
62 | this.thrd = thrd;
|
---|
63 | this.alarm = alarm;
|
---|
64 | this.period = period;
|
---|
65 | next = 0;
|
---|
66 | set = false;
|
---|
67 | kernel_alarm = false;
|
---|
68 | }
|
---|
69 |
|
---|
70 | void ?{}( alarm_node_t & this, processor * proc, __cfa_time_t alarm = 0`cfa_s, __cfa_time_t period = 0`cfa_s ) with( this ) {
|
---|
71 | this.proc = proc;
|
---|
72 | this.alarm = alarm;
|
---|
73 | this.period = period;
|
---|
74 | next = 0;
|
---|
75 | set = false;
|
---|
76 | kernel_alarm = true;
|
---|
77 | }
|
---|
78 |
|
---|
79 | void ^?{}( alarm_node_t & this ) {
|
---|
80 | if( this.set ) {
|
---|
81 | unregister_self( &this );
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | __cfaabi_dbg_debug_do( bool validate( alarm_list_t * this ) {
|
---|
86 | alarm_node_t ** it = &this->head;
|
---|
87 | while( (*it) ) {
|
---|
88 | it = &(*it)->next;
|
---|
89 | }
|
---|
90 |
|
---|
91 | return it == this->tail;
|
---|
92 | })
|
---|
93 |
|
---|
94 | static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
|
---|
95 | verify( !n->next );
|
---|
96 | if( p == this->tail ) {
|
---|
97 | this->tail = &n->next;
|
---|
98 | }
|
---|
99 | else {
|
---|
100 | n->next = *p;
|
---|
101 | }
|
---|
102 | *p = n;
|
---|
103 |
|
---|
104 | verify( validate( this ) );
|
---|
105 | }
|
---|
106 |
|
---|
107 | void insert( alarm_list_t * this, alarm_node_t * n ) {
|
---|
108 | alarm_node_t ** it = &this->head;
|
---|
109 | while( (*it) && (n->alarm > (*it)->alarm) ) {
|
---|
110 | it = &(*it)->next;
|
---|
111 | }
|
---|
112 |
|
---|
113 | insert_at( this, n, it );
|
---|
114 |
|
---|
115 | verify( validate( this ) );
|
---|
116 | }
|
---|
117 |
|
---|
118 | alarm_node_t * pop( alarm_list_t * this ) {
|
---|
119 | alarm_node_t * head = this->head;
|
---|
120 | if( head ) {
|
---|
121 | this->head = head->next;
|
---|
122 | if( !head->next ) {
|
---|
123 | this->tail = &this->head;
|
---|
124 | }
|
---|
125 | head->next = NULL;
|
---|
126 | }
|
---|
127 | verify( validate( this ) );
|
---|
128 | return head;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
|
---|
132 | verify( it );
|
---|
133 | verify( (*it) == n );
|
---|
134 |
|
---|
135 | (*it) = n->next;
|
---|
136 | if( !n-> next ) {
|
---|
137 | this->tail = it;
|
---|
138 | }
|
---|
139 | n->next = NULL;
|
---|
140 |
|
---|
141 | verify( validate( this ) );
|
---|
142 | }
|
---|
143 |
|
---|
144 | static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
|
---|
145 | alarm_node_t ** it = &this->head;
|
---|
146 | while( (*it) && (*it) != n ) {
|
---|
147 | it = &(*it)->next;
|
---|
148 | }
|
---|
149 |
|
---|
150 | verify( validate( this ) );
|
---|
151 |
|
---|
152 | if( *it ) { remove_at( this, n, it ); }
|
---|
153 |
|
---|
154 | verify( validate( this ) );
|
---|
155 | }
|
---|
156 |
|
---|
157 | void register_self( alarm_node_t * this ) {
|
---|
158 | alarm_list_t * alarms = &event_kernel->alarms;
|
---|
159 |
|
---|
160 | disable_interrupts();
|
---|
161 | lock( event_kernel->lock __cfaabi_dbg_ctx2 );
|
---|
162 | {
|
---|
163 | verify( validate( alarms ) );
|
---|
164 | bool first = !alarms->head;
|
---|
165 |
|
---|
166 | insert( alarms, this );
|
---|
167 | if( first ) {
|
---|
168 | __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
|
---|
169 | }
|
---|
170 | }
|
---|
171 | unlock( event_kernel->lock );
|
---|
172 | this->set = true;
|
---|
173 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
174 | }
|
---|
175 |
|
---|
176 | void unregister_self( alarm_node_t * this ) {
|
---|
177 | disable_interrupts();
|
---|
178 | lock( event_kernel->lock __cfaabi_dbg_ctx2 );
|
---|
179 | {
|
---|
180 | verify( validate( &event_kernel->alarms ) );
|
---|
181 | remove( &event_kernel->alarms, this );
|
---|
182 | }
|
---|
183 | unlock( event_kernel->lock );
|
---|
184 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
185 | this->set = false;
|
---|
186 | }
|
---|
187 |
|
---|
188 | // Local Variables: //
|
---|
189 | // mode: c //
|
---|
190 | // tab-width: 4 //
|
---|
191 | // End: //
|
---|