[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> |
---|
| 25 | } |
---|
| 26 | |
---|
[db6f06a] | 27 | //----------------------------------------------------------------------------- |
---|
| 28 | // Locks |
---|
[bdeba0b] | 29 | struct semaphore { |
---|
[ea7d2b0] | 30 | __spinlock_t lock; |
---|
[bdeba0b] | 31 | int count; |
---|
[0cf5b79] | 32 | __queue_t(thread_desc) waiting; |
---|
[9c31349] | 33 | }; |
---|
| 34 | |
---|
[242a902] | 35 | void ?{}(semaphore & this, int count = 1); |
---|
| 36 | void ^?{}(semaphore & this); |
---|
[4cedd9f] | 37 | void P (semaphore & this); |
---|
| 38 | void V (semaphore & this); |
---|
[9c31349] | 39 | |
---|
[db6f06a] | 40 | |
---|
[bd98b58] | 41 | //----------------------------------------------------------------------------- |
---|
[de94a60] | 42 | // Processor |
---|
[de6319f] | 43 | extern struct cluster * mainCluster; |
---|
[bd98b58] | 44 | |
---|
[0c78741] | 45 | enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule }; |
---|
| 46 | |
---|
| 47 | //TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI) |
---|
[db6f06a] | 48 | struct FinishAction { |
---|
| 49 | FinishOpCode action_code; |
---|
[348006f] | 50 | thread_desc * thrd; |
---|
[ea7d2b0] | 51 | __spinlock_t * lock; |
---|
| 52 | __spinlock_t ** locks; |
---|
[0c78741] | 53 | unsigned short lock_count; |
---|
| 54 | thread_desc ** thrds; |
---|
| 55 | unsigned short thrd_count; |
---|
[8fcbb4c] | 56 | }; |
---|
[242a902] | 57 | static inline void ?{}(FinishAction & this) { |
---|
| 58 | this.action_code = No_Action; |
---|
| 59 | this.thrd = NULL; |
---|
| 60 | this.lock = NULL; |
---|
[db6f06a] | 61 | } |
---|
[242a902] | 62 | static inline void ^?{}(FinishAction & this) {} |
---|
[8fcbb4c] | 63 | |
---|
[e60e0dc] | 64 | // Processor |
---|
[094476d] | 65 | coroutine processorCtx_t { |
---|
| 66 | struct processor * proc; |
---|
| 67 | }; |
---|
| 68 | |
---|
[e60e0dc] | 69 | // Wrapper around kernel threads |
---|
[c84e80a] | 70 | struct processor { |
---|
[e60e0dc] | 71 | // Main state |
---|
[025278e] | 72 | // Coroutine ctx who does keeps the state of the processor |
---|
[094476d] | 73 | struct processorCtx_t runner; |
---|
[025278e] | 74 | |
---|
| 75 | // Cluster from which to get threads |
---|
[de94a60] | 76 | struct cluster * cltr; |
---|
[025278e] | 77 | |
---|
[de6319f] | 78 | // Name of the processor |
---|
| 79 | const char * name; |
---|
| 80 | |
---|
[025278e] | 81 | // Handle to pthreads |
---|
| 82 | pthread_t kernel_thread; |
---|
[2ac095d] | 83 | |
---|
[e60e0dc] | 84 | // Termination |
---|
[025278e] | 85 | // Set to true to notify the processor should terminate |
---|
| 86 | volatile bool do_terminate; |
---|
| 87 | |
---|
| 88 | // Termination synchronisation |
---|
| 89 | semaphore terminated; |
---|
[db6f06a] | 90 | |
---|
[e60e0dc] | 91 | // RunThread data |
---|
[025278e] | 92 | // Action to do after a thread is ran |
---|
| 93 | struct FinishAction finish; |
---|
[c81ebf9] | 94 | |
---|
[e60e0dc] | 95 | // Preemption data |
---|
[025278e] | 96 | // Node which is added in the discrete event simulaiton |
---|
| 97 | struct alarm_node_t * preemption_alarm; |
---|
| 98 | |
---|
| 99 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible |
---|
| 100 | bool pending_preemption; |
---|
[c81ebf9] | 101 | |
---|
[de94a60] | 102 | // Idle lock |
---|
| 103 | |
---|
| 104 | // Link lists fields |
---|
[14a61b5] | 105 | struct { |
---|
[de94a60] | 106 | struct processor * next; |
---|
| 107 | struct processor * prev; |
---|
| 108 | } node; |
---|
[14a61b5] | 109 | |
---|
[e60e0dc] | 110 | #ifdef __CFA_DEBUG__ |
---|
[025278e] | 111 | // Last function to enable preemption on this processor |
---|
[cdbfab0] | 112 | const char * last_enable; |
---|
[e60e0dc] | 113 | #endif |
---|
[c84e80a] | 114 | }; |
---|
| 115 | |
---|
[de94a60] | 116 | void ?{}(processor & this, const char * name, struct cluster & cltr); |
---|
[242a902] | 117 | void ^?{}(processor & this); |
---|
[c84e80a] | 118 | |
---|
[de6319f] | 119 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } |
---|
[de94a60] | 120 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } |
---|
[de6319f] | 121 | static inline void ?{}(processor & this, const char * name) { this{name, *mainCluster }; } |
---|
| 122 | |
---|
[de94a60] | 123 | static inline [processor *&, processor *& ] __get( processor & this ) { |
---|
| 124 | return this.node.[next, prev]; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | //----------------------------------------------------------------------------- |
---|
| 128 | // Cluster |
---|
| 129 | struct cluster { |
---|
| 130 | // Ready queue locks |
---|
| 131 | __spinlock_t ready_queue_lock; |
---|
| 132 | |
---|
| 133 | // Ready queue for threads |
---|
| 134 | __queue_t(thread_desc) ready_queue; |
---|
| 135 | |
---|
| 136 | // Name of the cluster |
---|
| 137 | const char * name; |
---|
| 138 | |
---|
| 139 | // Preemption rate on this cluster |
---|
| 140 | Duration preemption_rate; |
---|
| 141 | |
---|
| 142 | // List of processors |
---|
| 143 | __spinlock_t proc_list_lock; |
---|
| 144 | __dllist_t(struct processor) procs; |
---|
| 145 | __dllist_t(struct processor) idles; |
---|
| 146 | |
---|
[a1a17a74] | 147 | // List of processors |
---|
| 148 | __spinlock_t thread_list_lock; |
---|
| 149 | __dllist_t(struct thread_desc) threads; |
---|
| 150 | |
---|
[de94a60] | 151 | // Link lists fields |
---|
| 152 | struct { |
---|
| 153 | cluster * next; |
---|
| 154 | cluster * prev; |
---|
| 155 | } node; |
---|
| 156 | }; |
---|
| 157 | extern Duration default_preemption(); |
---|
| 158 | |
---|
| 159 | void ?{} (cluster & this, const char * name, Duration preemption_rate); |
---|
| 160 | void ^?{}(cluster & this); |
---|
| 161 | |
---|
| 162 | static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption()}; } |
---|
| 163 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; } |
---|
| 164 | static inline void ?{} (cluster & this, const char * name) { this{name, default_preemption()}; } |
---|
| 165 | |
---|
| 166 | static inline [cluster *&, cluster *& ] __get( cluster & this ) { |
---|
| 167 | return this.node.[next, prev]; |
---|
| 168 | } |
---|
| 169 | |
---|
[8118303] | 170 | // Local Variables: // |
---|
[6b0b624] | 171 | // mode: c // |
---|
| 172 | // tab-width: 4 // |
---|
[8118303] | 173 | // End: // |
---|