source: libcfa/src/concurrency/alarm.cfa @ b721578

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since b721578 was a3821fa, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Changed enable interrupts:

  • no longer save the caller for debugging
  • now polls based on parameter passed in
  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[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
[c1ee231]12// Last Modified On : Wed Jun 17 16:11:35 2020
13// Update Count     : 75
[6cff9f3]14//
15
[2026bb6]16#define __cforall_thread__
[c9ec301]17// #define __CFA_DEBUG_PRINT_PREEMPTION__
[2026bb6]18
[82ff5845]19#include <errno.h>
20#include <stdio.h>
21#include <unistd.h>
[c1ee231]22#include <string.h>
[c81ebf9]23#include <sys/time.h>
24
[73abe95]25#include "alarm.hfa"
[e660761]26#include "kernel/fwd.hfa"
[73abe95]27#include "preemption.hfa"
[6cff9f3]28
[c81ebf9]29//=============================================================================================
30// Clock logic
31//=============================================================================================
32
[2a84d06d]33Time __kernel_get_time() {
[c81ebf9]34        timespec curr;
[2a84d06d]35        clock_gettime( CLOCK_MONOTONIC_RAW, &curr );            // CLOCK_REALTIME
36        return (Time){ curr };
[c81ebf9]37}
38
[2a84d06d]39void __kernel_set_timer( Duration alarm ) {
[e0c235c]40        verifyf(alarm >= 1`us || alarm == 0, "Setting timer to < 1us (%jins)", alarm`ns);
[121be3e]41        setitimer( ITIMER_REAL, &(itimerval){ alarm }, 0p );
[6cff9f3]42}
43
[c81ebf9]44//=============================================================================================
45// Alarm logic
46//=============================================================================================
47
[eeb5023]48void ?{}( alarm_node_t & this, $thread * thrd, Time alarm, Duration period) with( this ) {
[242a902]49        this.thrd = thrd;
50        this.alarm = alarm;
51        this.period = period;
[c40e7c5]52        set = false;
[eeb5023]53        type = User;
[6cff9f3]54}
55
[c1ee231]56void ?{}( alarm_node_t & this, processor * proc, Time alarm, Duration period ) with( this ) {
[242a902]57        this.proc = proc;
58        this.alarm = alarm;
59        this.period = period;
[c40e7c5]60        set = false;
[eeb5023]61        type = Kernel;
62}
[4aeaee5]63void ?{}( alarm_node_t & this, Alarm_Callback callback, Time alarm, Duration period ) with( this ) {
[eeb5023]64        this.alarm = alarm;
65        this.period = period;
66        this.callback = callback;
67        set = false;
68        type = Callback;
[c81ebf9]69}
[6cff9f3]70
[242a902]71void ^?{}( alarm_node_t & this ) {
72        if( this.set ) {
73                unregister_self( &this );
[c81ebf9]74        }
[6cff9f3]75}
76
77void insert( alarm_list_t * this, alarm_node_t * n ) {
[d3ab183]78        alarm_node_t * it = & (*this)`first;
79        while( it && (n->alarm > it->alarm) ) {
80                it = & (*it)`next;
81        }
82        if ( it ) {
83                insert_before( *it, *n );
84        } else {
85                insert_last(*this, *n);
[6cff9f3]86        }
87
[d3ab183]88        verify( validate( *this ) );
[6cff9f3]89}
90
[c81ebf9]91alarm_node_t * pop( alarm_list_t * this ) {
[d3ab183]92        verify( validate( *this ) );
93        alarm_node_t * head = & (*this)`first;
[6cff9f3]94        if( head ) {
[d3ab183]95                remove(*head);
[6cff9f3]96        }
[d3ab183]97        verify( validate( *this ) );
[6cff9f3]98        return head;
[c81ebf9]99}
100
101void register_self( alarm_node_t * this ) {
[d3ab183]102        alarm_list_t & alarms = event_kernel->alarms;
[e60e0dc]103
[c81ebf9]104        disable_interrupts();
[36982fc]105        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[c81ebf9]106        {
[e60e0dc]107                verify( validate( alarms ) );
[d3ab183]108                bool first = ! & alarms`first;
[82ff5845]109
[c9ec301]110                __cfadbg_print_safe( preemption, " KERNEL: alarm inserting %p (%lu).\n", this, this->alarm.tn );
[d3ab183]111                insert( &alarms, this );
[82ff5845]112                if( first ) {
[d3ab183]113                        __kernel_set_timer( alarms`first.alarm - __kernel_get_time() );
[82ff5845]114                }
[c81ebf9]115        }
[ea7d2b0]116        unlock( event_kernel->lock );
[c81ebf9]117        this->set = true;
[a3821fa]118        enable_interrupts();
[c81ebf9]119}
120
121void unregister_self( alarm_node_t * this ) {
122        disable_interrupts();
[36982fc]123        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[0b33412]124        {
[d3ab183]125                verify( validate( event_kernel->alarms ) );
126                remove( *this );
[0b33412]127        }
[ea7d2b0]128        unlock( event_kernel->lock );
[a3821fa]129        enable_interrupts();
[c81ebf9]130        this->set = false;
[4aa2fb2]131}
[6b0b624]132
[2d8f7b0]133//=============================================================================================
134// Utilities
135//=============================================================================================
136
137void sleep( Duration duration ) {
138        alarm_node_t node = { active_thread(), __kernel_get_time() + duration, 0`s };
139
140        register_self( &node );
[e235429]141        park();
[2d8f7b0]142
143        /* paranoid */ verify( !node.set );
[d3ab183]144        /* paranoid */ verify( & node`next == 0p );
145        /* paranoid */ verify( & node`prev == 0p );
[2d8f7b0]146}
147
[6b0b624]148// Local Variables: //
149// mode: c //
150// tab-width: 4 //
151// End: //
Note: See TracBrowser for help on using the repository browser.