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

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since c20533ea was 4aeaee5, checked in by Colby Alexander Parsons <caparsons@…>, 4 years ago

added alarm callback to union and refactored

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