source: libcfa/src/concurrency/alarm.cfa @ 09bbe78

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 09bbe78 was e660761, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

First attempt at reducing complation time by restructuring the code.
Notably, starting the runtime has been moved to kernel/startup.cfa

  • Property mode set to 100644
File size: 3.5 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 : Wed Jun 17 16:11:35 2020
13// Update Count     : 75
14//
15
16#define __cforall_thread__
17
18#include <errno.h>
19#include <stdio.h>
20#include <unistd.h>
21#include <string.h>
22#include <sys/time.h>
23
24#include "alarm.hfa"
25#include "kernel/fwd.hfa"
26#include "preemption.hfa"
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        verifyf(alarm >= 1`us || alarm == 0, "Setting timer to < 1us (%jins)", alarm`ns);
40        setitimer( ITIMER_REAL, &(itimerval){ alarm }, 0p );
41}
42
43//=============================================================================================
44// Alarm logic
45//=============================================================================================
46
47void ?{}( alarm_node_t & this, $thread * thrd, Time alarm, Duration period ) with( this ) {
48        this.thrd = thrd;
49        this.alarm = alarm;
50        this.period = period;
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        set = false;
60        kernel_alarm = true;
61}
62
63void ^?{}( alarm_node_t & this ) {
64        if( this.set ) {
65                unregister_self( &this );
66        }
67}
68
69void insert( alarm_list_t * this, alarm_node_t * n ) {
70        alarm_node_t * it = & (*this)`first;
71        while( it && (n->alarm > it->alarm) ) {
72                it = & (*it)`next;
73        }
74        if ( it ) {
75                insert_before( *it, *n );
76        } else {
77                insert_last(*this, *n);
78        }
79
80        verify( validate( *this ) );
81}
82
83alarm_node_t * pop( alarm_list_t * this ) {
84        verify( validate( *this ) );
85        alarm_node_t * head = & (*this)`first;
86        if( head ) {
87                remove(*head);
88        }
89        verify( validate( *this ) );
90        return head;
91}
92
93void register_self( alarm_node_t * this ) {
94        alarm_list_t & alarms = event_kernel->alarms;
95
96        disable_interrupts();
97        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
98        {
99                verify( validate( alarms ) );
100                bool first = ! & alarms`first;
101
102                insert( &alarms, this );
103                if( first ) {
104                        __kernel_set_timer( alarms`first.alarm - __kernel_get_time() );
105                }
106        }
107        unlock( event_kernel->lock );
108        this->set = true;
109        enable_interrupts( __cfaabi_dbg_ctx );
110}
111
112void unregister_self( alarm_node_t * this ) {
113        disable_interrupts();
114        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
115        {
116                verify( validate( event_kernel->alarms ) );
117                remove( *this );
118        }
119        unlock( event_kernel->lock );
120        enable_interrupts( __cfaabi_dbg_ctx );
121        this->set = false;
122}
123
124//=============================================================================================
125// Utilities
126//=============================================================================================
127
128void sleep( Duration duration ) {
129        alarm_node_t node = { active_thread(), __kernel_get_time() + duration, 0`s };
130
131        register_self( &node );
132        park( __cfaabi_dbg_ctx );
133
134        /* paranoid */ verify( !node.set );
135        /* paranoid */ verify( & node`next == 0p );
136        /* paranoid */ verify( & node`prev == 0p );
137}
138
139// Local Variables: //
140// mode: c //
141// tab-width: 4 //
142// End: //
Note: See TracBrowser for help on using the repository browser.