source: src/libcfa/concurrency/alarm.c @ 2f6a7e93

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 2f6a7e93 was 9236060, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' into references

  • Property mode set to 100644
File size: 5.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>
[c81ebf9]20#include <time.h>
[82ff5845]21#include <unistd.h>
[c81ebf9]22#include <sys/time.h>
23}
24
[2ac095d]25#include "libhdr.h"
26
[6cff9f3]27#include "alarm.h"
28#include "kernel_private.h"
[c81ebf9]29#include "preemption.h"
[6cff9f3]30
[969b3fe]31//=============================================================================================
32// time type
33//=============================================================================================
34
35#define one_second         1_000_000_000ul
36#define one_milisecond         1_000_000ul
37#define one_microsecond            1_000ul
38#define one_nanosecond                 1ul
39
40__cfa_time_t zero_time = { 0 };
41
[9236060]42void ?{}( __cfa_time_t & this ) { this.val = 0; }
43void ?{}( __cfa_time_t & this, zero_t zero ) { this.val = 0; }
44
45void ?{}( itimerval & this, __cfa_time_t * alarm ) {
46        this.it_value.tv_sec = alarm->val / one_second;                 // seconds
47        this.it_value.tv_usec = max( (alarm->val % one_second) / one_microsecond, 1000 ); // microseconds
48        this.it_interval.tv_sec = 0;
49        this.it_interval.tv_usec = 0;
[969b3fe]50}
51
52
[9236060]53void ?{}( __cfa_time_t & this, timespec * curr ) {
[969b3fe]54        uint64_t secs  = curr->tv_sec;
55        uint64_t nsecs = curr->tv_nsec;
[9236060]56        this.val = (secs * one_second) + nsecs;
[969b3fe]57}
58
[9236060]59__cfa_time_t ?=?( __cfa_time_t & this, zero_t rhs ) {
60        this.val = 0;
61        return this;
[969b3fe]62}
63
64__cfa_time_t from_s ( uint64_t val ) { __cfa_time_t ret; ret.val = val * 1_000_000_000ul; return ret; }
65__cfa_time_t from_ms( uint64_t val ) { __cfa_time_t ret; ret.val = val *     1_000_000ul; return ret; }
66__cfa_time_t from_us( uint64_t val ) { __cfa_time_t ret; ret.val = val *         1_000ul; return ret; }
67__cfa_time_t from_ns( uint64_t val ) { __cfa_time_t ret; ret.val = val *             1ul; return ret; }
68
[c81ebf9]69//=============================================================================================
70// Clock logic
71//=============================================================================================
72
73__cfa_time_t __kernel_get_time() {
74        timespec curr;
75        clock_gettime( CLOCK_REALTIME, &curr );
[969b3fe]76        return (__cfa_time_t){ &curr };
[c81ebf9]77}
78
79void __kernel_set_timer( __cfa_time_t alarm ) {
[969b3fe]80        itimerval val = { &alarm };
[c81ebf9]81        setitimer( ITIMER_REAL, &val, NULL );
[6cff9f3]82}
83
[c81ebf9]84//=============================================================================================
85// Alarm logic
86//=============================================================================================
87
[9236060]88void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
[242a902]89        this.thrd = thrd;
90        this.alarm = alarm;
91        this.period = period;
92        this.next = 0;
93        this.set = false;
94        this.kernel_alarm = false;
[6cff9f3]95}
96
[9236060]97void ?{}( alarm_node_t & this, processor   * proc, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
[242a902]98        this.proc = proc;
99        this.alarm = alarm;
100        this.period = period;
101        this.next = 0;
102        this.set = false;
103        this.kernel_alarm = true;
[c81ebf9]104}
[6cff9f3]105
[242a902]106void ^?{}( alarm_node_t & this ) {
107        if( this.set ) {
108                unregister_self( &this );
[c81ebf9]109        }
[6cff9f3]110}
111
[4e6fb8e]112LIB_DEBUG_DO( bool validate( alarm_list_t * this ) {
113        alarm_node_t ** it = &this->head;
114        while( (*it) ) {
115                it = &(*it)->next;
116        }
117
118        return it == this->tail;
119})
120
[6cff9f3]121static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
[0b33412]122        verify( !n->next );
[6cff9f3]123        if( p == this->tail ) {
124                this->tail = &n->next;
125        }
126        else {
127                n->next = *p;
128        }
129        *p = n;
[4e6fb8e]130
[0b33412]131        verify( validate( this ) );
[6cff9f3]132}
133
134void insert( alarm_list_t * this, alarm_node_t * n ) {
135        alarm_node_t ** it = &this->head;
136        while( (*it) && (n->alarm > (*it)->alarm) ) {
137                it = &(*it)->next;
138        }
139
140        insert_at( this, n, it );
[4e6fb8e]141
[0b33412]142        verify( validate( this ) );
[6cff9f3]143}
144
[c81ebf9]145alarm_node_t * pop( alarm_list_t * this ) {
[6cff9f3]146        alarm_node_t * head = this->head;
147        if( head ) {
148                this->head = head->next;
149                if( !head->next ) {
150                        this->tail = &this->head;
151                }
152                head->next = NULL;
153        }
[0b33412]154        verify( validate( this ) );
[6cff9f3]155        return head;
[c81ebf9]156}
157
158static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
[4aa2fb2]159        verify( it );
[ec35498]160        verify( (*it) == n );
[c81ebf9]161
[4e6fb8e]162        (*it) = n->next;
[c81ebf9]163        if( !n-> next ) {
164                this->tail = it;
165        }
166        n->next = NULL;
[4e6fb8e]167
[0b33412]168        verify( validate( this ) );
[c81ebf9]169}
170
171static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
172        alarm_node_t ** it = &this->head;
[4e6fb8e]173        while( (*it) && (*it) != n ) {
[c81ebf9]174                it = &(*it)->next;
175        }
176
[0b33412]177        verify( validate( this ) );
[4e6fb8e]178
[c81ebf9]179        if( *it ) { remove_at( this, n, it ); }
[4e6fb8e]180
[0b33412]181        verify( validate( this ) );
[c81ebf9]182}
183
184void register_self( alarm_node_t * this ) {
[e60e0dc]185        alarm_list_t * alarms = &event_kernel->alarms;
186
[c81ebf9]187        disable_interrupts();
[e60e0dc]188        lock( &event_kernel->lock DEBUG_CTX2 );
[c81ebf9]189        {
[e60e0dc]190                verify( validate( alarms ) );
191                bool first = !alarms->head;
[82ff5845]192
[e60e0dc]193                insert( alarms, this );
[82ff5845]194                if( first ) {
[e60e0dc]195                        __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
[82ff5845]196                }
[c81ebf9]197        }
[e60e0dc]198        unlock( &event_kernel->lock );
[c81ebf9]199        this->set = true;
[2ac095d]200        enable_interrupts( DEBUG_CTX );
[c81ebf9]201}
202
203void unregister_self( alarm_node_t * this ) {
204        disable_interrupts();
[e60e0dc]205        lock( &event_kernel->lock DEBUG_CTX2 );
[0b33412]206        {
[e60e0dc]207                verify( validate( &event_kernel->alarms ) );
208                remove( &event_kernel->alarms, this );
[0b33412]209        }
[e60e0dc]210        unlock( &event_kernel->lock );
[2ac095d]211        enable_interrupts( DEBUG_CTX );
[c81ebf9]212        this->set = false;
[4aa2fb2]213}
[6b0b624]214
215// Local Variables: //
216// mode: c //
217// tab-width: 4 //
218// End: //
Note: See TracBrowser for help on using the repository browser.