source: src/libcfa/concurrency/alarm.c@ e1e8408

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since e1e8408 was 9236060, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' into references

  • Property mode set to 100644
File size: 5.4 KB
Line 
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
16extern "C" {
17#include <errno.h>
18#include <stdio.h>
19#include <string.h>
20#include <time.h>
21#include <unistd.h>
22#include <sys/time.h>
23}
24
25#include "libhdr.h"
26
27#include "alarm.h"
28#include "kernel_private.h"
29#include "preemption.h"
30
31//=============================================================================================
32// time type
33//=============================================================================================
34
35#define one_second 1_000_000_000ul
36#define one_milisecond 1_000_000ul
37#define one_microsecond 1_000ul
38#define one_nanosecond 1ul
39
40__cfa_time_t zero_time = { 0 };
41
42void ?{}( __cfa_time_t & this ) { this.val = 0; }
43void ?{}( __cfa_time_t & this, zero_t zero ) { this.val = 0; }
44
45void ?{}( itimerval & this, __cfa_time_t * alarm ) {
46 this.it_value.tv_sec = alarm->val / one_second; // seconds
47 this.it_value.tv_usec = max( (alarm->val % one_second) / one_microsecond, 1000 ); // microseconds
48 this.it_interval.tv_sec = 0;
49 this.it_interval.tv_usec = 0;
50}
51
52
53void ?{}( __cfa_time_t & this, timespec * curr ) {
54 uint64_t secs = curr->tv_sec;
55 uint64_t nsecs = curr->tv_nsec;
56 this.val = (secs * one_second) + nsecs;
57}
58
59__cfa_time_t ?=?( __cfa_time_t & this, zero_t rhs ) {
60 this.val = 0;
61 return this;
62}
63
64__cfa_time_t from_s ( uint64_t val ) { __cfa_time_t ret; ret.val = val * 1_000_000_000ul; return ret; }
65__cfa_time_t from_ms( uint64_t val ) { __cfa_time_t ret; ret.val = val * 1_000_000ul; return ret; }
66__cfa_time_t from_us( uint64_t val ) { __cfa_time_t ret; ret.val = val * 1_000ul; return ret; }
67__cfa_time_t from_ns( uint64_t val ) { __cfa_time_t ret; ret.val = val * 1ul; return ret; }
68
69//=============================================================================================
70// Clock logic
71//=============================================================================================
72
73__cfa_time_t __kernel_get_time() {
74 timespec curr;
75 clock_gettime( CLOCK_REALTIME, &curr );
76 return (__cfa_time_t){ &curr };
77}
78
79void __kernel_set_timer( __cfa_time_t alarm ) {
80 itimerval val = { &alarm };
81 setitimer( ITIMER_REAL, &val, NULL );
82}
83
84//=============================================================================================
85// Alarm logic
86//=============================================================================================
87
88void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
89 this.thrd = thrd;
90 this.alarm = alarm;
91 this.period = period;
92 this.next = 0;
93 this.set = false;
94 this.kernel_alarm = false;
95}
96
97void ?{}( alarm_node_t & this, processor * proc, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
98 this.proc = proc;
99 this.alarm = alarm;
100 this.period = period;
101 this.next = 0;
102 this.set = false;
103 this.kernel_alarm = true;
104}
105
106void ^?{}( alarm_node_t & this ) {
107 if( this.set ) {
108 unregister_self( &this );
109 }
110}
111
112LIB_DEBUG_DO( bool validate( alarm_list_t * this ) {
113 alarm_node_t ** it = &this->head;
114 while( (*it) ) {
115 it = &(*it)->next;
116 }
117
118 return it == this->tail;
119})
120
121static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
122 verify( !n->next );
123 if( p == this->tail ) {
124 this->tail = &n->next;
125 }
126 else {
127 n->next = *p;
128 }
129 *p = n;
130
131 verify( validate( this ) );
132}
133
134void insert( alarm_list_t * this, alarm_node_t * n ) {
135 alarm_node_t ** it = &this->head;
136 while( (*it) && (n->alarm > (*it)->alarm) ) {
137 it = &(*it)->next;
138 }
139
140 insert_at( this, n, it );
141
142 verify( validate( this ) );
143}
144
145alarm_node_t * pop( alarm_list_t * this ) {
146 alarm_node_t * head = this->head;
147 if( head ) {
148 this->head = head->next;
149 if( !head->next ) {
150 this->tail = &this->head;
151 }
152 head->next = NULL;
153 }
154 verify( validate( this ) );
155 return head;
156}
157
158static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
159 verify( it );
160 verify( (*it) == n );
161
162 (*it) = n->next;
163 if( !n-> next ) {
164 this->tail = it;
165 }
166 n->next = NULL;
167
168 verify( validate( this ) );
169}
170
171static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
172 alarm_node_t ** it = &this->head;
173 while( (*it) && (*it) != n ) {
174 it = &(*it)->next;
175 }
176
177 verify( validate( this ) );
178
179 if( *it ) { remove_at( this, n, it ); }
180
181 verify( validate( this ) );
182}
183
184void register_self( alarm_node_t * this ) {
185 alarm_list_t * alarms = &event_kernel->alarms;
186
187 disable_interrupts();
188 lock( &event_kernel->lock DEBUG_CTX2 );
189 {
190 verify( validate( alarms ) );
191 bool first = !alarms->head;
192
193 insert( alarms, this );
194 if( first ) {
195 __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
196 }
197 }
198 unlock( &event_kernel->lock );
199 this->set = true;
200 enable_interrupts( DEBUG_CTX );
201}
202
203void unregister_self( alarm_node_t * this ) {
204 disable_interrupts();
205 lock( &event_kernel->lock DEBUG_CTX2 );
206 {
207 verify( validate( &event_kernel->alarms ) );
208 remove( &event_kernel->alarms, this );
209 }
210 unlock( &event_kernel->lock );
211 enable_interrupts( DEBUG_CTX );
212 this->set = false;
213}
214
215// Local Variables: //
216// mode: c //
217// tab-width: 4 //
218// End: //
Note: See TracBrowser for help on using the repository browser.