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