source: libcfa/src/concurrency/alarm.cfa @ 7ac3151

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 7ac3151 was e0c235c, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

move clock.hfa into time.hfa, add getCPUTime, rename duration and time field to "tn", update mktime code

  • Property mode set to 100644
File size: 4.0 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
[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]18extern "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]34Time __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]40void __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
[2a84d06d]49void ?{}( alarm_node_t & this, thread_desc * thrd, Time alarm, Duration period ) with( this ) {
[242a902]50        this.thrd = thrd;
51        this.alarm = alarm;
52        this.period = period;
[c40e7c5]53        next = 0;
54        set = false;
55        kernel_alarm = false;
[6cff9f3]56}
57
[2a84d06d]58void ?{}( alarm_node_t & this, processor   * proc, Time alarm, Duration period ) with( this ) {
[242a902]59        this.proc = proc;
60        this.alarm = alarm;
61        this.period = period;
[c40e7c5]62        next = 0;
63        set = false;
64        kernel_alarm = true;
[c81ebf9]65}
[6cff9f3]66
[242a902]67void ^?{}( alarm_node_t & this ) {
68        if( this.set ) {
69                unregister_self( &this );
[c81ebf9]70        }
[6cff9f3]71}
72
[b1a4300]73#if !defined(NDEBUG) && (defined(__CFA_DEBUG__) || defined(__CFA_VERIFY__))
74bool validate( alarm_list_t * this ) {
[4e6fb8e]75        alarm_node_t ** it = &this->head;
76        while( (*it) ) {
77                it = &(*it)->next;
78        }
79
80        return it == this->tail;
[b1a4300]81}
82#endif
[4e6fb8e]83
[6cff9f3]84static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
[0b33412]85        verify( !n->next );
[6cff9f3]86        if( p == this->tail ) {
87                this->tail = &n->next;
88        }
89        else {
90                n->next = *p;
91        }
92        *p = n;
[4e6fb8e]93
[0b33412]94        verify( validate( this ) );
[6cff9f3]95}
96
97void insert( alarm_list_t * this, alarm_node_t * n ) {
98        alarm_node_t ** it = &this->head;
99        while( (*it) && (n->alarm > (*it)->alarm) ) {
100                it = &(*it)->next;
101        }
102
103        insert_at( this, n, it );
[4e6fb8e]104
[0b33412]105        verify( validate( this ) );
[6cff9f3]106}
107
[c81ebf9]108alarm_node_t * pop( alarm_list_t * this ) {
[6cff9f3]109        alarm_node_t * head = this->head;
110        if( head ) {
111                this->head = head->next;
112                if( !head->next ) {
113                        this->tail = &this->head;
114                }
[121be3e]115                head->next = 0p;
[6cff9f3]116        }
[0b33412]117        verify( validate( this ) );
[6cff9f3]118        return head;
[c81ebf9]119}
120
121static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
[4aa2fb2]122        verify( it );
[ec35498]123        verify( (*it) == n );
[c81ebf9]124
[4e6fb8e]125        (*it) = n->next;
[c81ebf9]126        if( !n-> next ) {
127                this->tail = it;
128        }
[121be3e]129        n->next = 0p;
[4e6fb8e]130
[0b33412]131        verify( validate( this ) );
[c81ebf9]132}
133
134static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
135        alarm_node_t ** it = &this->head;
[4e6fb8e]136        while( (*it) && (*it) != n ) {
[c81ebf9]137                it = &(*it)->next;
138        }
139
[0b33412]140        verify( validate( this ) );
[4e6fb8e]141
[c81ebf9]142        if( *it ) { remove_at( this, n, it ); }
[4e6fb8e]143
[0b33412]144        verify( validate( this ) );
[c81ebf9]145}
146
147void register_self( alarm_node_t * this ) {
[e60e0dc]148        alarm_list_t * alarms = &event_kernel->alarms;
149
[c81ebf9]150        disable_interrupts();
[36982fc]151        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[c81ebf9]152        {
[e60e0dc]153                verify( validate( alarms ) );
154                bool first = !alarms->head;
[82ff5845]155
[e60e0dc]156                insert( alarms, this );
[82ff5845]157                if( first ) {
[e60e0dc]158                        __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
[82ff5845]159                }
[c81ebf9]160        }
[ea7d2b0]161        unlock( event_kernel->lock );
[c81ebf9]162        this->set = true;
[36982fc]163        enable_interrupts( __cfaabi_dbg_ctx );
[c81ebf9]164}
165
166void unregister_self( alarm_node_t * this ) {
167        disable_interrupts();
[36982fc]168        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[0b33412]169        {
[e60e0dc]170                verify( validate( &event_kernel->alarms ) );
171                remove( &event_kernel->alarms, this );
[0b33412]172        }
[ea7d2b0]173        unlock( event_kernel->lock );
[36982fc]174        enable_interrupts( __cfaabi_dbg_ctx );
[c81ebf9]175        this->set = false;
[4aa2fb2]176}
[6b0b624]177
178// Local Variables: //
179// mode: c //
180// tab-width: 4 //
181// End: //
Note: See TracBrowser for help on using the repository browser.