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 | |
---|
18 | extern "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 | |
---|
34 | Time __kernel_get_time() { |
---|
35 | timespec curr; |
---|
36 | clock_gettime( CLOCK_MONOTONIC_RAW, &curr ); // CLOCK_REALTIME |
---|
37 | return (Time){ curr }; |
---|
38 | } |
---|
39 | |
---|
40 | void __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 | |
---|
49 | void ?{}( 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 | |
---|
57 | void ?{}( 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 | |
---|
65 | void ^?{}( alarm_node_t & this ) { |
---|
66 | if( this.set ) { |
---|
67 | unregister_self( &this ); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | void 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 | |
---|
85 | alarm_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 | |
---|
95 | void 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 | |
---|
114 | void 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 | |
---|
130 | void 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: // |
---|