[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 |
---|
[e3fea42] | 12 | // Last Modified On : Tue Feb 4 12:29:26 2020 |
---|
| 13 | // Update Count : 22 |
---|
[8118303] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[8118303] | 17 | |
---|
[c84e80a] | 18 | #include <stdbool.h> |
---|
[92976d9] | 19 | #include <stdint.h> |
---|
[8118303] | 20 | |
---|
[bd98b58] | 21 | #include "invoke.h" |
---|
[73abe95] | 22 | #include "time_t.hfa" |
---|
[d76bd79] | 23 | #include "coroutine.hfa" |
---|
[bd98b58] | 24 | |
---|
[8def349] | 25 | extern "C" { |
---|
| 26 | #include <pthread.h> |
---|
[6b4cdd3] | 27 | #include <semaphore.h> |
---|
[8def349] | 28 | } |
---|
| 29 | |
---|
[db6f06a] | 30 | //----------------------------------------------------------------------------- |
---|
| 31 | // Locks |
---|
[bdeba0b] | 32 | struct semaphore { |
---|
[ea7d2b0] | 33 | __spinlock_t lock; |
---|
[bdeba0b] | 34 | int count; |
---|
[ac2b598] | 35 | __queue_t($thread) waiting; |
---|
[9c31349] | 36 | }; |
---|
| 37 | |
---|
[242a902] | 38 | void ?{}(semaphore & this, int count = 1); |
---|
| 39 | void ^?{}(semaphore & this); |
---|
[4cedd9f] | 40 | void P (semaphore & this); |
---|
[f0ce5f4] | 41 | bool V (semaphore & this); |
---|
[9c31349] | 42 | |
---|
[db6f06a] | 43 | |
---|
[bd98b58] | 44 | //----------------------------------------------------------------------------- |
---|
[de94a60] | 45 | // Processor |
---|
[de6319f] | 46 | extern struct cluster * mainCluster; |
---|
[bd98b58] | 47 | |
---|
[e60e0dc] | 48 | // Processor |
---|
[094476d] | 49 | coroutine processorCtx_t { |
---|
| 50 | struct processor * proc; |
---|
| 51 | }; |
---|
| 52 | |
---|
[e60e0dc] | 53 | // Wrapper around kernel threads |
---|
[c84e80a] | 54 | struct processor { |
---|
[e60e0dc] | 55 | // Main state |
---|
[025278e] | 56 | // Coroutine ctx who does keeps the state of the processor |
---|
[094476d] | 57 | struct processorCtx_t runner; |
---|
[025278e] | 58 | |
---|
| 59 | // Cluster from which to get threads |
---|
[de94a60] | 60 | struct cluster * cltr; |
---|
[025278e] | 61 | |
---|
[de6319f] | 62 | // Name of the processor |
---|
| 63 | const char * name; |
---|
| 64 | |
---|
[025278e] | 65 | // Handle to pthreads |
---|
| 66 | pthread_t kernel_thread; |
---|
[2ac095d] | 67 | |
---|
[e60e0dc] | 68 | // RunThread data |
---|
[025278e] | 69 | // Action to do after a thread is ran |
---|
[ac2b598] | 70 | $thread * destroyer; |
---|
[c81ebf9] | 71 | |
---|
[e60e0dc] | 72 | // Preemption data |
---|
[025278e] | 73 | // Node which is added in the discrete event simulaiton |
---|
| 74 | struct alarm_node_t * preemption_alarm; |
---|
| 75 | |
---|
| 76 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible |
---|
| 77 | bool pending_preemption; |
---|
[c81ebf9] | 78 | |
---|
[92e7631] | 79 | // Idle lock (kernel semaphore) |
---|
| 80 | __bin_sem_t idle; |
---|
[85b1deb] | 81 | |
---|
| 82 | // Termination |
---|
| 83 | // Set to true to notify the processor should terminate |
---|
| 84 | volatile bool do_terminate; |
---|
| 85 | |
---|
[92e7631] | 86 | // Termination synchronisation (user semaphore) |
---|
[85b1deb] | 87 | semaphore terminated; |
---|
[de94a60] | 88 | |
---|
[27f5f71] | 89 | // pthread Stack |
---|
| 90 | void * stack; |
---|
| 91 | |
---|
[de94a60] | 92 | // Link lists fields |
---|
[ea8b2f7] | 93 | struct __dbg_node_proc { |
---|
[de94a60] | 94 | struct processor * next; |
---|
| 95 | struct processor * prev; |
---|
| 96 | } node; |
---|
[14a61b5] | 97 | |
---|
[e60e0dc] | 98 | #ifdef __CFA_DEBUG__ |
---|
[025278e] | 99 | // Last function to enable preemption on this processor |
---|
[cdbfab0] | 100 | const char * last_enable; |
---|
[e60e0dc] | 101 | #endif |
---|
[c84e80a] | 102 | }; |
---|
| 103 | |
---|
[e3fea42] | 104 | void ?{}(processor & this, const char name[], struct cluster & cltr); |
---|
[242a902] | 105 | void ^?{}(processor & this); |
---|
[c84e80a] | 106 | |
---|
[de6319f] | 107 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } |
---|
[de94a60] | 108 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } |
---|
[e3fea42] | 109 | static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; } |
---|
[de6319f] | 110 | |
---|
[c7a900a] | 111 | static inline [processor *&, processor *& ] __get( processor & this ) __attribute__((const)) { return this.node.[next, prev]; } |
---|
[de94a60] | 112 | |
---|
[92976d9] | 113 | //----------------------------------------------------------------------------- |
---|
| 114 | // I/O |
---|
| 115 | #if defined(HAVE_LINUX_IO_URING_H) |
---|
| 116 | struct io_uring_sq { |
---|
| 117 | uint32_t * head; |
---|
| 118 | uint32_t * tail; |
---|
| 119 | uint32_t * mask; |
---|
| 120 | uint32_t * entries; |
---|
| 121 | uint32_t * flags; |
---|
| 122 | uint32_t * dropped; |
---|
| 123 | uint32_t * array; |
---|
| 124 | struct io_uring_sqe * sqes; |
---|
| 125 | |
---|
| 126 | uint32_t sqe_head; |
---|
| 127 | uint32_t sqe_tail; |
---|
| 128 | |
---|
| 129 | size_t ring_sz; |
---|
| 130 | void * ring_ptr; |
---|
| 131 | }; |
---|
| 132 | |
---|
| 133 | struct io_uring_cq { |
---|
| 134 | volatile uint32_t * head; |
---|
| 135 | volatile uint32_t * tail; |
---|
| 136 | uint32_t * mask; |
---|
| 137 | struct io_uring_cqe * entries; |
---|
| 138 | uint32_t * overflow; |
---|
| 139 | struct io_uring_cqe * cqes; |
---|
| 140 | |
---|
| 141 | size_t ring_sz; |
---|
| 142 | void * ring_ptr; |
---|
| 143 | }; |
---|
| 144 | |
---|
| 145 | struct io_ring { |
---|
| 146 | struct io_uring_sq submit_q; |
---|
| 147 | struct io_uring_cq completion_q; |
---|
| 148 | uint32_t flags; |
---|
| 149 | int fd; |
---|
| 150 | pthread_t poller; |
---|
| 151 | void * stack; |
---|
| 152 | volatile bool done; |
---|
| 153 | }; |
---|
| 154 | #endif |
---|
| 155 | |
---|
[de94a60] | 156 | //----------------------------------------------------------------------------- |
---|
| 157 | // Cluster |
---|
| 158 | struct cluster { |
---|
| 159 | // Ready queue locks |
---|
| 160 | __spinlock_t ready_queue_lock; |
---|
| 161 | |
---|
| 162 | // Ready queue for threads |
---|
[ac2b598] | 163 | __queue_t($thread) ready_queue; |
---|
[de94a60] | 164 | |
---|
| 165 | // Name of the cluster |
---|
| 166 | const char * name; |
---|
| 167 | |
---|
| 168 | // Preemption rate on this cluster |
---|
| 169 | Duration preemption_rate; |
---|
| 170 | |
---|
| 171 | // List of processors |
---|
[92e7631] | 172 | __spinlock_t idle_lock; |
---|
[de94a60] | 173 | __dllist_t(struct processor) procs; |
---|
| 174 | __dllist_t(struct processor) idles; |
---|
[d4e68a6] | 175 | unsigned int nprocessors; |
---|
[de94a60] | 176 | |
---|
[d4e68a6] | 177 | // List of threads |
---|
[a1a17a74] | 178 | __spinlock_t thread_list_lock; |
---|
[ac2b598] | 179 | __dllist_t(struct $thread) threads; |
---|
[d4e68a6] | 180 | unsigned int nthreads; |
---|
[a1a17a74] | 181 | |
---|
[de94a60] | 182 | // Link lists fields |
---|
[ea8b2f7] | 183 | struct __dbg_node_cltr { |
---|
[de94a60] | 184 | cluster * next; |
---|
| 185 | cluster * prev; |
---|
| 186 | } node; |
---|
[92976d9] | 187 | |
---|
| 188 | #if defined(HAVE_LINUX_IO_URING_H) |
---|
| 189 | struct io_ring io; |
---|
| 190 | #endif |
---|
[de94a60] | 191 | }; |
---|
| 192 | extern Duration default_preemption(); |
---|
| 193 | |
---|
[e3fea42] | 194 | void ?{} (cluster & this, const char name[], Duration preemption_rate); |
---|
[de94a60] | 195 | void ^?{}(cluster & this); |
---|
| 196 | |
---|
| 197 | static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption()}; } |
---|
| 198 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; } |
---|
[e3fea42] | 199 | static inline void ?{} (cluster & this, const char name[]) { this{name, default_preemption()}; } |
---|
[de94a60] | 200 | |
---|
[c7a900a] | 201 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } |
---|
[de94a60] | 202 | |
---|
[0f2c555] | 203 | static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE |
---|
| 204 | static inline struct cluster * active_cluster () { return TL_GET( this_processor )->cltr; } |
---|
[d4e68a6] | 205 | |
---|
[8118303] | 206 | // Local Variables: // |
---|
[6b0b624] | 207 | // mode: c // |
---|
| 208 | // tab-width: 4 // |
---|
[8118303] | 209 | // End: // |
---|