source: libcfa/src/concurrency/alarm.cfa @ d72c074

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since d72c074 was d3ab183, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Using dlist in alarm.

Minimal interface changes in alarm.hfa applied; some spill to preemption.cfa occured.

List management logic removed, including such helper functions from alarm.cfa. Remaining logic is concurrency-protection wrapping, and picking insertion point by traversing and comparing alarm-fire times.

Integrity checking definitions removed here. Calling version brought into list.hfa by change 4d741 instead.

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