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