| 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 : Sun Jan  5 08:41:36 2020
 | 
|---|
| 13 | // Update Count     : 69
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #define __cforall_thread__
 | 
|---|
| 17 | 
 | 
|---|
| 18 | extern "C" {
 | 
|---|
| 19 | #include <errno.h>
 | 
|---|
| 20 | #include <stdio.h>
 | 
|---|
| 21 | #include <string.h>
 | 
|---|
| 22 | #include <unistd.h>
 | 
|---|
| 23 | #include <sys/time.h>
 | 
|---|
| 24 | }
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "alarm.hfa"
 | 
|---|
| 27 | #include "kernel_private.hfa"
 | 
|---|
| 28 | #include "preemption.hfa"
 | 
|---|
| 29 | 
 | 
|---|
| 30 | //=============================================================================================
 | 
|---|
| 31 | // Clock logic
 | 
|---|
| 32 | //=============================================================================================
 | 
|---|
| 33 | 
 | 
|---|
| 34 | Time __kernel_get_time() {
 | 
|---|
| 35 |         timespec curr;
 | 
|---|
| 36 |         clock_gettime( CLOCK_MONOTONIC_RAW, &curr );            // CLOCK_REALTIME
 | 
|---|
| 37 |         return (Time){ curr };
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | void __kernel_set_timer( Duration alarm ) {
 | 
|---|
| 41 |         verifyf(alarm >= 1`us || alarm == 0, "Setting timer to < 1us (%jins)", alarm`ns);
 | 
|---|
| 42 |         setitimer( ITIMER_REAL, &(itimerval){ alarm }, 0p );
 | 
|---|
| 43 | }
 | 
|---|
| 44 | 
 | 
|---|
| 45 | //=============================================================================================
 | 
|---|
| 46 | // Alarm logic
 | 
|---|
| 47 | //=============================================================================================
 | 
|---|
| 48 | 
 | 
|---|
| 49 | void ?{}( alarm_node_t & this, $thread * thrd, Time alarm, Duration period ) with( this ) {
 | 
|---|
| 50 |         this.thrd = thrd;
 | 
|---|
| 51 |         this.alarm = alarm;
 | 
|---|
| 52 |         this.period = period;
 | 
|---|
| 53 |         next = 0;
 | 
|---|
| 54 |         set = false;
 | 
|---|
| 55 |         kernel_alarm = false;
 | 
|---|
| 56 | }
 | 
|---|
| 57 | 
 | 
|---|
| 58 | void ?{}( alarm_node_t & this, processor   * proc, Time alarm, Duration period ) with( this ) {
 | 
|---|
| 59 |         this.proc = proc;
 | 
|---|
| 60 |         this.alarm = alarm;
 | 
|---|
| 61 |         this.period = period;
 | 
|---|
| 62 |         next = 0;
 | 
|---|
| 63 |         set = false;
 | 
|---|
| 64 |         kernel_alarm = true;
 | 
|---|
| 65 | }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | void ^?{}( alarm_node_t & this ) {
 | 
|---|
| 68 |         if( this.set ) {
 | 
|---|
| 69 |                 unregister_self( &this );
 | 
|---|
| 70 |         }
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | #if !defined(NDEBUG) && (defined(__CFA_DEBUG__) || defined(__CFA_VERIFY__))
 | 
|---|
| 74 | bool validate( alarm_list_t * this ) {
 | 
|---|
| 75 |         alarm_node_t ** it = &this->head;
 | 
|---|
| 76 |         while( (*it) ) {
 | 
|---|
| 77 |                 it = &(*it)->next;
 | 
|---|
| 78 |         }
 | 
|---|
| 79 | 
 | 
|---|
| 80 |         return it == this->tail;
 | 
|---|
| 81 | }
 | 
|---|
| 82 | #endif
 | 
|---|
| 83 | 
 | 
|---|
| 84 | static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
 | 
|---|
| 85 |         verify( !n->next );
 | 
|---|
| 86 |         if( p == this->tail ) {
 | 
|---|
| 87 |                 this->tail = &n->next;
 | 
|---|
| 88 |         }
 | 
|---|
| 89 |         else {
 | 
|---|
| 90 |                 n->next = *p;
 | 
|---|
| 91 |         }
 | 
|---|
| 92 |         *p = n;
 | 
|---|
| 93 | 
 | 
|---|
| 94 |         verify( validate( this ) );
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | void insert( alarm_list_t * this, alarm_node_t * n ) {
 | 
|---|
| 98 |         alarm_node_t ** it = &this->head;
 | 
|---|
| 99 |         while( (*it) && (n->alarm > (*it)->alarm) ) {
 | 
|---|
| 100 |                 it = &(*it)->next;
 | 
|---|
| 101 |         }
 | 
|---|
| 102 | 
 | 
|---|
| 103 |         insert_at( this, n, it );
 | 
|---|
| 104 | 
 | 
|---|
| 105 |         verify( validate( this ) );
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | alarm_node_t * pop( alarm_list_t * this ) {
 | 
|---|
| 109 |         alarm_node_t * head = this->head;
 | 
|---|
| 110 |         if( head ) {
 | 
|---|
| 111 |                 this->head = head->next;
 | 
|---|
| 112 |                 if( !head->next ) {
 | 
|---|
| 113 |                         this->tail = &this->head;
 | 
|---|
| 114 |                 }
 | 
|---|
| 115 |                 head->next = 0p;
 | 
|---|
| 116 |         }
 | 
|---|
| 117 |         verify( validate( this ) );
 | 
|---|
| 118 |         return head;
 | 
|---|
| 119 | }
 | 
|---|
| 120 | 
 | 
|---|
| 121 | static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
 | 
|---|
| 122 |         verify( it );
 | 
|---|
| 123 |         verify( (*it) == n );
 | 
|---|
| 124 | 
 | 
|---|
| 125 |         (*it) = n->next;
 | 
|---|
| 126 |         if( !n-> next ) {
 | 
|---|
| 127 |                 this->tail = it;
 | 
|---|
| 128 |         }
 | 
|---|
| 129 |         n->next = 0p;
 | 
|---|
| 130 | 
 | 
|---|
| 131 |         verify( validate( this ) );
 | 
|---|
| 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) != n ) {
 | 
|---|
| 137 |                 it = &(*it)->next;
 | 
|---|
| 138 |         }
 | 
|---|
| 139 | 
 | 
|---|
| 140 |         verify( validate( this ) );
 | 
|---|
| 141 | 
 | 
|---|
| 142 |         if( *it ) { remove_at( this, n, it ); }
 | 
|---|
| 143 | 
 | 
|---|
| 144 |         verify( validate( this ) );
 | 
|---|
| 145 | }
 | 
