source: src/libcfa/concurrency/alarm.c @ ad4458f

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since ad4458f was b69ea6b, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Updated alarm to use bits/cfatime and fixed preemption for coroutines

  • Property mode set to 100644
File size: 4.4 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
12// Last Modified On : Fri Jul 21 22:35:18 2017
13// Update Count     : 1
[6cff9f3]14//
15
[c81ebf9]16extern "C" {
[82ff5845]17#include <errno.h>
18#include <stdio.h>
19#include <string.h>
20#include <unistd.h>
[c81ebf9]21#include <sys/time.h>
22}
23
[6cff9f3]24#include "alarm.h"
25#include "kernel_private.h"
[c81ebf9]26#include "preemption.h"
[6cff9f3]27
[9236060]28
[b69ea6b]29static inline void ?{}( itimerval & this, __cfa_time_t * alarm ) with( this ) {
30        it_value.tv_sec = alarm->val / (1`cfa_s).val;                   // seconds
31        it_value.tv_usec = max( (alarm->val % (1`cfa_s).val) / (1`cfa_us).val, 1000 ); // microseconds
[c40e7c5]32        it_interval.tv_sec = 0;
33        it_interval.tv_usec = 0;
[969b3fe]34}
35
[b69ea6b]36static inline void ?{}( __cfa_time_t & this, timespec * curr ) {
[969b3fe]37        uint64_t secs  = curr->tv_sec;
38        uint64_t nsecs = curr->tv_nsec;
[b69ea6b]39        this.val = from_s(secs).val + nsecs;
[969b3fe]40}
41
[c81ebf9]42//=============================================================================================
43// Clock logic
44//=============================================================================================
45
46__cfa_time_t __kernel_get_time() {
47        timespec curr;
48        clock_gettime( CLOCK_REALTIME, &curr );
[969b3fe]49        return (__cfa_time_t){ &curr };
[c81ebf9]50}
51
52void __kernel_set_timer( __cfa_time_t alarm ) {
[969b3fe]53        itimerval val = { &alarm };
[c81ebf9]54        setitimer( ITIMER_REAL, &val, NULL );
[6cff9f3]55}
56
[c81ebf9]57//=============================================================================================
58// Alarm logic
59//=============================================================================================
60
[b69ea6b]61void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = 0`cfa_s, __cfa_time_t period = 0`cfa_s ) with( this ) {
[242a902]62        this.thrd = thrd;
63        this.alarm = alarm;
64        this.period = period;
[c40e7c5]65        next = 0;
66        set = false;
67        kernel_alarm = false;
[6cff9f3]68}
69
[b69ea6b]70void ?{}( alarm_node_t & this, processor   * proc, __cfa_time_t alarm = 0`cfa_s, __cfa_time_t period = 0`cfa_s ) with( this ) {
[242a902]71        this.proc = proc;
72        this.alarm = alarm;
73        this.period = period;
[c40e7c5]74        next = 0;
75        set = false;
76        kernel_alarm = true;
[c81ebf9]77}
[6cff9f3]78
[242a902]79void ^?{}( alarm_node_t & this ) {
80        if( this.set ) {
81                unregister_self( &this );
[c81ebf9]82        }
[6cff9f3]83}
84
[36982fc]85__cfaabi_dbg_debug_do( bool validate( alarm_list_t * this ) {
[4e6fb8e]86        alarm_node_t ** it = &this->head;
87        while( (*it) ) {
88                it = &(*it)->next;
89        }
90
91        return it == this->tail;
92})
93
[6cff9f3]94static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
[0b33412]95        verify( !n->next );
[6cff9f3]96        if( p == this->tail ) {
97                this->tail = &n->next;
98        }
99        else {
100                n->next = *p;
101        }
102        *p = n;
[4e6fb8e]103
[0b33412]104        verify( validate( this ) );
[6cff9f3]105}
106
107void insert( alarm_list_t * this, alarm_node_t * n ) {
108        alarm_node_t ** it = &this->head;
109        while( (*it) && (n->alarm > (*it)->alarm) ) {
110                it = &(*it)->next;
111        }
112
113        insert_at( this, n, it );
[4e6fb8e]114
[0b33412]115        verify( validate( this ) );
[6cff9f3]116}
117
[c81ebf9]118alarm_node_t * pop( alarm_list_t * this ) {
[6cff9f3]119        alarm_node_t * head = this->head;
120        if( head ) {
121                this->head = head->next;
122                if( !head->next ) {
123                        this->tail = &this->head;
124                }
125                head->next = NULL;
126        }
[0b33412]127        verify( validate( this ) );
[6cff9f3]128        return head;
[c81ebf9]129}
130
131static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
[4aa2fb2]132        verify( it );
[ec35498]133        verify( (*it) == n );
[c81ebf9]134
[4e6fb8e]135        (*it) = n->next;
[c81ebf9]136        if( !n-> next ) {
137                this->tail = it;
138        }
139        n->next = NULL;
[4e6fb8e]140
[0b33412]141        verify( validate( this ) );
[c81ebf9]142}
143
144static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
145        alarm_node_t ** it = &this->head;
[4e6fb8e]146        while( (*it) && (*it) != n ) {
[c81ebf9]147                it = &(*it)->next;
148        }
149
[0b33412]150        verify( validate( this ) );
[4e6fb8e]151
[c81ebf9]152        if( *it ) { remove_at( this, n, it ); }
[4e6fb8e]153
[0b33412]154        verify( validate( this ) );
[c81ebf9]155}
156
157void register_self( alarm_node_t * this ) {
[e60e0dc]158        alarm_list_t * alarms = &event_kernel->alarms;
159
[c81ebf9]160        disable_interrupts();
[36982fc]161        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[c81ebf9]162        {
[e60e0dc]163                verify( validate( alarms ) );
164                bool first = !alarms->head;
[82ff5845]165
[e60e0dc]166                insert( alarms, this );
[82ff5845]167                if( first ) {
[e60e0dc]168                        __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
[82ff5845]169                }
[c81ebf9]170        }
[ea7d2b0]171        unlock( event_kernel->lock );
[c81ebf9]172        this->set = true;
[36982fc]173        enable_interrupts( __cfaabi_dbg_ctx );
[c81ebf9]174}
175
176void unregister_self( alarm_node_t * this ) {
177        disable_interrupts();
[36982fc]178        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[0b33412]179        {
[e60e0dc]180                verify( validate( &event_kernel->alarms ) );
181                remove( &event_kernel->alarms, this );
[0b33412]182        }
[ea7d2b0]183        unlock( event_kernel->lock );
[36982fc]184        enable_interrupts( __cfaabi_dbg_ctx );
[c81ebf9]185        this->set = false;
[4aa2fb2]186}
[6b0b624]187
188// Local Variables: //
189// mode: c //
190// tab-width: 4 //
191// End: //
Note: See TracBrowser for help on using the repository browser.