[8118303] | 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 | // |
---|
[75a17f1] | 7 | // kernel -- |
---|
[8118303] | 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
[75f3522] | 10 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
[0f56058] | 12 | // Last Modified On : Tue Apr 10 14:46:49 2018 |
---|
| 13 | // Update Count : 10 |
---|
[8118303] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[8118303] | 17 | |
---|
[c84e80a] | 18 | #include <stdbool.h> |
---|
[8118303] | 19 | |
---|
[bd98b58] | 20 | #include "invoke.h" |
---|
[0f56058] | 21 | #include "time_t.h" |
---|
[bd98b58] | 22 | |
---|
[8def349] | 23 | extern "C" { |
---|
| 24 | #include <pthread.h> |
---|
[6b4cdd3] | 25 | #include <semaphore.h> |
---|
[8def349] | 26 | } |
---|
| 27 | |
---|
[db6f06a] | 28 | //----------------------------------------------------------------------------- |
---|
| 29 | // Locks |
---|
[bdeba0b] | 30 | struct semaphore { |
---|
[ea7d2b0] | 31 | __spinlock_t lock; |
---|
[bdeba0b] | 32 | int count; |
---|
[0cf5b79] | 33 | __queue_t(thread_desc) waiting; |
---|
[9c31349] | 34 | }; |
---|
| 35 | |
---|
[242a902] | 36 | void ?{}(semaphore & this, int count = 1); |
---|
| 37 | void ^?{}(semaphore & this); |
---|
[4cedd9f] | 38 | void P (semaphore & this); |
---|
| 39 | void V (semaphore & this); |
---|
[9c31349] | 40 | |
---|
[db6f06a] | 41 | |
---|
[bd98b58] | 42 | //----------------------------------------------------------------------------- |
---|
[de94a60] | 43 | // Processor |
---|
[de6319f] | 44 | extern struct cluster * mainCluster; |
---|
[bd98b58] | 45 | |
---|
[09800e9] | 46 | enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule, Callback }; |
---|
| 47 | |
---|
| 48 | typedef void (*__finish_callback_fptr_t)(void); |
---|
[0c78741] | 49 | |
---|
| 50 | //TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI) |
---|
[db6f06a] | 51 | struct FinishAction { |
---|
| 52 | FinishOpCode action_code; |
---|
[09800e9] | 53 | /* |
---|
| 54 | // Union of possible actions |
---|
| 55 | union { |
---|
| 56 | // Option 1 : locks and threads |
---|
| 57 | struct { |
---|
| 58 | // 1 thread or N thread |
---|
| 59 | union { |
---|
| 60 | thread_desc * thrd; |
---|
| 61 | struct { |
---|
| 62 | thread_desc ** thrds; |
---|
| 63 | unsigned short thrd_count; |
---|
| 64 | }; |
---|
| 65 | }; |
---|
| 66 | // 1 lock or N lock |
---|
| 67 | union { |
---|
| 68 | __spinlock_t * lock; |
---|
| 69 | struct { |
---|
| 70 | __spinlock_t ** locks; |
---|
| 71 | unsigned short lock_count; |
---|
| 72 | }; |
---|
| 73 | }; |
---|
| 74 | }; |
---|
| 75 | // Option 2 : action pointer |
---|
| 76 | __finish_callback_fptr_t callback; |
---|
| 77 | }; |
---|
| 78 | /*/ |
---|
[348006f] | 79 | thread_desc * thrd; |
---|
[09800e9] | 80 | thread_desc ** thrds; |
---|
| 81 | unsigned short thrd_count; |
---|
[ea7d2b0] | 82 | __spinlock_t * lock; |
---|
| 83 | __spinlock_t ** locks; |
---|
[0c78741] | 84 | unsigned short lock_count; |
---|
[09800e9] | 85 | __finish_callback_fptr_t callback; |
---|
| 86 | //*/ |
---|
[8fcbb4c] | 87 | }; |
---|
[242a902] | 88 | static inline void ?{}(FinishAction & this) { |
---|
| 89 | this.action_code = No_Action; |
---|
| 90 | this.thrd = NULL; |
---|
| 91 | this.lock = NULL; |
---|
[db6f06a] | 92 | } |
---|
[242a902] | 93 | static inline void ^?{}(FinishAction & this) {} |
---|
[8fcbb4c] | 94 | |
---|
[e60e0dc] | 95 | // Processor |
---|
[094476d] | 96 | coroutine processorCtx_t { |
---|
| 97 | struct processor * proc; |
---|
| 98 | }; |
---|
| 99 | |
---|
[e60e0dc] | 100 | // Wrapper around kernel threads |
---|
[c84e80a] | 101 | struct processor { |
---|
[e60e0dc] | 102 | // Main state |
---|
[025278e] | 103 | // Coroutine ctx who does keeps the state of the processor |
---|
[094476d] | 104 | struct processorCtx_t runner; |
---|
[025278e] | 105 | |
---|
| 106 | // Cluster from which to get threads |
---|
[de94a60] | 107 | struct cluster * cltr; |
---|
[025278e] | 108 | |
---|
[de6319f] | 109 | // Name of the processor |
---|
| 110 | const char * name; |
---|
| 111 | |
---|
[025278e] | 112 | // Handle to pthreads |
---|
| 113 | pthread_t kernel_thread; |
---|
[2ac095d] | 114 | |
---|
[e60e0dc] | 115 | // Termination |
---|
[025278e] | 116 | // Set to true to notify the processor should terminate |
---|
| 117 | volatile bool do_terminate; |
---|
| 118 | |
---|
| 119 | // Termination synchronisation |
---|
| 120 | semaphore terminated; |
---|
[db6f06a] | 121 | |
---|
[e60e0dc] | 122 | // RunThread data |
---|
[025278e] | 123 | // Action to do after a thread is ran |
---|
| 124 | struct FinishAction finish; |
---|
[c81ebf9] | 125 | |
---|
[e60e0dc] | 126 | // Preemption data |
---|
[025278e] | 127 | // Node which is added in the discrete event simulaiton |
---|
| 128 | struct alarm_node_t * preemption_alarm; |
---|
| 129 | |
---|
| 130 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible |
---|
| 131 | bool pending_preemption; |
---|
[c81ebf9] | 132 | |
---|
[de94a60] | 133 | // Idle lock |
---|
[6b4cdd3] | 134 | sem_t idleLock; |
---|
[ea8b2f7] | 135 | // __bin_sem_t idleLock; |
---|
[de94a60] | 136 | |
---|
| 137 | // Link lists fields |
---|
[ea8b2f7] | 138 | struct __dbg_node_proc { |
---|
[de94a60] | 139 | struct processor * next; |
---|
| 140 | struct processor * prev; |
---|
| 141 | } node; |
---|
[14a61b5] | 142 | |
---|
[e60e0dc] | 143 | #ifdef __CFA_DEBUG__ |
---|
[025278e] | 144 | // Last function to enable preemption on this processor |
---|
[cdbfab0] | 145 | const char * last_enable; |
---|
[e60e0dc] | 146 | #endif |
---|
[c84e80a] | 147 | }; |
---|
| 148 | |
---|
[de94a60] | 149 | void ?{}(processor & this, const char * name, struct cluster & cltr); |
---|
[242a902] | 150 | void ^?{}(processor & this); |
---|
[c84e80a] | 151 | |
---|
[de6319f] | 152 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } |
---|
[de94a60] | 153 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } |
---|
[de6319f] | 154 | static inline void ?{}(processor & this, const char * name) { this{name, *mainCluster }; } |
---|
| 155 | |
---|
[de94a60] | 156 | static inline [processor *&, processor *& ] __get( processor & this ) { |
---|
| 157 | return this.node.[next, prev]; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | //----------------------------------------------------------------------------- |
---|
| 161 | // Cluster |
---|
| 162 | struct cluster { |
---|
| 163 | // Ready queue locks |
---|
| 164 | __spinlock_t ready_queue_lock; |
---|
| 165 | |
---|
| 166 | // Ready queue for threads |
---|
| 167 | __queue_t(thread_desc) ready_queue; |
---|
| 168 | |
---|
| 169 | // Name of the cluster |
---|
| 170 | const char * name; |
---|
| 171 | |
---|
| 172 | // Preemption rate on this cluster |
---|
| 173 | Duration preemption_rate; |
---|
| 174 | |
---|
| 175 | // List of processors |
---|
| 176 | __spinlock_t proc_list_lock; |
---|
| 177 | __dllist_t(struct processor) procs; |
---|
| 178 | __dllist_t(struct processor) idles; |
---|
| 179 | |
---|
[a1a17a74] | 180 | // List of processors |
---|
| 181 | __spinlock_t thread_list_lock; |
---|
| 182 | __dllist_t(struct thread_desc) threads; |
---|
| 183 | |
---|
[de94a60] | 184 | // Link lists fields |
---|
[ea8b2f7] | 185 | struct __dbg_node_cltr { |
---|
[de94a60] | 186 | cluster * next; |
---|
| 187 | cluster * prev; |
---|
| 188 | } node; |
---|
| 189 | }; |
---|
| 190 | extern Duration default_preemption(); |
---|
| 191 | |
---|
| 192 | void ?{} (cluster & this, const char * name, Duration preemption_rate); |
---|
| 193 | void ^?{}(cluster & this); |
---|
| 194 | |
---|
| 195 | static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption()}; } |
---|
| 196 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; } |
---|
| 197 | static inline void ?{} (cluster & this, const char * name) { this{name, default_preemption()}; } |
---|
| 198 | |
---|
| 199 | static inline [cluster *&, cluster *& ] __get( cluster & this ) { |
---|
| 200 | return this.node.[next, prev]; |
---|
| 201 | } |
---|
| 202 | |
---|
[8118303] | 203 | // Local Variables: // |
---|
[6b0b624] | 204 | // mode: c // |
---|
| 205 | // tab-width: 4 // |
---|
[8118303] | 206 | // End: // |
---|