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