[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] | 17 | extern "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 |
|
---|
| 47 | void __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 |
|
---|
| 67 | void ?{}( 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] | 76 | void ?{}( 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] | 85 | void ^?{}( alarm_node_t * this ) {
|
---|
| 86 | if( this->set ) {
|
---|
| 87 | unregister_self( this );
|
---|
| 88 | }
|
---|
[6cff9f3] | 89 | }
|
---|
| 90 |
|
---|
| 91 | static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
|
---|
| 92 | assert( !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 |
|
---|
| 102 | void insert( alarm_list_t * this, alarm_node_t * n ) {
|
---|
| 103 | alarm_node_t ** it = &this->head;
|
---|
| 104 | while( (*it) && (n->alarm > (*it)->alarm) ) {
|
---|
| 105 | it = &(*it)->next;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | insert_at( this, n, it );
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[c81ebf9] | 111 | alarm_node_t * pop( alarm_list_t * this ) {
|
---|
[6cff9f3] | 112 | alarm_node_t * head = this->head;
|
---|
| 113 | if( head ) {
|
---|
| 114 | this->head = head->next;
|
---|
| 115 | if( !head->next ) {
|
---|
| 116 | this->tail = &this->head;
|
---|
| 117 | }
|
---|
| 118 | head->next = NULL;
|
---|
| 119 | }
|
---|
| 120 | return head;
|
---|
[c81ebf9] | 121 | }
|
---|
| 122 |
|
---|
| 123 | static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
|
---|
| 124 | assert( it );
|
---|
| 125 | assert( (*it)->next == n );
|
---|
| 126 |
|
---|
| 127 | (*it)->next = n->next;
|
---|
| 128 | if( !n-> next ) {
|
---|
| 129 | this->tail = it;
|
---|
| 130 | }
|
---|
| 131 | n->next = NULL;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
|
---|
| 135 | alarm_node_t ** it = &this->head;
|
---|
| 136 | while( (*it) && (*it)->next != n ) {
|
---|
| 137 | it = &(*it)->next;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | if( *it ) { remove_at( this, n, it ); }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | void register_self( alarm_node_t * this ) {
|
---|
| 144 | disable_interrupts();
|
---|
| 145 | assert( !systemProcessor->pending_alarm );
|
---|
| 146 | lock( &systemProcessor->alarm_lock );
|
---|
| 147 | {
|
---|
[82ff5845] | 148 | bool first = !systemProcessor->alarms.head;
|
---|
| 149 |
|
---|
[c81ebf9] | 150 | insert( &systemProcessor->alarms, this );
|
---|
| 151 | if( systemProcessor->pending_alarm ) {
|
---|
| 152 | tick_preemption();
|
---|
| 153 | }
|
---|
[82ff5845] | 154 | if( first ) {
|
---|
| 155 | __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );
|
---|
| 156 | }
|
---|
[c81ebf9] | 157 | }
|
---|
| 158 | unlock( &systemProcessor->alarm_lock );
|
---|
| 159 | this->set = true;
|
---|
| 160 | enable_interrupts();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | void unregister_self( alarm_node_t * this ) {
|
---|
| 164 | disable_interrupts();
|
---|
| 165 | lock( &systemProcessor->alarm_lock );
|
---|
| 166 | remove( &systemProcessor->alarms, this );
|
---|
| 167 | unlock( &systemProcessor->alarm_lock );
|
---|
| 168 | disable_interrupts();
|
---|
| 169 | this->set = false;
|
---|
[6cff9f3] | 170 | }
|
---|