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

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 with_gc
Last change on this file since b10affd was 2a84d06d, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

second draft of time package and incorporation into runtime kernel

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[6cff9f3]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
[6b0b624]11// Last Modified By : Peter A. Buhr
[2a84d06d]12// Last Modified On : Tue Mar 27 14:12:11 2018
13// Update Count : 41
[6cff9f3]14//
15
[c81ebf9]16extern "C" {
[82ff5845]17#include <errno.h>
18#include <stdio.h>
19#include <string.h>
20#include <unistd.h>
[c81ebf9]21#include <sys/time.h>
22}
23
[6cff9f3]24#include "alarm.h"
25#include "kernel_private.h"
[c81ebf9]26#include "preemption.h"
[6cff9f3]27
[9236060]28
[2a84d06d]29static inline void ?{}( itimerval & this, Duration alarm ) with( this ) {
30 it_value { alarm }; // seconds, microseconds
31 it_interval{ 0 };
[969b3fe]32}
33
[c81ebf9]34//=============================================================================================
35// Clock logic
36//=============================================================================================
37
[2a84d06d]38Time __kernel_get_time() {
[c81ebf9]39 timespec curr;
[2a84d06d]40 clock_gettime( CLOCK_MONOTONIC_RAW, &curr ); // CLOCK_REALTIME
41 return (Time){ curr };
[c81ebf9]42}
43
[2a84d06d]44void __kernel_set_timer( Duration alarm ) {
45 setitimer( ITIMER_REAL, &(itimerval){ alarm }, NULL );
[6cff9f3]46}
47
[c81ebf9]48//=============================================================================================
49// Alarm logic
50//=============================================================================================
51
[2a84d06d]52void ?{}( alarm_node_t & this, thread_desc * thrd, Time alarm, Duration period ) with( this ) {
[242a902]53 this.thrd = thrd;
54 this.alarm = alarm;
55 this.period = period;
[c40e7c5]56 next = 0;
57 set = false;
58 kernel_alarm = false;
[6cff9f3]59}
60
[2a84d06d]61void ?{}( alarm_node_t & this, processor * proc, Time alarm, Duration period ) with( this ) {
[242a902]62 this.proc = proc;
63 this.alarm = alarm;
64 this.period = period;
[c40e7c5]65 next = 0;
66 set = false;
67 kernel_alarm = true;
[c81ebf9]68}
[6cff9f3]69
[242a902]70void ^?{}( alarm_node_t & this ) {
71 if( this.set ) {
72 unregister_self( &this );
[c81ebf9]73 }
[6cff9f3]74}
75
[36982fc]76__cfaabi_dbg_debug_do( bool validate( alarm_list_t * this ) {
[4e6fb8e]77 alarm_node_t ** it = &this->head;
78 while( (*it) ) {
79 it = &(*it)->next;
80 }
81
82 return it == this->tail;
83})
84
[6cff9f3]85static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
[0b33412]86 verify( !n->next );
[6cff9f3]87 if( p == this->tail ) {
88 this->tail = &n->next;
89 }
90 else {
91 n->next = *p;
92 }
93 *p = n;
[4e6fb8e]94
[0b33412]95 verify( validate( this ) );
[6cff9f3]96}
97
98void insert( alarm_list_t * this, alarm_node_t * n ) {
99 alarm_node_t ** it = &this->head;
100 while( (*it) && (n->alarm > (*it)->alarm) ) {
101 it = &(*it)->next;
102 }
103
104 insert_at( this, n, it );
[4e6fb8e]105
[0b33412]106 verify( validate( this ) );
[6cff9f3]107}
108
[c81ebf9]109alarm_node_t * pop( alarm_list_t * this ) {
[6cff9f3]110 alarm_node_t * head = this->head;
111 if( head ) {
112 this->head = head->next;
113 if( !head->next ) {
114 this->tail = &this->head;
115 }
116 head->next = NULL;
117 }
[0b33412]118 verify( validate( this ) );
[6cff9f3]119 return head;
[c81ebf9]120}
121
122static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
[4aa2fb2]123 verify( it );
[ec35498]124 verify( (*it) == n );
[c81ebf9]125
[4e6fb8e]126 (*it) = n->next;
[c81ebf9]127 if( !n-> next ) {
128 this->tail = it;
129 }
130 n->next = NULL;
[4e6fb8e]131
[0b33412]132 verify( validate( this ) );
[c81ebf9]133}
134
135static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
136 alarm_node_t ** it = &this->head;
[4e6fb8e]137 while( (*it) && (*it) != n ) {
[c81ebf9]138 it = &(*it)->next;
139 }
140
[0b33412]141 verify( validate( this ) );
[4e6fb8e]142
[c81ebf9]143 if( *it ) { remove_at( this, n, it ); }
[4e6fb8e]144
[0b33412]145 verify( validate( this ) );
[c81ebf9]146}
147
148void register_self( alarm_node_t * this ) {
[e60e0dc]149 alarm_list_t * alarms = &event_kernel->alarms;
150
[c81ebf9]151 disable_interrupts();
[36982fc]152 lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[c81ebf9]153 {
[e60e0dc]154 verify( validate( alarms ) );
155 bool first = !alarms->head;
[82ff5845]156
[e60e0dc]157 insert( alarms, this );
[82ff5845]158 if( first ) {
[e60e0dc]159 __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
[82ff5845]160 }
[c81ebf9]161 }
[ea7d2b0]162 unlock( event_kernel->lock );
[c81ebf9]163 this->set = true;
[36982fc]164 enable_interrupts( __cfaabi_dbg_ctx );
[c81ebf9]165}
166
167void unregister_self( alarm_node_t * this ) {
168 disable_interrupts();
[36982fc]169 lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[0b33412]170 {
[e60e0dc]171 verify( validate( &event_kernel->alarms ) );
172 remove( &event_kernel->alarms, this );
[0b33412]173 }
[ea7d2b0]174 unlock( event_kernel->lock );
[36982fc]175 enable_interrupts( __cfaabi_dbg_ctx );
[c81ebf9]176 this->set = false;
[4aa2fb2]177}
[6b0b624]178
179// Local Variables: //
180// mode: c //
181// tab-width: 4 //
182// End: //
Note: See TracBrowser for help on using the repository browser.