[6cff9f3] | 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 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
[e0c235c] | 12 | // Last Modified On : Sun Jan 5 08:41:36 2020 |
---|
| 13 | // Update Count : 69 |
---|
[6cff9f3] | 14 | // |
---|
| 15 | |
---|
[2026bb6] | 16 | #define __cforall_thread__ |
---|
| 17 | |
---|
[c81ebf9] | 18 | extern "C" { |
---|
[82ff5845] | 19 | #include <errno.h> |
---|
| 20 | #include <stdio.h> |
---|
| 21 | #include <string.h> |
---|
| 22 | #include <unistd.h> |
---|
[c81ebf9] | 23 | #include <sys/time.h> |
---|
| 24 | } |
---|
| 25 | |
---|
[73abe95] | 26 | #include "alarm.hfa" |
---|
| 27 | #include "kernel_private.hfa" |
---|
| 28 | #include "preemption.hfa" |
---|
[6cff9f3] | 29 | |
---|
[c81ebf9] | 30 | //============================================================================================= |
---|
| 31 | // Clock logic |
---|
| 32 | //============================================================================================= |
---|
| 33 | |
---|
[2a84d06d] | 34 | Time __kernel_get_time() { |
---|
[c81ebf9] | 35 | timespec curr; |
---|
[2a84d06d] | 36 | clock_gettime( CLOCK_MONOTONIC_RAW, &curr ); // CLOCK_REALTIME |
---|
| 37 | return (Time){ curr }; |
---|
[c81ebf9] | 38 | } |
---|
| 39 | |
---|
[2a84d06d] | 40 | void __kernel_set_timer( Duration alarm ) { |
---|
[e0c235c] | 41 | verifyf(alarm >= 1`us || alarm == 0, "Setting timer to < 1us (%jins)", alarm`ns); |
---|
[121be3e] | 42 | setitimer( ITIMER_REAL, &(itimerval){ alarm }, 0p ); |
---|
[6cff9f3] | 43 | } |
---|
| 44 | |
---|
[c81ebf9] | 45 | //============================================================================================= |
---|
| 46 | // Alarm logic |
---|
| 47 | //============================================================================================= |
---|
| 48 | |
---|
[ac2b598] | 49 | void ?{}( alarm_node_t & this, $thread * thrd, Time alarm, Duration period ) with( this ) { |
---|
[242a902] | 50 | this.thrd = thrd; |
---|
| 51 | this.alarm = alarm; |
---|
| 52 | this.period = period; |
---|
[c40e7c5] | 53 | set = false; |
---|
| 54 | kernel_alarm = false; |
---|
[6cff9f3] | 55 | } |
---|
| 56 | |
---|
[2a84d06d] | 57 | void ?{}( alarm_node_t & this, processor * proc, Time alarm, Duration period ) with( this ) { |
---|
[242a902] | 58 | this.proc = proc; |
---|
| 59 | this.alarm = alarm; |
---|
| 60 | this.period = period; |
---|
[c40e7c5] | 61 | set = false; |
---|
| 62 | kernel_alarm = true; |
---|
[c81ebf9] | 63 | } |
---|
[6cff9f3] | 64 | |
---|
[242a902] | 65 | void ^?{}( alarm_node_t & this ) { |
---|
| 66 | if( this.set ) { |
---|
| 67 | unregister_self( &this ); |
---|
[c81ebf9] | 68 | } |
---|
[6cff9f3] | 69 | } |
---|
| 70 | |
---|
| 71 | void insert( alarm_list_t * this, alarm_node_t * n ) { |
---|
[d3ab183] | 72 | alarm_node_t * it = & (*this)`first; |
---|
| 73 | while( it && (n->alarm > it->alarm) ) { |
---|
| 74 | it = & (*it)`next; |
---|
| 75 | } |
---|
| 76 | if ( it ) { |
---|
| 77 | insert_before( *it, *n ); |
---|
| 78 | } else { |
---|
| 79 | insert_last(*this, *n); |
---|
[6cff9f3] | 80 | } |
---|
| 81 | |
---|
[d3ab183] | 82 | verify( validate( *this ) ); |
---|
[6cff9f3] | 83 | } |
---|
| 84 | |
---|
[c81ebf9] | 85 | alarm_node_t * pop( alarm_list_t * this ) { |
---|
[d3ab183] | 86 | verify( validate( *this ) ); |
---|
| 87 | alarm_node_t * head = & (*this)`first; |
---|
[6cff9f3] | 88 | if( head ) { |
---|
[d3ab183] | 89 | remove(*head); |
---|
[6cff9f3] | 90 | } |
---|
[d3ab183] | 91 | verify( validate( *this ) ); |
---|
[6cff9f3] | 92 | return head; |
---|
[c81ebf9] | 93 | } |
---|
| 94 | |
---|
| 95 | void register_self( alarm_node_t * this ) { |
---|
[d3ab183] | 96 | alarm_list_t & alarms = event_kernel->alarms; |
---|
[e60e0dc] | 97 | |
---|
[c81ebf9] | 98 | disable_interrupts(); |
---|
[36982fc] | 99 | lock( event_kernel->lock __cfaabi_dbg_ctx2 ); |
---|
[c81ebf9] | 100 | { |
---|
[e60e0dc] | 101 | verify( validate( alarms ) ); |
---|
[d3ab183] | 102 | bool first = ! & alarms`first; |
---|
[82ff5845] | 103 | |
---|
[d3ab183] | 104 | insert( &alarms, this ); |
---|
[82ff5845] | 105 | if( first ) { |
---|
[d3ab183] | 106 | __kernel_set_timer( alarms`first.alarm - __kernel_get_time() ); |
---|
[82ff5845] | 107 | } |
---|
[c81ebf9] | 108 | } |
---|
[ea7d2b0] | 109 | unlock( event_kernel->lock ); |
---|
[c81ebf9] | 110 | this->set = true; |
---|
[36982fc] | 111 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[c81ebf9] | 112 | } |
---|
| 113 | |
---|
| 114 | void unregister_self( alarm_node_t * this ) { |
---|
| 115 | disable_interrupts(); |
---|
[36982fc] | 116 | lock( event_kernel->lock __cfaabi_dbg_ctx2 ); |
---|
[0b33412] | 117 | { |
---|
[d3ab183] | 118 | verify( validate( event_kernel->alarms ) ); |
---|
| 119 | remove( *this ); |
---|
[0b33412] | 120 | } |
---|
[ea7d2b0] | 121 | unlock( event_kernel->lock ); |
---|
[36982fc] | 122 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[c81ebf9] | 123 | this->set = false; |
---|
[4aa2fb2] | 124 | } |
---|
[6b0b624] | 125 | |
---|
[2d8f7b0] | 126 | //============================================================================================= |
---|
| 127 | // Utilities |
---|
| 128 | //============================================================================================= |
---|
| 129 | |
---|
| 130 | void sleep( Duration duration ) { |
---|
| 131 | alarm_node_t node = { active_thread(), __kernel_get_time() + duration, 0`s }; |
---|
| 132 | |
---|
| 133 | register_self( &node ); |
---|
| 134 | park( __cfaabi_dbg_ctx ); |
---|
| 135 | |
---|
| 136 | /* paranoid */ verify( !node.set ); |
---|
[d3ab183] | 137 | /* paranoid */ verify( & node`next == 0p ); |
---|
| 138 | /* paranoid */ verify( & node`prev == 0p ); |
---|
[2d8f7b0] | 139 | } |
---|
| 140 | |
---|
[6b0b624] | 141 | // Local Variables: // |
---|
| 142 | // mode: c // |
---|
| 143 | // tab-width: 4 // |
---|
| 144 | // End: // |
---|