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

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 cd99ef1 was 969b3fe, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

More clean-up of the kernel code

  • Property mode set to 100644
File size: 5.3 KB
Line 
1//                              -*- Mode: CFA -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// alarm.c --
9//
10// Author           : Thierry Delisle
11// Created On       : Fri Jun 2 11:31:25 2017
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count     : 0
15//
16
17extern "C" {
18#include <errno.h>
19#include <stdio.h>
20#include <string.h>
21#include <time.h>
22#include <unistd.h>
23#include <sys/time.h>
24}
25
26#include "libhdr.h"
27
28#include "alarm.h"
29#include "kernel_private.h"
30#include "preemption.h"
31
32//=============================================================================================
33// time type
34//=============================================================================================
35
36#define one_second         1_000_000_000ul
37#define one_milisecond         1_000_000ul
38#define one_microsecond            1_000ul
39#define one_nanosecond                 1ul
40
41__cfa_time_t zero_time = { 0 };
42
43void ?{}( __cfa_time_t * this ) { this->val = 0; }
44void ?{}( __cfa_time_t * this, zero_t zero ) { this->val = 0; }
45
46void ?{}( itimerval * this, __cfa_time_t * alarm ) {
47        this->it_value.tv_sec = alarm->val / one_second;                        // seconds
48        this->it_value.tv_usec = max( (alarm->val % one_second) / one_microsecond, 1000 ); // microseconds
49        this->it_interval.tv_sec = 0;
50        this->it_interval.tv_usec = 0;
51}
52
53
54void ?{}( __cfa_time_t * this, timespec * curr ) {
55        uint64_t secs  = curr->tv_sec;
56        uint64_t nsecs = curr->tv_nsec;
57        this->val = (secs * one_second) + nsecs;
58}
59
60__cfa_time_t ?=?( __cfa_time_t * this, zero_t rhs ) {
61        this->val = 0;
62        return *this;
63}
64
65__cfa_time_t from_s ( uint64_t val ) { __cfa_time_t ret; ret.val = val * 1_000_000_000ul; return ret; }
66__cfa_time_t from_ms( uint64_t val ) { __cfa_time_t ret; ret.val = val *     1_000_000ul; return ret; }
67__cfa_time_t from_us( uint64_t val ) { __cfa_time_t ret; ret.val = val *         1_000ul; return ret; }
68__cfa_time_t from_ns( uint64_t val ) { __cfa_time_t ret; ret.val = val *             1ul; return ret; }
69
70//=============================================================================================
71// Clock logic
72//=============================================================================================
73
74__cfa_time_t __kernel_get_time() {
75        timespec curr;
76        clock_gettime( CLOCK_REALTIME, &curr );
77        return (__cfa_time_t){ &curr };
78}
79
80void __kernel_set_timer( __cfa_time_t alarm ) {
81        itimerval val = { &alarm };
82        setitimer( ITIMER_REAL, &val, NULL );
83}
84
85//=============================================================================================
86// Alarm logic
87//=============================================================================================
88
89void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
90        this->thrd = thrd;
91        this->alarm = alarm;
92        this->period = period;
93        this->next = 0;
94        this->set = false;
95        this->kernel_alarm = false;
96}
97
98void ?{}( alarm_node_t * this, processor   * proc, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
99        this->proc = proc;
100        this->alarm = alarm;
101        this->period = period;
102        this->next = 0;
103        this->set = false;
104        this->kernel_alarm = true;
105}
106
107void ^?{}( alarm_node_t * this ) {
108        if( this->set ) {
109                unregister_self( this );
110        }
111}
112
113LIB_DEBUG_DO( bool validate( alarm_list_t * this ) {
114        alarm_node_t ** it = &this->head;
115        while( (*it) ) {
116                it = &(*it)->next;
117        }
118
119        return it == this->tail;
120})
121
122static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
123        verify( !n->next );
124        if( p == this->tail ) {
125                this->tail = &n->next;
126        }
127        else {
128                n->next = *p;
129        }
130        *p = n;
131
132        verify( validate( this ) );
133}
134
135void insert( alarm_list_t * this, alarm_node_t * n ) {
136        alarm_node_t ** it = &this->head;
137        while( (*it) && (n->alarm > (*it)->alarm) ) {
138                it = &(*it)->next;
139        }
140
141        insert_at( this, n, it );
142
143        verify( validate( this ) );
144}
145
146alarm_node_t * pop( alarm_list_t * this ) {
147        alarm_node_t * head = this->head;
148        if( head ) {
149                this->head = head->next;
150                if( !head->next ) {
151                        this->tail = &this->head;
152                }
153                head->next = NULL;
154        }
155        verify( validate( this ) );
156        return head;
157}
158
159static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
160        verify( it );
161        verify( (*it) == n );
162
163        (*it) = n->next;
164        if( !n-> next ) {
165                this->tail = it;
166        }
167        n->next = NULL;
168
169        verify( validate( this ) );
170}
171
172static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
173        alarm_node_t ** it = &this->head;
174        while( (*it) && (*it) != n ) {
175                it = &(*it)->next;
176        }
177
178        verify( validate( this ) );
179
180        if( *it ) { remove_at( this, n, it ); }
181
182        verify( validate( this ) );
183}
184
185void register_self( alarm_node_t * this ) {
186        alarm_list_t * alarms = &event_kernel->alarms;
187
188        disable_interrupts();
189        lock( &event_kernel->lock DEBUG_CTX2 );
190        {
191                verify( validate( alarms ) );
192                bool first = !alarms->head;
193
194                insert( alarms, this );
195                if( first ) {
196                        __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
197                }
198        }
199        unlock( &event_kernel->lock );
200        this->set = true;
201        enable_interrupts( DEBUG_CTX );
202}
203
204void unregister_self( alarm_node_t * this ) {
205        disable_interrupts();
206        lock( &event_kernel->lock DEBUG_CTX2 );
207        {
208                verify( validate( &event_kernel->alarms ) );
209                remove( &event_kernel->alarms, this );
210        }
211        unlock( &event_kernel->lock );
212        enable_interrupts( DEBUG_CTX );
213        this->set = false;
214}
Note: See TracBrowser for help on using the repository browser.