| [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
 | 
|---|
 | 12 | // Last Modified On : Sat Jul 22 09:58:39 2017
 | 
|---|
 | 13 | // Update Count     : 2
 | 
|---|
| [8118303] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
| [6b0b624] | 16 | #pragma once
 | 
|---|
| [8118303] | 17 | 
 | 
|---|
| [c84e80a] | 18 | #include <stdbool.h>
 | 
|---|
| [8118303] | 19 | 
 | 
|---|
| [bd98b58] | 20 | #include "invoke.h"
 | 
|---|
| [d8548e2] | 21 | #include "bits/cfatime.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 | 
 | 
|---|
 | 50 |         // Preemption rate on this cluster
 | 
|---|
| [d8548e2] | 51 |         __cfa_time_t preemption_rate;
 | 
|---|
| [bd98b58] | 52 | };
 | 
|---|
 | 53 | 
 | 
|---|
| [d8548e2] | 54 | extern __cfa_time_t default_preemption();
 | 
|---|
 | 55 | 
 | 
|---|
| [4cedd9f] | 56 | void ?{} (cluster & this);
 | 
|---|
| [242a902] | 57 | void ^?{}(cluster & this);
 | 
|---|
| [bd98b58] | 58 | 
 | 
|---|
 | 59 | //-----------------------------------------------------------------------------
 | 
|---|
 | 60 | // Processor
 | 
|---|
| [0c78741] | 61 | enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule };
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | //TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI)
 | 
|---|
| [db6f06a] | 64 | struct FinishAction {
 | 
|---|
 | 65 |         FinishOpCode action_code;
 | 
|---|
| [348006f] | 66 |         thread_desc * thrd;
 | 
|---|
| [ea7d2b0] | 67 |         __spinlock_t * lock;
 | 
|---|
 | 68 |         __spinlock_t ** locks;
 | 
|---|
| [0c78741] | 69 |         unsigned short lock_count;
 | 
|---|
 | 70 |         thread_desc ** thrds;
 | 
|---|
 | 71 |         unsigned short thrd_count;
 | 
|---|
| [8fcbb4c] | 72 | };
 | 
|---|
| [242a902] | 73 | static inline void ?{}(FinishAction & this) {
 | 
|---|
 | 74 |         this.action_code = No_Action;
 | 
|---|
 | 75 |         this.thrd = NULL;
 | 
|---|
 | 76 |         this.lock = NULL;
 | 
|---|
| [db6f06a] | 77 | }
 | 
|---|
| [242a902] | 78 | static inline void ^?{}(FinishAction & this) {}
 | 
|---|
| [8fcbb4c] | 79 | 
 | 
|---|
| [e60e0dc] | 80 | // Processor
 | 
|---|
| [094476d] | 81 | coroutine processorCtx_t {
 | 
|---|
 | 82 |         struct processor * proc;
 | 
|---|
 | 83 | };
 | 
|---|
 | 84 | 
 | 
|---|
| [e60e0dc] | 85 | // Wrapper around kernel threads
 | 
|---|
| [c84e80a] | 86 | struct processor {
 | 
|---|
| [e60e0dc] | 87 |         // Main state
 | 
|---|
| [025278e] | 88 |         // Coroutine ctx who does keeps the state of the processor
 | 
|---|
| [094476d] | 89 |         struct processorCtx_t runner;
 | 
|---|
| [025278e] | 90 | 
 | 
|---|
 | 91 |         // Cluster from which to get threads
 | 
|---|
 | 92 |         cluster * cltr;
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 |         // Handle to pthreads
 | 
|---|
 | 95 |         pthread_t kernel_thread;
 | 
|---|
| [2ac095d] | 96 | 
 | 
|---|
| [e60e0dc] | 97 |         // Termination
 | 
|---|
| [025278e] | 98 |         // Set to true to notify the processor should terminate
 | 
|---|
 | 99 |         volatile bool do_terminate;
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 |         // Termination synchronisation
 | 
|---|
 | 102 |         semaphore terminated;
 | 
|---|
| [db6f06a] | 103 | 
 | 
|---|
| [e60e0dc] | 104 |         // RunThread data
 | 
|---|
| [025278e] | 105 |         // Action to do after a thread is ran
 | 
|---|
 | 106 |         struct FinishAction finish;
 | 
|---|
| [c81ebf9] | 107 | 
 | 
|---|
| [e60e0dc] | 108 |         // Preemption data
 | 
|---|
| [025278e] | 109 |         // Node which is added in the discrete event simulaiton
 | 
|---|
 | 110 |         struct alarm_node_t * preemption_alarm;
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 |         // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
 | 
|---|
 | 113 |         bool pending_preemption;
 | 
|---|
| [c81ebf9] | 114 | 
 | 
|---|
| [e60e0dc] | 115 | #ifdef __CFA_DEBUG__
 | 
|---|
| [025278e] | 116 |         // Last function to enable preemption on this processor
 | 
|---|
| [cdbfab0] | 117 |         const char * last_enable;
 | 
|---|
| [e60e0dc] | 118 | #endif
 | 
|---|
| [c84e80a] | 119 | };
 | 
|---|
 | 120 | 
 | 
|---|
| [8fc45b7] | 121 | void  ?{}(processor & this);
 | 
|---|
 | 122 | void  ?{}(processor & this, cluster * cltr);
 | 
|---|
| [242a902] | 123 | void ^?{}(processor & this);
 | 
|---|
| [c84e80a] | 124 | 
 | 
|---|
| [8118303] | 125 | // Local Variables: //
 | 
|---|
| [6b0b624] | 126 | // mode: c //
 | 
|---|
 | 127 | // tab-width: 4 //
 | 
|---|
| [8118303] | 128 | // End: //
 | 
|---|