source: src/libcfa/concurrency/alarm.c@ 405c592

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 405c592 was 0b33412, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Updated tests to use verify instead of assert

  • Property mode set to 100644
File size: 5.0 KB
Line 
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.c --
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
17extern "C" {
18#include <errno.h>
19#include <stdio.h>
20#include <string.h>
21#include <time.h>
22#include <unistd.h>
23#include <sys/time.h>
24}
25
26#include "alarm.h"
27#include "kernel_private.h"
28#include "libhdr.h"
29#include "preemption.h"
30
31//=============================================================================================
32// Clock logic
33//=============================================================================================
34
35__cfa_time_t __kernel_get_time() {
36 timespec curr;
37 clock_gettime( CLOCK_REALTIME, &curr );
38 __cfa_time_t curr_time = ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
39 LIB_DEBUG_DO(
40 char text[256];
41 __attribute__((unused)) int len = snprintf( text, 256, "Kernel : current time is %lu\n", curr_time );
42 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
43 );
44 return curr_time;
45}
46
47void __kernel_set_timer( __cfa_time_t alarm ) {
48
49 LIB_DEBUG_DO(
50 char text[256];
51 __attribute__((unused)) int len = snprintf( text, 256, "Kernel : set timer to %lu\n", (__cfa_time_t)alarm );
52 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
53 );
54
55 itimerval val;
56 val.it_value.tv_sec = alarm / TIMEGRAN; // seconds
57 val.it_value.tv_usec = (alarm % TIMEGRAN) / ( TIMEGRAN / 1_000_000L ); // microseconds
58 val.it_interval.tv_sec = 0;
59 val.it_interval.tv_usec = 0;
60 setitimer( ITIMER_REAL, &val, NULL );
61}
62
63//=============================================================================================
64// Alarm logic
65//=============================================================================================
66
67void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
68 this->thrd = thrd;
69 this->alarm = alarm;
70 this->period = period;
71 this->next = 0;
72 this->set = false;
73 this->kernel_alarm = false;
74}
75
76void ?{}( alarm_node_t * this, processor * proc, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
77 this->proc = proc;
78 this->alarm = alarm;
79 this->period = period;
80 this->next = 0;
81 this->set = false;
82 this->kernel_alarm = true;
83}
84
85void ^?{}( alarm_node_t * this ) {
86 if( this->set ) {
87 unregister_self( this );
88 }
89}
90
91LIB_DEBUG_DO( bool validate( alarm_list_t * this ) {
92 alarm_node_t ** it = &this->head;
93 while( (*it) ) {
94 it = &(*it)->next;
95 }
96
97 return it == this->tail;
98})
99
100static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
101 verify( !n->next );
102 if( p == this->tail ) {
103 this->tail = &n->next;
104 }
105 else {
106 n->next = *p;
107 }
108 *p = n;
109
110 verify( validate( this ) );
111}
112
113void insert( alarm_list_t * this, alarm_node_t * n ) {
114 alarm_node_t ** it = &this->head;
115 while( (*it) && (n->alarm > (*it)->alarm) ) {
116 it = &(*it)->next;
117 }
118
119 insert_at( this, n, it );
120
121 verify( validate( this ) );
122}
123
124alarm_node_t * pop( alarm_list_t * this ) {
125 alarm_node_t * head = this->head;
126 if( head ) {
127 this->head = head->next;
128 if( !head->next ) {
129 this->tail = &this->head;
130 }
131 head->next = NULL;
132 }
133 verify( validate( this ) );
134 return head;
135}
136
137static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
138 verify( it );
139 verify( (*it) == n );
140
141 (*it) = n->next;
142 if( !n-> next ) {
143 this->tail = it;
144 }
145 n->next = NULL;
146
147 verify( validate( this ) );
148}
149
150static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
151 alarm_node_t ** it = &this->head;
152 while( (*it) && (*it) != n ) {
153 it = &(*it)->next;
154 }
155
156 verify( validate( this ) );
157
158 if( *it ) { remove_at( this, n, it ); }
159
160 verify( validate( this ) );
161}
162
163void register_self( alarm_node_t * this ) {
164 disable_interrupts();
165 verify( !systemProcessor->pending_alarm );
166 lock( &systemProcessor->alarm_lock );
167 {
168 verify( validate( &systemProcessor->alarms ) );
169 bool first = !systemProcessor->alarms.head;
170
171 insert( &systemProcessor->alarms, this );
172 if( systemProcessor->pending_alarm ) {
173 tick_preemption();
174 }
175 if( first ) {
176 __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );
177 }
178 }
179 unlock( &systemProcessor->alarm_lock );
180 this->set = true;
181 enable_interrupts( __PRETTY_FUNCTION__ );
182}
183
184void unregister_self( alarm_node_t * this ) {
185 LIB_DEBUG_DO(
186 char text[256];
187 __attribute__((unused)) int len = snprintf( text, 256, "Kernel : unregister %p start\n", this );
188 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
189 );
190 disable_interrupts();
191 lock( &systemProcessor->alarm_lock );
192 {
193 verify( validate( &systemProcessor->alarms ) );
194 remove( &systemProcessor->alarms, this );
195 }
196 unlock( &systemProcessor->alarm_lock );
197 disable_interrupts();
198 this->set = false;
199 LIB_DEBUG_DO(
200 len = snprintf( text, 256, "Kernel : unregister %p end\n", this );
201 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
202 );
203}
Note: See TracBrowser for help on using the repository browser.