source: libcfa/src/concurrency/alarm.cfa @ 8419b76

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 8419b76 was c18bf9e, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Visibility concurrency

  • Property mode set to 100644
File size: 4.0 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
[c1ee231]12// Last Modified On : Wed Jun 17 16:11:35 2020
13// Update Count     : 75
[6cff9f3]14//
15
[2026bb6]16#define __cforall_thread__
[c9ec301]17// #define __CFA_DEBUG_PRINT_PREEMPTION__
[2026bb6]18
[82ff5845]19#include <errno.h>
20#include <stdio.h>
21#include <unistd.h>
[c1ee231]22#include <string.h>
[c81ebf9]23#include <sys/time.h>
24
[73abe95]25#include "alarm.hfa"
[e660761]26#include "kernel/fwd.hfa"
[73abe95]27#include "preemption.hfa"
[6cff9f3]28
[c81ebf9]29//=============================================================================================
30// Clock logic
31//=============================================================================================
32
[2a84d06d]33Time __kernel_get_time() {
[c81ebf9]34        timespec curr;
[2a84d06d]35        clock_gettime( CLOCK_MONOTONIC_RAW, &curr );            // CLOCK_REALTIME
36        return (Time){ curr };
[c81ebf9]37}
38
[2a84d06d]39void __kernel_set_timer( Duration alarm ) {
[c457dc41]40        alarm = max(alarm, 1`us);
41        itimerval otv @= { 0 };
42        getitimer( ITIMER_REAL, &otv );
43        Duration od = { otv.it_value };
44        if(od == 0 || od > alarm) {
45                setitimer( ITIMER_REAL, &(itimerval){ alarm }, 0p );
46        }
[6cff9f3]47}
48
[c81ebf9]49//=============================================================================================
50// Alarm logic
51//=============================================================================================
52
[e84ab3d]53void ?{}( alarm_node_t & this, thread$ * thrd, Duration alarm, Duration period) with( this ) {
[c457dc41]54        this.initial = alarm;
55        this.period  = period;
[242a902]56        this.thrd = thrd;
[0f1fb78]57        this.timeval = __kernel_get_time() + alarm;
[c40e7c5]58        set = false;
[eeb5023]59        type = User;
[6cff9f3]60}
61
[c457dc41]62void ?{}( alarm_node_t & this, processor * proc, Duration alarm, Duration period ) with( this ) {
63        this.initial = alarm;
64        this.period  = period;
[242a902]65        this.proc = proc;
[0f1fb78]66        this.timeval = __kernel_get_time() + alarm;
[c40e7c5]67        set = false;
[eeb5023]68        type = Kernel;
69}
[c457dc41]70void ?{}( alarm_node_t & this, Alarm_Callback callback, Duration alarm, Duration period ) with( this ) {
[0f1fb78]71        this.callback = callback;
[c457dc41]72        this.initial = alarm;
73        this.period  = period;
[0f1fb78]74        this.timeval = __kernel_get_time() + alarm;
[eeb5023]75        set = false;
76        type = Callback;
[c81ebf9]77}
[6cff9f3]78
[242a902]79void ^?{}( alarm_node_t & this ) {
80        if( this.set ) {
81                unregister_self( &this );
[c81ebf9]82        }
[6cff9f3]83}
84
85void insert( alarm_list_t * this, alarm_node_t * n ) {
[d3ab183]86        alarm_node_t * it = & (*this)`first;
[c457dc41]87        while( it && (n->timeval > it->timeval) ) {
[d3ab183]88                it = & (*it)`next;
89        }
90        if ( it ) {
91                insert_before( *it, *n );
92        } else {
93                insert_last(*this, *n);
[6cff9f3]94        }
95
[d3ab183]96        verify( validate( *this ) );
[6cff9f3]97}
98
[c81ebf9]99alarm_node_t * pop( alarm_list_t * this ) {
[d3ab183]100        verify( validate( *this ) );
101        alarm_node_t * head = & (*this)`first;
[6cff9f3]102        if( head ) {
[d3ab183]103                remove(*head);
[6cff9f3]104        }
[d3ab183]105        verify( validate( *this ) );
[6cff9f3]106        return head;
[c81ebf9]107}
108
109void register_self( alarm_node_t * this ) {
[d3ab183]110        alarm_list_t & alarms = event_kernel->alarms;
[e60e0dc]111
[c81ebf9]112        disable_interrupts();
[36982fc]113        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[c81ebf9]114        {
[c457dc41]115                /* paranoid */ verify( validate( alarms ) );
[82ff5845]116
[0f1fb78]117                Time curr = __kernel_get_time();
[c457dc41]118                __cfadbg_print_safe( preemption, " KERNEL: alarm inserting %p (%lu -> %lu).\n", this, curr.tn, this->timeval.tn );
[d3ab183]119                insert( &alarms, this );
[0f1fb78]120                __kernel_set_timer( this->timeval - curr);
[afd7faf]121                this->set = true;
[c81ebf9]122        }
[ea7d2b0]123        unlock( event_kernel->lock );
[a3821fa]124        enable_interrupts();
[c81ebf9]125}
126
127void unregister_self( alarm_node_t * this ) {
128        disable_interrupts();
[36982fc]129        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
[0b33412]130        {
[d3ab183]131                verify( validate( event_kernel->alarms ) );
[afd7faf]132                if (this->set) remove( *this );
133                this->set = false;
[0b33412]134        }
[ea7d2b0]135        unlock( event_kernel->lock );
[a3821fa]136        enable_interrupts();
[4aa2fb2]137}
[6b0b624]138
[2d8f7b0]139//=============================================================================================
140// Utilities
141//=============================================================================================
142
[c18bf9e]143void sleep( Duration duration ) libcfa_public {
[c457dc41]144        alarm_node_t node = { active_thread(), duration, 0`s };
[2d8f7b0]145
146        register_self( &node );
[e235429]147        park();
[2d8f7b0]148
149        /* paranoid */ verify( !node.set );
[d3ab183]150        /* paranoid */ verify( & node`next == 0p );
151        /* paranoid */ verify( & node`prev == 0p );
[2d8f7b0]152}
153
[6b0b624]154// Local Variables: //
155// mode: c //
156// tab-width: 4 //
157// End: //
Note: See TracBrowser for help on using the repository browser.