| 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 Jun 17 16:11:35 2020 | 
|---|
| 13 | // Update Count     : 75 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #define __cforall_thread__ | 
|---|
| 17 |  | 
|---|
| 18 | #include <errno.h> | 
|---|
| 19 | #include <stdio.h> | 
|---|
| 20 | #include <unistd.h> | 
|---|
| 21 | #include <string.h> | 
|---|
| 22 | #include <sys/time.h> | 
|---|
| 23 |  | 
|---|
| 24 | #include "alarm.hfa" | 
|---|
| 25 | #include "kernel/fwd.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`ns); | 
|---|
| 40 | setitimer( ITIMER_REAL, &(itimerval){ alarm }, 0p ); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | //============================================================================================= | 
|---|
| 44 | // Alarm logic | 
|---|
| 45 | //============================================================================================= | 
|---|
| 46 |  | 
|---|
| 47 | void ?{}( alarm_node_t & this, $thread * thrd, Time alarm, Duration period) with( this ) { | 
|---|
| 48 | this.thrd = thrd; | 
|---|
| 49 | this.alarm = alarm; | 
|---|
| 50 | this.period = period; | 
|---|
| 51 | set = false; | 
|---|
| 52 | type = User; | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | void ?{}( alarm_node_t & this, processor * proc, Time alarm, Duration period ) with( this ) { | 
|---|
| 56 | this.proc = proc; | 
|---|
| 57 | this.alarm = alarm; | 
|---|
| 58 | this.period = period; | 
|---|
| 59 | set = false; | 
|---|
| 60 | type = Kernel; | 
|---|
| 61 | } | 
|---|
| 62 | void ?{}( alarm_node_t & this, Alarm_Callback callback, Time alarm, Duration period ) with( this ) { | 
|---|
| 63 | this.alarm = alarm; | 
|---|
| 64 | this.period = period; | 
|---|
| 65 | this.callback = callback; | 
|---|
| 66 | set = false; | 
|---|
| 67 | type = Callback; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | void ^?{}( alarm_node_t & this ) { | 
|---|
| 71 | if( this.set ) { | 
|---|
| 72 | unregister_self( &this ); | 
|---|
| 73 | } | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | void insert( alarm_list_t * this, alarm_node_t * n ) { | 
|---|
| 77 | alarm_node_t * it = & (*this)`first; | 
|---|
| 78 | while( it && (n->alarm > it->alarm) ) { | 
|---|
| 79 | it = & (*it)`next; | 
|---|
| 80 | } | 
|---|
| 81 | if ( it ) { | 
|---|
| 82 | insert_before( *it, *n ); | 
|---|
| 83 | } else { | 
|---|
| 84 | insert_last(*this, *n); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | verify( validate( *this ) ); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | alarm_node_t * pop( alarm_list_t * this ) { | 
|---|
| 91 | verify( validate( *this ) ); | 
|---|
| 92 | alarm_node_t * head = & (*this)`first; | 
|---|
| 93 | if( head ) { | 
|---|
| 94 | remove(*head); | 
|---|
| 95 | } | 
|---|
| 96 | verify( validate( *this ) ); | 
|---|
| 97 | return head; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | void register_self( alarm_node_t * this ) { | 
|---|
| 101 | alarm_list_t & alarms = event_kernel->alarms; | 
|---|
| 102 |  | 
|---|
| 103 | disable_interrupts(); | 
|---|
| 104 | lock( event_kernel->lock __cfaabi_dbg_ctx2 ); | 
|---|
| 105 | { | 
|---|
| 106 | verify( validate( alarms ) ); | 
|---|
| 107 | bool first = ! & alarms`first; | 
|---|
| 108 |  | 
|---|
| 109 | insert( &alarms, this ); | 
|---|
| 110 | if( first ) { | 
|---|
| 111 | __kernel_set_timer( alarms`first.alarm - __kernel_get_time() ); | 
|---|
| 112 | } | 
|---|
| 113 | } | 
|---|
| 114 | unlock( event_kernel->lock ); | 
|---|
| 115 | this->set = true; | 
|---|
| 116 | enable_interrupts( __cfaabi_dbg_ctx ); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | void unregister_self( alarm_node_t * this ) { | 
|---|
| 120 | disable_interrupts(); | 
|---|
| 121 | lock( event_kernel->lock __cfaabi_dbg_ctx2 ); | 
|---|
| 122 | { | 
|---|
| 123 | verify( validate( event_kernel->alarms ) ); | 
|---|
| 124 | remove( *this ); | 
|---|
| 125 | } | 
|---|
| 126 | unlock( event_kernel->lock ); | 
|---|
| 127 | enable_interrupts( __cfaabi_dbg_ctx ); | 
|---|
| 128 | this->set = false; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | //============================================================================================= | 
|---|
| 132 | // Utilities | 
|---|
| 133 | //============================================================================================= | 
|---|
| 134 |  | 
|---|
| 135 | void sleep( Duration duration ) { | 
|---|
| 136 | alarm_node_t node = { active_thread(), __kernel_get_time() + duration, 0`s }; | 
|---|
| 137 |  | 
|---|
| 138 | register_self( &node ); | 
|---|
| 139 | park(); | 
|---|
| 140 |  | 
|---|
| 141 | /* paranoid */ verify( !node.set ); | 
|---|
| 142 | /* paranoid */ verify( & node`next == 0p ); | 
|---|
| 143 | /* paranoid */ verify( & node`prev == 0p ); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | // Local Variables: // | 
|---|
| 147 | // mode: c // | 
|---|
| 148 | // tab-width: 4 // | 
|---|
| 149 | // End: // | 
|---|