source: src/libcfa/concurrency/alarm.c @ 82ff5845

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

First implementation of preemption, does not appear to work with scheduling

  • Property mode set to 100644
File size: 4.2 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_DO(
40                char text[256];
41                __attribute__((unused)) int len = snprintf( text, 256, "Kernel : current time is %lu\n", curr_time );
42                LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
43        );
44        return curr_time;
45}
46
47void __kernel_set_timer( __cfa_time_t alarm ) {
48
49        LIB_DEBUG_DO(
50                char text[256];
51                __attribute__((unused)) int len = snprintf( text, 256, "Kernel : set timer to %lu\n", (__cfa_time_t)alarm );
52                LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
53        );
54
55        itimerval val;
56        val.it_value.tv_sec = alarm / TIMEGRAN;                 // seconds
57        val.it_value.tv_usec = (alarm % TIMEGRAN) / ( TIMEGRAN / 1_000_000L ); // microseconds
58        val.it_interval.tv_sec = 0;
59        val.it_interval.tv_usec = 0;
60        setitimer( ITIMER_REAL, &val, NULL );
61}
62
63//=============================================================================================
64// Alarm logic
65//=============================================================================================
66
67void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
68        this->thrd = thrd;
69        this->alarm = alarm;
70        this->period = period;
71        this->next = 0;
72        this->set = false;
73        this->kernel_alarm = false;
74}
75
76void ?{}( alarm_node_t * this, processor   * proc, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
77        this->proc = proc;
78        this->alarm = alarm;
79        this->period = period;
80        this->next = 0;
81        this->set = false;
82        this->kernel_alarm = true;
83}
84
85void ^?{}( alarm_node_t * this ) {
86        if( this->set ) {
87                unregister_self( this );
88        }
89}
90
91static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
92        assert( !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
102void insert( alarm_list_t * this, alarm_node_t * n ) {
103        alarm_node_t ** it = &this->head;
104        while( (*it) && (n->alarm > (*it)->alarm) ) {
105                it = &(*it)->next;
106        }
107
108        insert_at( this, n, it );
109}
110
111alarm_node_t * pop( alarm_list_t * this ) {
112        alarm_node_t * head = this->head;
113        if( head ) {
114                this->head = head->next;
115                if( !head->next ) {
116                        this->tail = &this->head;
117                }
118                head->next = NULL;
119        }
120        return head;
121}
122
123static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
124        assert( it );
125        assert( (*it)->next == n );
126
127        (*it)->next = n->next;
128        if( !n-> next ) {
129                this->tail = it;
130        }
131        n->next = NULL;
132}
133
134static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
135        alarm_node_t ** it = &this->head;
136        while( (*it) && (*it)->next != n ) {
137                it = &(*it)->next;
138        }
139
140        if( *it ) { remove_at( this, n, it ); }
141}
142
143void register_self( alarm_node_t * this ) {
144        disable_interrupts();
145        assert( !systemProcessor->pending_alarm );
146        lock( &systemProcessor->alarm_lock );
147        {
148                bool first = !systemProcessor->alarms.head;
149
150                insert( &systemProcessor->alarms, this );
151                if( systemProcessor->pending_alarm ) {
152                        tick_preemption();
153                }
154                if( first ) {
155                        __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );
156                }
157        }
158        unlock( &systemProcessor->alarm_lock );
159        this->set = true;
160        enable_interrupts();
161}
162
163void unregister_self( alarm_node_t * this ) {
164        disable_interrupts();
165        lock( &systemProcessor->alarm_lock );
166        remove( &systemProcessor->alarms, this );
167        unlock( &systemProcessor->alarm_lock );
168        disable_interrupts();
169        this->set = false;
170}
Note: See TracBrowser for help on using the repository browser.