source: src/libcfa/concurrency/alarm.c @ 273cde6

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 273cde6 was 2a84d06d, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

second draft of time package and incorporation into runtime kernel

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