|---|
| 146 | 
 | 
|---|
| 147 | void register_self( alarm_node_t * this ) {
 | 
|---|
| 148 |         alarm_list_t * alarms = &event_kernel->alarms;
 | 
|---|
| 149 | 
 | 
|---|
| 150 |         disable_interrupts();
 | 
|---|
| 151 |         lock( event_kernel->lock __cfaabi_dbg_ctx2 );
 | 
|---|
| 152 |         {
 | 
|---|
| 153 |                 verify( validate( alarms ) );
 | 
|---|
| 154 |                 bool first = !alarms->head;
 | 
|---|
| 155 | 
 | 
|---|
| 156 |                 insert( alarms, this );
 | 
|---|
| 157 |                 if( first ) {
 | 
|---|
| 158 |                         __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
 | 
|---|
| 159 |                 }
 | 
|---|
| 160 |         }
 | 
|---|
| 161 |         unlock( event_kernel->lock );
 | 
|---|
| 162 |         this->set = true;
 | 
|---|
| 163 |         enable_interrupts( __cfaabi_dbg_ctx );
 | 
|---|
| 164 | }
 | 
|---|
| 165 | 
 | 
|---|
| 166 | void unregister_self( alarm_node_t * this ) {
 | 
|---|
| 167 |         disable_interrupts();
 | 
|---|
| 168 |         lock( event_kernel->lock __cfaabi_dbg_ctx2 );
 | 
|---|
| 169 |         {
 | 
|---|
| 170 |                 verify( validate( &event_kernel->alarms ) );
 | 
|---|
| 171 |                 remove( &event_kernel->alarms, this );
 | 
|---|
| 172 |         }
 | 
|---|
| 173 |         unlock( event_kernel->lock );
 | 
|---|
| 174 |         enable_interrupts( __cfaabi_dbg_ctx );
 | 
|---|
| 175 |         this->set = false;
 | 
|---|
| 176 | }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | //=============================================================================================
 | 
|---|
| 179 | // Utilities
 | 
|---|
| 180 | //=============================================================================================
 | 
|---|
| 181 | 
 | 
|---|
| 182 | void sleep( Duration duration ) {
 | 
|---|
| 183 |         alarm_node_t node = { active_thread(), __kernel_get_time() + duration, 0`s };
 | 
|---|
| 184 | 
 | 
|---|
| 185 |         register_self( &node );
 | 
|---|
| 186 |         park( __cfaabi_dbg_ctx );
 | 
|---|
| 187 | 
 | 
|---|
| 188 |         /* paranoid */ verify( !node.set );
 | 
|---|
| 189 |         /* paranoid */ verify( node.next == 0p );
 | 
|---|
| 190 | }
 | 
|---|
| 191 | 
 | 
|---|
| 192 | // Local Variables: //
 | 
|---|
| 193 | // mode: c //
 | 
|---|
| 194 | // tab-width: 4 //
 | 
|---|
| 195 | // End: //
 | 
|---|