| 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 | // #define __CFA_DEBUG_PRINT_PREEMPTION__
|
|---|
| 18 |
|
|---|
| 19 | #include <errno.h>
|
|---|
| 20 | #include <stdio.h>
|
|---|
| 21 | #include <unistd.h>
|
|---|
| 22 | #include <string.h>
|
|---|
| 23 | #include <sys/time.h>
|
|---|
| 24 |
|
|---|
| 25 | #include "alarm.hfa"
|
|---|
| 26 | #include "kernel/fwd.hfa"
|
|---|
| 27 | #include "preemption.hfa"
|
|---|
| 28 |
|
|---|
| 29 | //=============================================================================================
|
|---|
| 30 | // Clock logic
|
|---|
| 31 | //=============================================================================================
|
|---|
| 32 |
|
|---|
| 33 | Time __kernel_get_time() {
|
|---|
| 34 | timespec curr;
|
|---|
| 35 | clock_gettime( CLOCK_MONOTONIC_RAW, &curr ); // CLOCK_REALTIME
|
|---|
| 36 | return (Time){ curr };
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | void __kernel_set_timer( Duration alarm ) {
|
|---|
| 40 | alarm = max(alarm, 1`us);
|
|---|
| 41 | itimerval otv @= { 0 };
|
|---|
| 42 | getitimer( ITIMER_REAL, &otv );
|
|---|
| 43 | Duration od = { otv.it_value };
|
|---|
| 44 | if(od == 0 || od > alarm) {
|
|---|
| 45 | setitimer( ITIMER_REAL, &(itimerval){ alarm }, 0p );
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | //=============================================================================================
|
|---|
| 50 | // Alarm logic
|
|---|
| 51 | //=============================================================================================
|
|---|
| 52 |
|
|---|
| 53 | void ?{}( alarm_node_t & this, thread$ * thrd, Duration alarm, Duration period) with( this ) {
|
|---|
| 54 | this.initial = alarm;
|
|---|
| 55 | this.period = period;
|
|---|
| 56 | this.thrd = thrd;
|
|---|
| 57 | this.timeval = __kernel_get_time() + alarm;
|
|---|
| 58 | set = false;
|
|---|
| 59 | type = User;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | void ?{}( alarm_node_t & this, processor * proc, Duration alarm, Duration period ) with( this ) {
|
|---|
| 63 | this.initial = alarm;
|
|---|
| 64 | this.period = period;
|
|---|
| 65 | this.proc = proc;
|
|---|
| 66 | this.timeval = __kernel_get_time() + alarm;
|
|---|
| 67 | set = false;
|
|---|
| 68 | type = Kernel;
|
|---|
| 69 | }
|
|---|
| 70 | void ?{}( alarm_node_t & this, Alarm_Callback callback, Duration alarm, Duration period ) with( this ) {
|
|---|
| 71 | this.callback = callback;
|
|---|
| 72 | this.initial = alarm;
|
|---|
| 73 | this.period = period;
|
|---|
| 74 | this.timeval = __kernel_get_time() + alarm;
|
|---|
| 75 | set = false;
|
|---|
| 76 | type = Callback;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | void ^?{}( alarm_node_t & this ) {
|
|---|
| 80 | if( this.set ) {
|
|---|
| 81 | unregister_self( &this );
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void insert( alarm_list_t * this, alarm_node_t * n ) {
|
|---|
| 86 | alarm_node_t * it = & (*this)`first;
|
|---|
| 87 | while( it && (n->timeval > it->timeval) ) {
|
|---|
| 88 | it = & (*it)`next;
|
|---|
| 89 | }
|
|---|
| 90 | if ( it ) {
|
|---|
| 91 | insert_before( *it, *n );
|
|---|
| 92 | } else {
|
|---|
| 93 | insert_last(*this, *n);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | verify( validate( *this ) );
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | alarm_node_t * pop( alarm_list_t * this ) {
|
|---|
| 100 | verify( validate( *this ) );
|
|---|
| 101 | alarm_node_t * head = & (*this)`first;
|
|---|
| 102 | if( head ) {
|
|---|
| 103 | remove(*head);
|
|---|
| 104 | }
|
|---|
| 105 | verify( validate( *this ) );
|
|---|
| 106 | return head;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | void register_self( alarm_node_t * this ) {
|
|---|
| 110 | alarm_list_t & alarms = event_kernel->alarms;
|
|---|
| 111 |
|
|---|
| 112 | disable_interrupts();
|
|---|
| 113 | lock( event_kernel->lock __cfaabi_dbg_ctx2 );
|
|---|
| 114 | {
|
|---|
| 115 | /* paranoid */ verify( validate( alarms ) );
|
|---|
| 116 |
|
|---|
| 117 | Time curr = __kernel_get_time();
|
|---|
| 118 | __cfadbg_print_safe( preemption, " KERNEL: alarm inserting %p (%lu -> %lu).\n", this, curr.tn, this->timeval.tn );
|
|---|
| 119 | insert( &alarms, this );
|
|---|
| 120 | __kernel_set_timer( this->timeval - curr);
|
|---|
| 121 | this->set = true;
|
|---|
| 122 | }
|
|---|
| 123 | unlock( event_kernel->lock );
|
|---|
| 124 | enable_interrupts();
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | void unregister_self( alarm_node_t * this ) {
|
|---|
| 128 | disable_interrupts();
|
|---|
| 129 | lock( event_kernel->lock __cfaabi_dbg_ctx2 );
|
|---|
| 130 | {
|
|---|
| 131 | verify( validate( event_kernel->alarms ) );
|
|---|
| 132 | if (this->set) remove( *this );
|
|---|
| 133 | this->set = false;
|
|---|
| 134 | }
|
|---|
| 135 | unlock( event_kernel->lock );
|
|---|
| 136 | enable_interrupts();
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | //=============================================================================================
|
|---|
| 140 | // Utilities
|
|---|
| 141 | //=============================================================================================
|
|---|
| 142 |
|
|---|
| 143 | void sleep( Duration duration ) libcfa_public {
|
|---|
| 144 | alarm_node_t node = { active_thread(), duration, 0`s };
|
|---|
| 145 |
|
|---|
| 146 | register_self( &node );
|
|---|
| 147 | park();
|
|---|
| 148 |
|
|---|
| 149 | /* paranoid */ verify( !node.set );
|
|---|
| 150 | /* paranoid */ verify( & node`next == 0p );
|
|---|
| 151 | /* paranoid */ verify( & node`prev == 0p );
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | // Local Variables: //
|
|---|
| 155 | // mode: c //
|
|---|
| 156 | // tab-width: 4 //
|
|---|
| 157 | // End: //
|
|---|