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

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since a8706fc was 8ad6533, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

remove cfatime.h, move itimerval constructor to "time", update concurrent examples to use Duration

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[6cff9f3]1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// alarm.c --
8//
9// Author           : Thierry Delisle
10// Created On       : Fri Jun 2 11:31:25 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
[8ad6533]12// Last Modified On : Mon Apr  9 13:36:18 2018
13// Update Count     : 61
[6cff9f3]14//
15
[c81ebf9]16extern "C" {
[82ff5845]17#include <errno.h>
18#include <stdio.h>
19#include <string.h>
20#include <unistd.h>
[c81ebf9]21#include <sys/time.h>
22}
23
[6cff9f3]24#include "alarm.h"
25#include "kernel_private.h"
[c81ebf9]26#include "preemption.h"
[6cff9f3]27
[c81ebf9]28//=============================================================================================
29// Clock logic
30//=============================================================================================
31
[2a84d06d]32Time __kernel_get_time() {
[c81ebf9]33        timespec curr;
[2a84d06d]34        clock_gettime( CLOCK_MONOTONIC_RAW, &curr );            // CLOCK_REALTIME
35        return (Time){ curr };
[c81ebf9]36}
37
[2a84d06d]38void __kernel_set_timer( Duration alarm ) {
39        setitimer( ITIMER_REAL, &(itimerval){ alarm }, NULL );
[6cff9f3]40}
41
[c81ebf9]42//=============================================================================================
43// Alarm logic
44//=============================================================================================
45
[2a84d06d]46void ?{}( alarm_node_t & this, thread_desc * thrd, Time alarm, Duration period ) with( this ) {
[242a902]47        this.thrd = thrd;
48        this.alarm = alarm;
49        this.period = period;
[c40e7c5]50        next = 0;
51        set = false;
52        kernel_alarm = false;
[6cff9f3]53}
54
[2a84d06d]55void ?{}( alarm_node_t & this, processor   * proc, Time alarm, Duration period ) with( this ) {
[242a902]56        this.proc = proc;
57        this.alarm = alarm;
58        this.period = period;
[c40e7c5]59        next = 0;
60        set = false;
61        kernel_alarm = true;
[c81ebf9]62}
[6cff9f3]63
[242a902]64void ^?{}( alarm_node_t & this ) {
65        if( this.set ) {
66                unregister_self( &this );
[c81ebf9]67        }
[6cff9f3]68}
69
[36982fc]70__cfaabi_dbg_debug_do( bool validate( alarm_list_t * this ) {
[4e6fb8e]71        alarm_node_t ** it = &this->head;
72        while( (*it) ) {
73                it = &(*it)->next;
74        }
75
76        return it == this->tail;
77})
78
[6cff9f3]79static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
[0b33412]80        verify( !n->next );
[6cff9f3]81        if( p == this->tail ) {
82                this->tail = &n->next;
83        }
84        else {
85                n->next = *p;
86        }
87        *p = n;
[4e6fb8e]88
[0b33412]89        verify( validate( this ) );
[6cff9f3]90}
91
92void insert( alarm_list_t * this, alarm_node_t * n ) {
93        alarm_node_t ** it = &this->head;
94        while( (*it) && (n->alarm > (*it)->alarm) ) {
95                it = &(*it)->next;
96        }
97
98        insert_at( this, n, it );
[4e6fb8e]99
[0b33412]100        verify( validate( this ) );
[6cff9f3]101}
102
[c81ebf9]103alarm_node_t * pop( alarm_list_t * this ) {
[6cff9f3]104        alarm_node_t * head = this->head;
105        if( head ) {
106                this->head = head->next;
107                if( !head->next ) {
108                        this->tail = &this->head;
109                }
110                head->next = NULL;
111        }
[0b33412]112        verify( validate( this ) );
[6cff9f3]113        return head;
[c81ebf9]114}
115
116static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
[4aa2fb2]117        verify( it );
[ec35498]118        verify( (*it) == n );
[c81ebf9]119
[4e6fb8e]120        (*it) = n->next;
[c81ebf9]121        if( !n-> next ) {
122                this->tail = it;
123        }
124        n->next = NULL;
[4e6fb8e]125
[0b33412]126        verify( validate( this ) );
[c81ebf9]127}
128
129static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
130        alarm_node_t ** it = &this->head;
[4e6fb8e]131        while( (*it) && (*it) != n ) {
[c81ebf9]132                it = &(*it)->next;
133        }
134
[0b33412]135        verify( validate( this ) );
[4e6fb8e]136
[c81ebf9]137        if( *it ) { remove_at( this, n, it ); }
[4e6fb8e]138
[0b33412]139        verify( validate( this ) );
[c81ebf9]140}
141
142void register_self( alarm_node_t * this ) {
[e60e0dc]143        alarm_list_t * alarms = &event_kernel->alarms;
144
[c81ebf9]145        disable_interrupts();
[36982fc]146        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[c81ebf9]147        {
[e60e0dc]148                verify( validate( alarms ) );
149                bool first = !alarms->head;
[82ff5845]150
[e60e0dc]151                insert( alarms, this );
[82ff5845]152                if( first ) {
[e60e0dc]153                        __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
[82ff5845]154                }
[c81ebf9]155        }
[ea7d2b0]156        unlock( event_kernel->lock );
[c81ebf9]157        this->set = true;
[36982fc]158        enable_interrupts( __cfaabi_dbg_ctx );
[c81ebf9]159}
160
161void unregister_self( alarm_node_t * this ) {
162        disable_interrupts();
[36982fc]163        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[0b33412]164        {
[e60e0dc]165                verify( validate( &event_kernel->alarms ) );
166                remove( &event_kernel->alarms, this );
[0b33412]167        }
[ea7d2b0]168        unlock( event_kernel->lock );
[36982fc]169        enable_interrupts( __cfaabi_dbg_ctx );
[c81ebf9]170        this->set = false;
[4aa2fb2]171}
[6b0b624]172
173// Local Variables: //
174// mode: c //
175// tab-width: 4 //
176// End: //
Note: See TracBrowser for help on using the repository browser.