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

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 ffd0ac2 was c2b9f21, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Removed libhdr, moved its content to bits

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