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
Line 
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
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Apr  9 13:36:18 2018
13// Update Count     : 61
14//
15
16extern "C" {
17#include <errno.h>
18#include <stdio.h>
19#include <string.h>
20#include <unistd.h>
21#include <sys/time.h>
22}
23
24#include "alarm.h"
25#include "kernel_private.h"
26#include "preemption.h"
27
28//=============================================================================================
29// Clock logic
30//=============================================================================================
31
32Time __kernel_get_time() {
33        timespec curr;
34        clock_gettime( CLOCK_MONOTONIC_RAW, &curr );            // CLOCK_REALTIME
35        return (Time){ curr };
36}
37
38void __kernel_set_timer( Duration alarm ) {
39        setitimer( ITIMER_REAL, &(itimerval){ alarm }, NULL );
40}
41
42//=============================================================================================
43// Alarm logic
44//=============================================================================================
45
46void ?{}( alarm_node_t & this, thread_desc * thrd, Time alarm, Duration period ) with( this ) {
47        this.thrd = thrd;
48        this.alarm = alarm;
49        this.period = period;
50        next = 0;
51        set = false;
52        kernel_alarm = false;
53}
54
55void ?{}( alarm_node_t & this, processor   * proc, Time alarm, Duration period ) with( this ) {
56        this.proc = proc;
57        this.alarm = alarm;
58        this.period = period;
59        next = 0;
60        set = false;
61        kernel_alarm = true;
62}
63
64void ^?{}( alarm_node_t & this ) {
65        if( this.set ) {
66                unregister_self( &this );
67        }
68}
69
70__cfaabi_dbg_debug_do( bool validate( alarm_list_t * this ) {
71        alarm_node_t ** it = &this->head;
72        while( (*it) ) {
73                it = &(*it)->next;
74        }
75
76        return it == this->tail;
77})
78
79static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
80        verify( !n->next );
81        if( p == this->tail ) {
82                this->tail = &n->next;
83        }
84        else {
85                n->next = *p;
86        }
87        *p = n;
88
89        verify( validate( this ) );
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 );
99
100        verify( validate( this ) );
101}
102
103alarm_node_t * pop( alarm_list_t * this ) {
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        }
112        verify( validate( this ) );
113        return head;
114}
115
116static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
117        verify( it );
118        verify( (*it) == n );
119
120        (*it) = n->next;
121        if( !n-> next ) {
122                this->tail = it;
123        }
124        n->next = NULL;
125
126        verify( validate( this ) );
127}
128
129static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
130        alarm_node_t ** it = &this->head;
131        while( (*it) && (*it) != n ) {
132                it = &(*it)->next;
133        }
134
135        verify( validate( this ) );
136
137        if( *it ) { remove_at( this, n, it ); }
138
139        verify( validate( this ) );
140}
141
142void register_self( alarm_node_t * this ) {
143        alarm_list_t * alarms = &event_kernel->alarms;
144
145        disable_interrupts();
146        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
147        {
148                verify( validate( alarms ) );
149                bool first = !alarms->head;
150
151                insert( alarms, this );
152                if( first ) {
153                        __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
154                }
155        }
156        unlock( event_kernel->lock );
157        this->set = true;
158        enable_interrupts( __cfaabi_dbg_ctx );
159}
160
161void unregister_self( alarm_node_t * this ) {
162        disable_interrupts();
163        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
164        {
165                verify( validate( &event_kernel->alarms ) );
166                remove( &event_kernel->alarms, this );
167        }
168        unlock( event_kernel->lock );
169        enable_interrupts( __cfaabi_dbg_ctx );
170        this->set = false;
171}
172
173// Local Variables: //
174// mode: c //
175// tab-width: 4 //
176// End: //
Note: See TracBrowser for help on using the repository browser.