source: src/libcfa/concurrency/alarm.c @ 8499c707

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 8499c707 was 242a902, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Convert more library files to use references

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[6cff9f3]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
[c81ebf9]17extern "C" {
[82ff5845]18#include <errno.h>
19#include <stdio.h>
20#include <string.h>
[c81ebf9]21#include <time.h>
[82ff5845]22#include <unistd.h>
[c81ebf9]23#include <sys/time.h>
24}
25
[2ac095d]26#include "libhdr.h"
27
[6cff9f3]28#include "alarm.h"
29#include "kernel_private.h"
[c81ebf9]30#include "preemption.h"
[6cff9f3]31
[c81ebf9]32//=============================================================================================
33// Clock logic
34//=============================================================================================
35
36__cfa_time_t __kernel_get_time() {
37        timespec curr;
38        clock_gettime( CLOCK_REALTIME, &curr );
[82ff5845]39        __cfa_time_t curr_time = ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
[b227f68]40        // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : current time is %lu\n", curr_time );
[82ff5845]41        return curr_time;
[c81ebf9]42}
43
44void __kernel_set_timer( __cfa_time_t alarm ) {
[47ecf2b]45        LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : set timer to %lu\n", (__cfa_time_t)alarm );
[c81ebf9]46        itimerval val;
47        val.it_value.tv_sec = alarm / TIMEGRAN;                 // seconds
48        val.it_value.tv_usec = (alarm % TIMEGRAN) / ( TIMEGRAN / 1_000_000L ); // microseconds
49        val.it_interval.tv_sec = 0;
50        val.it_interval.tv_usec = 0;
51        setitimer( ITIMER_REAL, &val, NULL );
[6cff9f3]52}
53
[c81ebf9]54//=============================================================================================
55// Alarm logic
56//=============================================================================================
57
[242a902]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;
[6cff9f3]65}
66
[242a902]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;
[c81ebf9]74}
[6cff9f3]75
[242a902]76void ^?{}( alarm_node_t & this ) {
77        if( this.set ) {
78                unregister_self( &this );
[c81ebf9]79        }
[6cff9f3]80}
81
[4e6fb8e]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
[6cff9f3]91static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
[0b33412]92        verify( !n->next );
[6cff9f3]93        if( p == this->tail ) {
94                this->tail = &n->next;
95        }
96        else {
97                n->next = *p;
98        }
99        *p = n;
[4e6fb8e]100
[0b33412]101        verify( validate( this ) );
[6cff9f3]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 );
[4e6fb8e]111
[0b33412]112        verify( validate( this ) );
[6cff9f3]113}
114
[c81ebf9]115alarm_node_t * pop( alarm_list_t * this ) {
[6cff9f3]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        }
[0b33412]124        verify( validate( this ) );
[6cff9f3]125        return head;
[c81ebf9]126}
127
128static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
[4aa2fb2]129        verify( it );
[ec35498]130        verify( (*it) == n );
[c81ebf9]131
[4e6fb8e]132        (*it) = n->next;
[c81ebf9]133        if( !n-> next ) {
134                this->tail = it;
135        }
136        n->next = NULL;
[4e6fb8e]137
[0b33412]138        verify( validate( this ) );
[c81ebf9]139}
140
141static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
142        alarm_node_t ** it = &this->head;
[4e6fb8e]143        while( (*it) && (*it) != n ) {
[c81ebf9]144                it = &(*it)->next;
145        }
146
[0b33412]147        verify( validate( this ) );
[4e6fb8e]148
[c81ebf9]149        if( *it ) { remove_at( this, n, it ); }
[4e6fb8e]150
[0b33412]151        verify( validate( this ) );
[c81ebf9]152}
153
154void register_self( alarm_node_t * this ) {
155        disable_interrupts();
[0b33412]156        verify( !systemProcessor->pending_alarm );
[2ac095d]157        lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
[c81ebf9]158        {
[0b33412]159                verify( validate( &systemProcessor->alarms ) );
[82ff5845]160                bool first = !systemProcessor->alarms.head;
161
[c81ebf9]162                insert( &systemProcessor->alarms, this );
163                if( systemProcessor->pending_alarm ) {
164                        tick_preemption();
165                }
[82ff5845]166                if( first ) {
167                        __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );
168                }
[c81ebf9]169        }
170        unlock( &systemProcessor->alarm_lock );
171        this->set = true;
[2ac095d]172        enable_interrupts( DEBUG_CTX );
[c81ebf9]173}
174
175void unregister_self( alarm_node_t * this ) {
[1c273d0]176        // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : unregister %p start\n", this );
[c81ebf9]177        disable_interrupts();
[2ac095d]178        lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
[0b33412]179        {
180                verify( validate( &systemProcessor->alarms ) );
181                remove( &systemProcessor->alarms, this );
182        }
[c81ebf9]183        unlock( &systemProcessor->alarm_lock );
[2ac095d]184        enable_interrupts( DEBUG_CTX );
[c81ebf9]185        this->set = false;
[1c273d0]186        // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Kernel : unregister %p end\n", this );
[4aa2fb2]187}
Note: See TracBrowser for help on using the repository browser.