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