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