Changeset c81ebf9 for src/libcfa/concurrency/alarm.c
- Timestamp:
- Jun 6, 2017, 11:45:13 AM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- c5ac6d5
- Parents:
- 7b13aeb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/alarm.c
r7b13aeb rc81ebf9 15 15 // 16 16 17 extern "C" { 18 #include <time.h> 19 #include <sys/time.h> 20 } 21 17 22 #include "alarm.h" 18 23 #include "kernel_private.h" 24 #include "preemption.h" 19 25 20 static void register_self( alarm_node_t * this ) { 21 lock( &systemProcessor->alarm_lock ); 22 insert( &systemProcessor->alarms, this ); 23 unlock( &systemProcessor->alarm_lock ); 26 //============================================================================================= 27 // Clock logic 28 //============================================================================================= 29 30 __cfa_time_t __kernel_get_time() { 31 timespec curr; 32 clock_gettime( CLOCK_REALTIME, &curr ); 33 return ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec; 24 34 } 25 35 26 void ?{}( alarm_node_t * this, thread_desc * thrd, cfa_time_t alarm, cfa_time_t period = 0 ) { 36 void __kernel_set_timer( __cfa_time_t alarm ) { 37 itimerval val; 38 val.it_value.tv_sec = alarm / TIMEGRAN; // seconds 39 val.it_value.tv_usec = (alarm % TIMEGRAN) / ( TIMEGRAN / 1_000_000L ); // microseconds 40 val.it_interval.tv_sec = 0; 41 val.it_interval.tv_usec = 0; 42 setitimer( ITIMER_REAL, &val, NULL ); 43 } 44 45 //============================================================================================= 46 // Alarm logic 47 //============================================================================================= 48 49 void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) { 27 50 this->thrd = thrd; 28 51 this->alarm = alarm; 29 52 this->period = period; 30 53 this->next = 0; 31 32 register_self( this );54 this->set = false; 55 this->kernel_alarm = false; 33 56 } 34 57 35 void ?{}( alarm_node_t * this, processor * proc, cfa_time_t alarm,cfa_time_t period = 0 ) {58 void ?{}( alarm_node_t * this, processor * proc, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) { 36 59 this->proc = proc; 37 60 this->alarm = alarm; 38 61 this->period = period; 39 62 this->next = 0; 63 this->set = false; 64 this->kernel_alarm = true; 65 } 40 66 41 register_self( this ); 67 void ^?{}( alarm_node_t * this ) { 68 if( this->set ) { 69 unregister_self( this ); 70 } 42 71 } 43 72 … … 62 91 } 63 92 64 voidpop( alarm_list_t * this ) {93 alarm_node_t * pop( alarm_list_t * this ) { 65 94 alarm_node_t * head = this->head; 66 95 if( head ) { … … 73 102 return head; 74 103 } 104 105 static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) { 106 assert( it ); 107 assert( (*it)->next == n ); 108 109 (*it)->next = n->next; 110 if( !n-> next ) { 111 this->tail = it; 112 } 113 n->next = NULL; 114 } 115 116 static inline void remove( alarm_list_t * this, alarm_node_t * n ) { 117 alarm_node_t ** it = &this->head; 118 while( (*it) && (*it)->next != n ) { 119 it = &(*it)->next; 120 } 121 122 if( *it ) { remove_at( this, n, it ); } 123 } 124 125 void register_self( alarm_node_t * this ) { 126 disable_interrupts(); 127 assert( !systemProcessor->pending_alarm ); 128 lock( &systemProcessor->alarm_lock ); 129 { 130 insert( &systemProcessor->alarms, this ); 131 if( systemProcessor->pending_alarm ) { 132 tick_preemption(); 133 } 134 } 135 unlock( &systemProcessor->alarm_lock ); 136 this->set = true; 137 enable_interrupts(); 138 } 139 140 void unregister_self( alarm_node_t * this ) { 141 disable_interrupts(); 142 lock( &systemProcessor->alarm_lock ); 143 remove( &systemProcessor->alarms, this ); 144 unlock( &systemProcessor->alarm_lock ); 145 disable_interrupts(); 146 this->set = false; 147 }
Note: See TracChangeset
for help on using the changeset viewer.