| 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 | //
|
|---|
| 7 | // kernel --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Thierry Delisle
|
|---|
| 10 | // Created On : Tue Jan 17 12:27:26 2017
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Tue Feb 4 12:29:26 2020
|
|---|
| 13 | // Update Count : 22
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #pragma once
|
|---|
| 17 |
|
|---|
| 18 | #include <stdbool.h>
|
|---|
| 19 |
|
|---|
| 20 | #include "invoke.h"
|
|---|
| 21 | #include "time_t.hfa"
|
|---|
| 22 | #include "coroutine.hfa"
|
|---|
| 23 |
|
|---|
| 24 | extern "C" {
|
|---|
| 25 | #include <pthread.h>
|
|---|
| 26 | #include <semaphore.h>
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | //-----------------------------------------------------------------------------
|
|---|
| 30 | // Locks
|
|---|
| 31 | struct semaphore {
|
|---|
| 32 | __spinlock_t lock;
|
|---|
| 33 | int count;
|
|---|
| 34 | __queue_t(thread_desc) waiting;
|
|---|
| 35 | };
|
|---|
| 36 |
|
|---|
| 37 | void ?{}(semaphore & this, int count = 1);
|
|---|
| 38 | void ^?{}(semaphore & this);
|
|---|
| 39 | void P (semaphore & this);
|
|---|
| 40 | void V (semaphore & this);
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | //-----------------------------------------------------------------------------
|
|---|
| 44 | // Processor
|
|---|
| 45 | extern struct cluster * mainCluster;
|
|---|
| 46 |
|
|---|
| 47 | enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule, Callback };
|
|---|
| 48 |
|
|---|
| 49 | typedef void (*__finish_callback_fptr_t)(void);
|
|---|
| 50 |
|
|---|
| 51 | //TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI)
|
|---|
| 52 | struct FinishAction {
|
|---|
| 53 | FinishOpCode action_code;
|
|---|
| 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 | /*/
|
|---|
| 80 | thread_desc * thrd;
|
|---|
| 81 | thread_desc ** thrds;
|
|---|
| 82 | unsigned short thrd_count;
|
|---|
| 83 | __spinlock_t * lock;
|
|---|
| 84 | __spinlock_t ** locks;
|
|---|
| 85 | unsigned short lock_count;
|
|---|
| 86 | __finish_callback_fptr_t callback;
|
|---|
| 87 | //*/
|
|---|
| 88 | };
|
|---|
| 89 | static inline void ?{}(FinishAction & this) {
|
|---|
| 90 | this.action_code = No_Action;
|
|---|
| 91 | this.thrd = 0p;
|
|---|
| 92 | this.lock = 0p;
|
|---|
| 93 | }
|
|---|
| 94 | static inline void ^?{}(FinishAction &) {}
|
|---|
| 95 |
|
|---|
| 96 | // Processor
|
|---|
| 97 | coroutine processorCtx_t {
|
|---|
| 98 | struct processor * proc;
|
|---|
| 99 | };
|
|---|
| 100 |
|
|---|
| 101 | // Wrapper around kernel threads
|
|---|
| 102 | struct processor {
|
|---|
| 103 | // Main state
|
|---|
| 104 | // Coroutine ctx who does keeps the state of the processor
|
|---|
| 105 | struct processorCtx_t runner;
|
|---|
| 106 |
|
|---|
| 107 | // Cluster from which to get threads
|
|---|
| 108 | struct cluster * cltr;
|
|---|
| 109 |
|
|---|
| 110 | // Name of the processor
|
|---|
| 111 | const char * name;
|
|---|
| 112 |
|
|---|
| 113 | // Handle to pthreads
|
|---|
| 114 | pthread_t kernel_thread;
|
|---|
| 115 |
|
|---|
| 116 | // RunThread data
|
|---|
| 117 | // Action to do after a thread is ran
|
|---|
| 118 | struct FinishAction finish;
|
|---|
| 119 |
|
|---|
| 120 | // Preemption data
|
|---|
| 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;
|
|---|
| 126 |
|
|---|
| 127 | // Idle lock
|
|---|
| 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;
|
|---|
| 136 |
|
|---|
| 137 | // pthread Stack
|
|---|
| 138 | void * stack;
|
|---|
| 139 |
|
|---|
| 140 | // Link lists fields
|
|---|
| 141 | struct __dbg_node_proc {
|
|---|
| 142 | struct processor * next;
|
|---|
| 143 | struct processor * prev;
|
|---|
| 144 | } node;
|
|---|
| 145 |
|
|---|
| 146 | #ifdef __CFA_DEBUG__
|
|---|
| 147 | // Last function to enable preemption on this processor
|
|---|
| 148 | const char * last_enable;
|
|---|
| 149 | #endif
|
|---|
| 150 | };
|
|---|
| 151 |
|
|---|
| 152 | void ?{}(processor & this, const char name[], struct cluster & cltr);
|
|---|
| 153 | void ^?{}(processor & this);
|
|---|
| 154 |
|
|---|
| 155 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
|
|---|
| 156 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
|
|---|
| 157 | static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
|
|---|
| 158 |
|
|---|
| 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;
|
|---|
| 182 | unsigned int nprocessors;
|
|---|
| 183 |
|
|---|
| 184 | // List of threads
|
|---|
| 185 | __spinlock_t thread_list_lock;
|
|---|
| 186 | __dllist_t(struct thread_desc) threads;
|
|---|
| 187 | unsigned int nthreads;
|
|---|
| 188 |
|
|---|
| 189 | // Link lists fields
|
|---|
| 190 | struct __dbg_node_cltr {
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 211 | // Local Variables: //
|
|---|
| 212 | // mode: c //
|
|---|
| 213 | // tab-width: 4 //
|
|---|
| 214 | // End: //
|
|---|