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

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 c2b9f21 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
Line 
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
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jul 21 22:35:18 2017
13// Update Count     : 1
14//
15
16extern "C" {
17#include <errno.h>
18#include <stdio.h>
19#include <string.h>
20#include <time.h>
21#include <unistd.h>
22#include <sys/time.h>
23}
24
25#include "alarm.h"
26#include "kernel_private.h"
27#include "preemption.h"
28
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
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;
48}
49
50
51void ?{}( __cfa_time_t & this, timespec * curr ) {
52        uint64_t secs  = curr->tv_sec;
53        uint64_t nsecs = curr->tv_nsec;
54        this.val = (secs * one_second) + nsecs;
55}
56
57__cfa_time_t ?=?( __cfa_time_t & this, zero_t rhs ) {
58        this.val = 0;
59        return this;
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
67//=============================================================================================
68// Clock logic
69//=============================================================================================
70
71__cfa_time_t __kernel_get_time() {
72        timespec curr;
73        clock_gettime( CLOCK_REALTIME, &curr );
74        return (__cfa_time_t){ &curr };
75}
76
77void __kernel_set_timer( __cfa_time_t alarm ) {
78        itimerval val = { &alarm };
79        setitimer( ITIMER_REAL, &val, NULL );
80}
81
82//=============================================================================================
83// Alarm logic
84//=============================================================================================
85
86void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
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;
93}
94
95void ?{}( alarm_node_t & this, processor   * proc, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
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;
102}
103
104void ^?{}( alarm_node_t & this ) {
105        if( this.set ) {
106                unregister_self( &this );
107        }
108}
109
110__cfaabi_dbg_debug_do( bool validate( alarm_list_t * this ) {
111        alarm_node_t ** it = &this->head;
112        while( (*it) ) {
113                it = &(*it)->next;
114        }
115
116        return it == this->tail;
117})
118
119static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
120        verify( !n->next );
121        if( p == this->tail ) {
122                this->tail = &n->next;
123        }
124        else {
125                n->next = *p;
126        }
127        *p = n;
128
129        verify( validate( this ) );
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 );
139
140        verify( validate( this ) );
141}
142
143alarm_node_t * pop( alarm_list_t * this ) {
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        }
152        verify( validate( this ) );
153        return head;
154}
155
156static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
157        verify( it );
158        verify( (*it) == n );
159
160        (*it) = n->next;
161        if( !n-> next ) {
162                this->tail = it;
163        }
164        n->next = NULL;
165
166        verify( validate( this ) );
167}
168
169static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
170        alarm_node_t ** it = &this->head;
171        while( (*it) && (*it) != n ) {
172                it = &(*it)->next;
173        }
174
175        verify( validate( this ) );
176
177        if( *it ) { remove_at( this, n, it ); }
178
179        verify( validate( this ) );
180}
181
182void register_self( alarm_node_t * this ) {
183        alarm_list_t * alarms = &event_kernel->alarms;
184
185        disable_interrupts();
186        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
187        {
188                verify( validate( alarms ) );
189                bool first = !alarms->head;
190
191                insert( alarms, this );
192                if( first ) {
193                        __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
194                }
195        }
196        unlock( event_kernel->lock );
197        this->set = true;
198        enable_interrupts( __cfaabi_dbg_ctx );
199}
200
201void unregister_self( alarm_node_t * this ) {
202        disable_interrupts();
203        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
204        {
205                verify( validate( &event_kernel->alarms ) );
206                remove( &event_kernel->alarms, this );
207        }
208        unlock( event_kernel->lock );
209        enable_interrupts( __cfaabi_dbg_ctx );
210        this->set = false;
211}
212
213// Local Variables: //
214// mode: c //
215// tab-width: 4 //
216// End: //
Note: See TracBrowser for help on using the repository browser.