source: src/libcfa/concurrency/alarm.c @ 1c273d0

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

preemption works for threads

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