source: src/libcfa/concurrency/alarm.c@ 1a18423

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 1a18423 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
RevLine 
[6cff9f3]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
[c81ebf9]17extern "C" {
[82ff5845]18#include <errno.h>
19#include <stdio.h>
20#include <string.h>
[c81ebf9]21#include <time.h>
[82ff5845]22#include <unistd.h>
[c81ebf9]23#include <sys/time.h>
24}
25
[6cff9f3]26#include "alarm.h"
27#include "kernel_private.h"
[82ff5845]28#include "libhdr.h"
[c81ebf9]29#include "preemption.h"
[6cff9f3]30
[c81ebf9]31//=============================================================================================
32// Clock logic
33//=============================================================================================
34
35__cfa_time_t __kernel_get_time() {
36 timespec curr;
37 clock_gettime( CLOCK_REALTIME, &curr );
[82ff5845]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;
[c81ebf9]45}
46
47void __kernel_set_timer( __cfa_time_t alarm ) {
[82ff5845]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
[c81ebf9]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 );
[6cff9f3]61}
62
[c81ebf9]63//=============================================================================================
64// Alarm logic
65//=============================================================================================
66
67void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
[6cff9f3]68 this->thrd = thrd;
69 this->alarm = alarm;
70 this->period = period;
71 this->next = 0;
[c81ebf9]72 this->set = false;
73 this->kernel_alarm = false;
[6cff9f3]74}
75
[c81ebf9]76void ?{}( alarm_node_t * this, processor * proc, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
[6cff9f3]77 this->proc = proc;
78 this->alarm = alarm;
79 this->period = period;
80 this->next = 0;
[c81ebf9]81 this->set = false;
82 this->kernel_alarm = true;
83}
[6cff9f3]84
[c81ebf9]85void ^?{}( alarm_node_t * this ) {
86 if( this->set ) {
87 unregister_self( this );
88 }
[6cff9f3]89}
90
[4e6fb8e]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
[6cff9f3]100static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
[0b33412]101 verify( !n->next );
[6cff9f3]102 if( p == this->tail ) {
103 this->tail = &n->next;
104 }
105 else {
106 n->next = *p;
107 }
108 *p = n;
[4e6fb8e]109
[0b33412]110 verify( validate( this ) );
[6cff9f3]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 );
[4e6fb8e]120
[0b33412]121 verify( validate( this ) );
[6cff9f3]122}
123
[c81ebf9]124alarm_node_t * pop( alarm_list_t * this ) {
[6cff9f3]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 }
[0b33412]133 verify( validate( this ) );
[6cff9f3]134 return head;
[c81ebf9]135}
136
137static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
[4aa2fb2]138 verify( it );
[ec35498]139 verify( (*it) == n );
[c81ebf9]140
[4e6fb8e]141 (*it) = n->next;
[c81ebf9]142 if( !n-> next ) {
143 this->tail = it;
144 }
145 n->next = NULL;
[4e6fb8e]146
[0b33412]147 verify( validate( this ) );
[c81ebf9]148}
149
150static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
151 alarm_node_t ** it = &this->head;
[4e6fb8e]152 while( (*it) && (*it) != n ) {
[c81ebf9]153 it = &(*it)->next;
154 }
155
[0b33412]156 verify( validate( this ) );
[4e6fb8e]157
[c81ebf9]158 if( *it ) { remove_at( this, n, it ); }
[4e6fb8e]159
[0b33412]160 verify( validate( this ) );
[c81ebf9]161}
162
163void register_self( alarm_node_t * this ) {
164 disable_interrupts();
[0b33412]165 verify( !systemProcessor->pending_alarm );
[c81ebf9]166 lock( &systemProcessor->alarm_lock );
167 {
[0b33412]168 verify( validate( &systemProcessor->alarms ) );
[82ff5845]169 bool first = !systemProcessor->alarms.head;
170
[c81ebf9]171 insert( &systemProcessor->alarms, this );
172 if( systemProcessor->pending_alarm ) {
173 tick_preemption();
174 }
[82ff5845]175 if( first ) {
176 __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );
177 }
[c81ebf9]178 }
179 unlock( &systemProcessor->alarm_lock );
180 this->set = true;
[4e6fb8e]181 enable_interrupts( __PRETTY_FUNCTION__ );
[c81ebf9]182}
183
184void unregister_self( alarm_node_t * this ) {
[4e6fb8e]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 );
[c81ebf9]190 disable_interrupts();
191 lock( &systemProcessor->alarm_lock );
[0b33412]192 {
193 verify( validate( &systemProcessor->alarms ) );
194 remove( &systemProcessor->alarms, this );
195 }
[c81ebf9]196 unlock( &systemProcessor->alarm_lock );
197 disable_interrupts();
198 this->set = false;
[4e6fb8e]199 LIB_DEBUG_DO(
200 len = snprintf( text, 256, "Kernel : unregister %p end\n", this );
201 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
202 );
[4aa2fb2]203}
Note: See TracBrowser for help on using the repository browser.