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

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since eeb5023 was eeb5023, checked in by Colby Alexander Parsons <caparsons@…>, 3 years ago

added full timeout functionality to unified condition variables

  • Property mode set to 100644
File size: 3.7 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        type = User;
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        type = Kernel;
61}
62void ?{}( alarm_node_t & this, $thread * thrd, Time alarm, Duration period, Alarm_Callback callback ) with( this ) {
63        this.thrd = thrd;
64        this.alarm = alarm;
65        this.period = period;
66        this.callback = callback;
67        set = false;
68        type = Callback;
69}
70
71void ^?{}( alarm_node_t & this ) {
72        if( this.set ) {
73                unregister_self( &this );
74        }
75}
76
77void insert( alarm_list_t * this, alarm_node_t * n ) {
78        alarm_node_t * it = & (*this)`first;
79        while( it && (n->alarm > it->alarm) ) {
80                it = & (*it)`next;
81        }
82        if ( it ) {
83                insert_before( *it, *n );
84        } else {
85                insert_last(*this, *n);
86        }
87
88        verify( validate( *this ) );
89}
90
91alarm_node_t * pop( alarm_list_t * this ) {
92        verify( validate( *this ) );
93        alarm_node_t * head = & (*this)`first;
94        if( head ) {
95                remove(*head);
96        }
97        verify( validate( *this ) );
98        return head;
99}
100
101void register_self( alarm_node_t * this ) {
102        alarm_list_t & alarms = event_kernel->alarms;
103
104        disable_interrupts();
105        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
106        {
107                verify( validate( alarms ) );
108                bool first = ! & alarms`first;
109
110                insert( &alarms, this );
111                if( first ) {
112                        __kernel_set_timer( alarms`first.alarm - __kernel_get_time() );
113                }
114        }
115        unlock( event_kernel->lock );
116        this->set = true;
117        enable_interrupts( __cfaabi_dbg_ctx );
118}
119
120void unregister_self( alarm_node_t * this ) {
121        disable_interrupts();
122        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
123        {
124                verify( validate( event_kernel->alarms ) );
125                remove( *this );
126        }
127        unlock( event_kernel->lock );
128        enable_interrupts( __cfaabi_dbg_ctx );
129        this->set = false;
130}
131
132//=============================================================================================
133// Utilities
134//=============================================================================================
135
136void sleep( Duration duration ) {
137        alarm_node_t node = { active_thread(), __kernel_get_time() + duration, 0`s };
138
139        register_self( &node );
140        park();
141
142        /* paranoid */ verify( !node.set );
143        /* paranoid */ verify( & node`next == 0p );
144        /* paranoid */ verify( & node`prev == 0p );
145}
146
147// Local Variables: //
148// mode: c //
149// tab-width: 4 //
150// End: //
Note: See TracBrowser for help on using the repository browser.