| 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 Apr 10 14:46:49 2018
|
|---|
| 13 | // Update Count : 10
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #pragma once
|
|---|
| 17 |
|
|---|
| 18 | #include <stdbool.h>
|
|---|
| 19 |
|
|---|
| 20 | #include "invoke.h"
|
|---|
| 21 | #include "time_t.h"
|
|---|
| 22 |
|
|---|
| 23 | extern "C" {
|
|---|
| 24 | #include <pthread.h>
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | //-----------------------------------------------------------------------------
|
|---|
| 28 | // Locks
|
|---|
| 29 | struct semaphore {
|
|---|
| 30 | __spinlock_t lock;
|
|---|
| 31 | int count;
|
|---|
| 32 | __queue_t(thread_desc) waiting;
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | void ?{}(semaphore & this, int count = 1);
|
|---|
| 36 | void ^?{}(semaphore & this);
|
|---|
| 37 | void P (semaphore & this);
|
|---|
| 38 | void V (semaphore & this);
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | //-----------------------------------------------------------------------------
|
|---|
| 42 | // Cluster
|
|---|
| 43 | struct cluster {
|
|---|
| 44 | // Ready queue locks
|
|---|
| 45 | __spinlock_t ready_queue_lock;
|
|---|
| 46 |
|
|---|
| 47 | // Ready queue for threads
|
|---|
| 48 | __queue_t(thread_desc) ready_queue;
|
|---|
| 49 |
|
|---|
| 50 | // Name of the cluster
|
|---|
| 51 | const char * name;
|
|---|
| 52 |
|
|---|
| 53 | // Preemption rate on this cluster
|
|---|
| 54 | Duration preemption_rate;
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | extern struct cluster * mainCluster;
|
|---|
| 58 | extern Duration default_preemption();
|
|---|
| 59 |
|
|---|
| 60 | void ?{} (cluster & this, const char * name, Duration preemption_rate);
|
|---|
| 61 | void ^?{}(cluster & this);
|
|---|
| 62 |
|
|---|
| 63 | static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption()}; }
|
|---|
| 64 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; }
|
|---|
| 65 | static inline void ?{} (cluster & this, const char * name) { this{name, default_preemption()}; }
|
|---|
| 66 |
|
|---|
| 67 | //-----------------------------------------------------------------------------
|
|---|
| 68 | // Processor
|
|---|
| 69 | enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule };
|
|---|
| 70 |
|
|---|
| 71 | //TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI)
|
|---|
| 72 | struct FinishAction {
|
|---|
| 73 | FinishOpCode action_code;
|
|---|
| 74 | thread_desc * thrd;
|
|---|
| 75 | __spinlock_t * lock;
|
|---|
| 76 | __spinlock_t ** locks;
|
|---|
| 77 | unsigned short lock_count;
|
|---|
| 78 | thread_desc ** thrds;
|
|---|
| 79 | unsigned short thrd_count;
|
|---|
| 80 | };
|
|---|
| 81 | static inline void ?{}(FinishAction & this) {
|
|---|
| 82 | this.action_code = No_Action;
|
|---|
| 83 | this.thrd = NULL;
|
|---|
| 84 | this.lock = NULL;
|
|---|
| 85 | }
|
|---|
| 86 | static inline void ^?{}(FinishAction & this) {}
|
|---|
| 87 |
|
|---|
| 88 | // Processor
|
|---|
| 89 | coroutine processorCtx_t {
|
|---|
| 90 | struct processor * proc;
|
|---|
| 91 | };
|
|---|
| 92 |
|
|---|
| 93 | // Wrapper around kernel threads
|
|---|
| 94 | struct processor {
|
|---|
| 95 | // Main state
|
|---|
| 96 | // Coroutine ctx who does keeps the state of the processor
|
|---|
| 97 | struct processorCtx_t runner;
|
|---|
| 98 |
|
|---|
| 99 | // Cluster from which to get threads
|
|---|
| 100 | cluster * cltr;
|
|---|
| 101 |
|
|---|
| 102 | // Name of the processor
|
|---|
| 103 | const char * name;
|
|---|
| 104 |
|
|---|
| 105 | // Handle to pthreads
|
|---|
| 106 | pthread_t kernel_thread;
|
|---|
| 107 |
|
|---|
| 108 | // Termination
|
|---|
| 109 | // Set to true to notify the processor should terminate
|
|---|
| 110 | volatile bool do_terminate;
|
|---|
| 111 |
|
|---|
| 112 | // Termination synchronisation
|
|---|
| 113 | semaphore terminated;
|
|---|
| 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 | #ifdef __CFA_DEBUG__
|
|---|
| 127 | // Last function to enable preemption on this processor
|
|---|
| 128 | const char * last_enable;
|
|---|
| 129 | #endif
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | void ?{}(processor & this, const char * name, cluster & cltr);
|
|---|
| 133 | void ^?{}(processor & this);
|
|---|
| 134 |
|
|---|
| 135 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
|
|---|
| 136 | static inline void ?{}(processor & this, cluster & cltr) { this{ "Anonymous Processor", cltr}; }
|
|---|
| 137 | static inline void ?{}(processor & this, const char * name) { this{name, *mainCluster }; }
|
|---|
| 138 |
|
|---|
| 139 | // Local Variables: //
|
|---|
| 140 | // mode: c //
|
|---|
| 141 | // tab-width: 4 //
|
|---|
| 142 | // End: //
|
|---|