| 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 : Wed Apr 4 16:47:29 2018
|
|---|
| 13 | // Update Count : 49
|
|---|
| 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, Duration alarm ) with( this ) {
|
|---|
| 30 | // itimerval.it_value is a duration but uses time data-structure timeval.
|
|---|
| 31 | it_value{ alarm`s, alarm`us }; // seconds, microseconds
|
|---|
| 32 | it_interval{ 0 }; // 0 seconds, 0 microseconds
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | //=============================================================================================
|
|---|
| 36 | // Clock logic
|
|---|
| 37 | //=============================================================================================
|
|---|
| 38 |
|
|---|
| 39 | Time __kernel_get_time() {
|
|---|
| 40 | timespec curr;
|
|---|
| 41 | clock_gettime( CLOCK_MONOTONIC_RAW, &curr ); // CLOCK_REALTIME
|
|---|
| 42 | return (Time){ curr };
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | void __kernel_set_timer( Duration alarm ) {
|
|---|
| 46 | setitimer( ITIMER_REAL, &(itimerval){ alarm }, NULL );
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | //=============================================================================================
|
|---|
| 50 | // Alarm logic
|
|---|
| 51 | //=============================================================================================
|
|---|
| 52 |
|
|---|
| 53 | void ?{}( alarm_node_t & this, thread_desc * thrd, Time alarm, Duration period ) with( this ) {
|
|---|
| 54 | this.thrd = thrd;
|
|---|
| 55 | this.alarm = alarm;
|
|---|
| 56 | this.period = period;
|
|---|
| 57 | next = 0;
|
|---|
| 58 | set = false;
|
|---|
| 59 | kernel_alarm = false;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | void ?{}( alarm_node_t & this, processor * proc, Time alarm, Duration period ) with( this ) {
|
|---|
| 63 | this.proc = proc;
|
|---|
| 64 | this.alarm = alarm;
|
|---|
| 65 | this.period = period;
|
|---|
| 66 | next = 0;
|
|---|
| 67 | set = false;
|
|---|
| 68 | kernel_alarm = true;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | void ^?{}( alarm_node_t & this ) {
|
|---|
| 72 | if( this.set ) {
|
|---|
| 73 | unregister_self( &this );
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | __cfaabi_dbg_debug_do( bool validate( alarm_list_t * this ) {
|
|---|
| 78 | alarm_node_t ** it = &this->head;
|
|---|
| 79 | while( (*it) ) {
|
|---|
| 80 | it = &(*it)->next;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | return it == this->tail;
|
|---|
| 84 | })
|
|---|
| 85 |
|
|---|
| 86 | static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
|
|---|
| 87 | verify( !n->next );
|
|---|
| 88 | if( p == this->tail ) {
|
|---|
| 89 | this->tail = &n->next;
|
|---|
| 90 | }
|
|---|
| 91 | else {
|
|---|
| 92 | n->next = *p;
|
|---|
| 93 | }
|
|---|
| 94 | *p = n;
|
|---|
| 95 |
|
|---|
| 96 | verify( validate( this ) );
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | void insert( alarm_list_t * this, alarm_node_t * n ) {
|
|---|
| 100 | alarm_node_t ** it = &this->head;
|
|---|
| 101 | while( (*it) && (n->alarm > (*it)->alarm) ) {
|
|---|
| 102 | it = &(*it)->next;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | insert_at( this, n, it );
|
|---|
| 106 |
|
|---|
| 107 | verify( validate( this ) );
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | alarm_node_t * pop( alarm_list_t * this ) {
|
|---|
| 111 | alarm_node_t * head = this->head;
|
|---|
| 112 | if( head ) {
|
|---|
| 113 | this->head = head->next;
|
|---|
| 114 | if( !head->next ) {
|
|---|
| 115 | this->tail = &this->head;
|
|---|
| 116 | }
|
|---|
| 117 | head->next = NULL;
|
|---|
| 118 | }
|
|---|
| 119 | verify( validate( this ) );
|
|---|
| 120 | return head;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
|
|---|
| 124 | verify( it );
|
|---|
| 125 | verify( (*it) == n );
|
|---|
| 126 |
|
|---|
| 127 | (*it) = n->next;
|
|---|
| 128 | if( !n-> next ) {
|
|---|
| 129 | this->tail = it;
|
|---|
| 130 | }
|
|---|
| 131 | n->next = NULL;
|
|---|
| 132 |
|
|---|
| 133 | verify( validate( this ) );
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
|
|---|
| 137 | alarm_node_t ** it = &this->head;
|
|---|
| 138 | while( (*it) && (*it) != n ) {
|
|---|
| 139 | it = &(*it)->next;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | verify( validate( this ) );
|
|---|
| 143 |
|
|---|
| 144 | if( *it ) { remove_at( this, n, it ); }
|
|---|
| 145 |
|
|---|
| 146 | verify( validate( this ) );
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | void register_self( alarm_node_t * this ) {
|
|---|
| 150 | alarm_list_t * alarms = &event_kernel->alarms;
|
|---|
| 151 |
|
|---|
| 152 | disable_interrupts();
|
|---|
| 153 | lock( event_kernel->lock __cfaabi_dbg_ctx2 );
|
|---|
| 154 | {
|
|---|
| 155 | verify( validate( alarms ) );
|
|---|
| 156 | bool first = !alarms->head;
|
|---|
| 157 |
|
|---|
| 158 | insert( alarms, this );
|
|---|
| 159 | if( first ) {
|
|---|
| 160 | __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | unlock( event_kernel->lock );
|
|---|
| 164 | this->set = true;
|
|---|
| 165 | enable_interrupts( __cfaabi_dbg_ctx );
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | void unregister_self( alarm_node_t * this ) {
|
|---|
| 169 | disable_interrupts();
|
|---|
| 170 | lock( event_kernel->lock __cfaabi_dbg_ctx2 );
|
|---|
| 171 | {
|
|---|
| 172 | verify( validate( &event_kernel->alarms ) );
|
|---|
| 173 | remove( &event_kernel->alarms, this );
|
|---|
| 174 | }
|
|---|
| 175 | unlock( event_kernel->lock );
|
|---|
| 176 | enable_interrupts( __cfaabi_dbg_ctx );
|
|---|
| 177 | this->set = false;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | // Local Variables: //
|
|---|
| 181 | // mode: c //
|
|---|
| 182 | // tab-width: 4 //
|
|---|
| 183 | // End: //
|
|---|