source: src/libcfa/concurrency/alarm.c @ 5bd0aad

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 5bd0aad was e60e0dc, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Some cleanu[ in the kernel, notably phasing out the system processor

  • Property mode set to 100644
File size: 4.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// Clock logic
34//=============================================================================================
35
36__cfa_time_t __kernel_get_time() {
37        timespec curr;
38        clock_gettime( CLOCK_REALTIME, &curr );
39        __cfa_time_t curr_time = ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
40        // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : current time is %lu\n", curr_time );
41        return curr_time;
42}
43
44void __kernel_set_timer( __cfa_time_t alarm ) {
45        LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : set timer to %llu\n", (__cfa_time_t)alarm );
46        itimerval val;
47        val.it_value.tv_sec = alarm / TIMEGRAN;                 // seconds
48        val.it_value.tv_usec = max( (alarm % TIMEGRAN) / ( TIMEGRAN / 1_000_000L ), 1000 ); // microseconds
49        val.it_interval.tv_sec = 0;
50        val.it_interval.tv_usec = 0;
51        setitimer( ITIMER_REAL, &val, NULL );
52}
53
54//=============================================================================================
55// Alarm logic
56//=============================================================================================
57
58void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
59        this->thrd = thrd;
60        this->alarm = alarm;
61        this->period = period;
62        this->next = 0;
63        this->set = false;
64        this->kernel_alarm = false;
65}
66
67void ?{}( alarm_node_t * this, processor   * proc, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
68        this->proc = proc;
69        this->alarm = alarm;
70        this->period = period;
71        this->next = 0;
72        this->set = false;
73        this->kernel_alarm = true;
74}
75
76void ^?{}( alarm_node_t * this ) {
77        if( this->set ) {
78                unregister_self( this );
79        }
80}
81
82LIB_DEBUG_DO( bool validate( alarm_list_t * this ) {
83        alarm_node_t ** it = &this->head;
84        while( (*it) ) {
85                it = &(*it)->next;
86        }
87
88        return it == this->tail;
89})
90
91static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
92        verify( !n->next );
93        if( p == this->tail ) {
94                this->tail = &n->next;
95        }
96        else {
97                n->next = *p;
98        }
99        *p = n;
100
101        verify( validate( this ) );
102}
103
104void insert( alarm_list_t * this, alarm_node_t * n ) {
105        alarm_node_t ** it = &this->head;
106        while( (*it) && (n->alarm > (*it)->alarm) ) {
107                it = &(*it)->next;
108        }
109
110        insert_at( this, n, it );
111
112        verify( validate( this ) );
113}
114
115alarm_node_t * pop( alarm_list_t * this ) {
116        alarm_node_t * head = this->head;
117        if( head ) {
118                this->head = head->next;
119                if( !head->next ) {
120                        this->tail = &this->head;
121                }
122                head->next = NULL;
123        }
124        verify( validate( this ) );
125        return head;
126}
127
128static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
129        verify( it );
130        verify( (*it) == n );
131
132        (*it) = n->next;
133        if( !n-> next ) {
134                this->tail = it;
135        }
136        n->next = NULL;
137
138        verify( validate( this ) );
139}
140
141static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
142        alarm_node_t ** it = &this->head;
143        while( (*it) && (*it) != n ) {
144                it = &(*it)->next;
145        }
146
147        verify( validate( this ) );
148
149        if( *it ) { remove_at( this, n, it ); }
150
151        verify( validate( this ) );
152}
153
154void register_self( alarm_node_t * this ) {
155        alarm_list_t * alarms = &event_kernel->alarms;
156
157        disable_interrupts();
158        lock( &event_kernel->lock DEBUG_CTX2 );
159        {
160                verify( validate( alarms ) );
161                bool first = !alarms->head;
162
163                insert( alarms, this );
164                if( first ) {
165                        __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
166                }
167        }
168        unlock( &event_kernel->lock );
169        this->set = true;
170        enable_interrupts( DEBUG_CTX );
171}
172
173void unregister_self( alarm_node_t * this ) {
174        disable_interrupts();
175        lock( &event_kernel->lock DEBUG_CTX2 );
176        {
177                verify( validate( &event_kernel->alarms ) );
178                remove( &event_kernel->alarms, this );
179        }
180        unlock( &event_kernel->lock );
181        enable_interrupts( DEBUG_CTX );
182        this->set = false;
183}
Note: See TracBrowser for help on using the repository browser.