| [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> | 
|---|
| [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; | 
|---|
| [ac2b598] | 34 | __queue_t($thread) waiting; | 
|---|
| [9c31349] | 35 | }; | 
|---|
|  | 36 |  | 
|---|
| [242a902] | 37 | void  ?{}(semaphore & this, int count = 1); | 
|---|
|  | 38 | void ^?{}(semaphore & this); | 
|---|
| [4cedd9f] | 39 | void   P (semaphore & this); | 
|---|
| [f0ce5f4] | 40 | bool   V (semaphore & this); | 
|---|
| [9c31349] | 41 |  | 
|---|
| [db6f06a] | 42 |  | 
|---|
| [bd98b58] | 43 | //----------------------------------------------------------------------------- | 
|---|
| [de94a60] | 44 | // Processor | 
|---|
| [de6319f] | 45 | extern struct cluster * mainCluster; | 
|---|
| [bd98b58] | 46 |  | 
|---|
| [e60e0dc] | 47 | // Processor | 
|---|
| [094476d] | 48 | coroutine processorCtx_t { | 
|---|
|  | 49 | struct processor * proc; | 
|---|
|  | 50 | }; | 
|---|
|  | 51 |  | 
|---|
| [e60e0dc] | 52 | // Wrapper around kernel threads | 
|---|
| [c84e80a] | 53 | struct processor { | 
|---|
| [e60e0dc] | 54 | // Main state | 
|---|
| [025278e] | 55 | // Coroutine ctx who does keeps the state of the processor | 
|---|
| [094476d] | 56 | struct processorCtx_t runner; | 
|---|
| [025278e] | 57 |  | 
|---|
|  | 58 | // Cluster from which to get threads | 
|---|
| [de94a60] | 59 | struct cluster * cltr; | 
|---|
| [025278e] | 60 |  | 
|---|
| [de6319f] | 61 | // Name of the processor | 
|---|
|  | 62 | const char * name; | 
|---|
|  | 63 |  | 
|---|
| [025278e] | 64 | // Handle to pthreads | 
|---|
|  | 65 | pthread_t kernel_thread; | 
|---|
| [2ac095d] | 66 |  | 
|---|
| [e60e0dc] | 67 | // RunThread data | 
|---|
| [025278e] | 68 | // Action to do after a thread is ran | 
|---|
| [ac2b598] | 69 | $thread * destroyer; | 
|---|
| [c81ebf9] | 70 |  | 
|---|
| [e60e0dc] | 71 | // Preemption data | 
|---|
| [025278e] | 72 | // Node which is added in the discrete event simulaiton | 
|---|
|  | 73 | struct alarm_node_t * preemption_alarm; | 
|---|
|  | 74 |  | 
|---|
|  | 75 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible | 
|---|
|  | 76 | bool pending_preemption; | 
|---|
| [c81ebf9] | 77 |  | 
|---|
| [92e7631] | 78 | // Idle lock (kernel semaphore) | 
|---|
|  | 79 | __bin_sem_t idle; | 
|---|
| [85b1deb] | 80 |  | 
|---|
|  | 81 | // Termination | 
|---|
|  | 82 | // Set to true to notify the processor should terminate | 
|---|
|  | 83 | volatile bool do_terminate; | 
|---|
|  | 84 |  | 
|---|
| [92e7631] | 85 | // Termination synchronisation (user semaphore) | 
|---|
| [85b1deb] | 86 | semaphore terminated; | 
|---|
| [de94a60] | 87 |  | 
|---|
| [27f5f71] | 88 | // pthread Stack | 
|---|
|  | 89 | void * stack; | 
|---|
|  | 90 |  | 
|---|
| [de94a60] | 91 | // Link lists fields | 
|---|
| [ea8b2f7] | 92 | struct __dbg_node_proc { | 
|---|
| [de94a60] | 93 | struct processor * next; | 
|---|
|  | 94 | struct processor * prev; | 
|---|
|  | 95 | } node; | 
|---|
| [14a61b5] | 96 |  | 
|---|
| [e60e0dc] | 97 | #ifdef __CFA_DEBUG__ | 
|---|
| [025278e] | 98 | // Last function to enable preemption on this processor | 
|---|
| [cdbfab0] | 99 | const char * last_enable; | 
|---|
| [e60e0dc] | 100 | #endif | 
|---|
| [c84e80a] | 101 | }; | 
|---|
|  | 102 |  | 
|---|
| [e3fea42] | 103 | void  ?{}(processor & this, const char name[], struct cluster & cltr); | 
|---|
| [242a902] | 104 | void ^?{}(processor & this); | 
|---|
| [c84e80a] | 105 |  | 
|---|
| [de6319f] | 106 | static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; } | 
|---|
| [de94a60] | 107 | static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; } | 
|---|
| [e3fea42] | 108 | static inline void  ?{}(processor & this, const char name[]) { this{name, *mainCluster }; } | 
|---|
| [de6319f] | 109 |  | 
|---|
| [c7a900a] | 110 | static inline [processor *&, processor *& ] __get( processor & this ) __attribute__((const)) { return this.node.[next, prev]; } | 
|---|
| [de94a60] | 111 |  | 
|---|
|  | 112 | //----------------------------------------------------------------------------- | 
|---|
|  | 113 | // Cluster | 
|---|
|  | 114 | struct cluster { | 
|---|
|  | 115 | // Ready queue locks | 
|---|
|  | 116 | __spinlock_t ready_queue_lock; | 
|---|
|  | 117 |  | 
|---|
|  | 118 | // Ready queue for threads | 
|---|
| [ac2b598] | 119 | __queue_t($thread) ready_queue; | 
|---|
| [de94a60] | 120 |  | 
|---|
|  | 121 | // Name of the cluster | 
|---|
|  | 122 | const char * name; | 
|---|
|  | 123 |  | 
|---|
|  | 124 | // Preemption rate on this cluster | 
|---|
|  | 125 | Duration preemption_rate; | 
|---|
|  | 126 |  | 
|---|
|  | 127 | // List of processors | 
|---|
| [92e7631] | 128 | __spinlock_t idle_lock; | 
|---|
| [de94a60] | 129 | __dllist_t(struct processor) procs; | 
|---|
|  | 130 | __dllist_t(struct processor) idles; | 
|---|
| [d4e68a6] | 131 | unsigned int nprocessors; | 
|---|
| [de94a60] | 132 |  | 
|---|
| [d4e68a6] | 133 | // List of threads | 
|---|
| [a1a17a74] | 134 | __spinlock_t thread_list_lock; | 
|---|
| [ac2b598] | 135 | __dllist_t(struct $thread) threads; | 
|---|
| [d4e68a6] | 136 | unsigned int nthreads; | 
|---|
| [a1a17a74] | 137 |  | 
|---|
| [de94a60] | 138 | // Link lists fields | 
|---|
| [ea8b2f7] | 139 | struct __dbg_node_cltr { | 
|---|
| [de94a60] | 140 | cluster * next; | 
|---|
|  | 141 | cluster * prev; | 
|---|
|  | 142 | } node; | 
|---|
|  | 143 | }; | 
|---|
|  | 144 | extern Duration default_preemption(); | 
|---|
|  | 145 |  | 
|---|
| [e3fea42] | 146 | void ?{} (cluster & this, const char name[], Duration preemption_rate); | 
|---|
| [de94a60] | 147 | void ^?{}(cluster & this); | 
|---|
|  | 148 |  | 
|---|
|  | 149 | static inline void ?{} (cluster & this)                           { this{"Anonymous Cluster", default_preemption()}; } | 
|---|
|  | 150 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; } | 
|---|
| [e3fea42] | 151 | static inline void ?{} (cluster & this, const char name[])        { this{name, default_preemption()}; } | 
|---|
| [de94a60] | 152 |  | 
|---|
| [c7a900a] | 153 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } | 
|---|
| [de94a60] | 154 |  | 
|---|
| [0f2c555] | 155 | static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE | 
|---|
|  | 156 | static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; } | 
|---|
| [d4e68a6] | 157 |  | 
|---|
| [8118303] | 158 | // Local Variables: // | 
|---|
| [6b0b624] | 159 | // mode: c // | 
|---|
|  | 160 | // tab-width: 4 // | 
|---|
| [8118303] | 161 | // End: // | 
|---|