| 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 Jul 22 09:58:39 2017
|
|---|
| 13 | // Update Count : 2
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #pragma once
|
|---|
| 17 |
|
|---|
| 18 | #include <stdbool.h>
|
|---|
| 19 |
|
|---|
| 20 | #include "invoke.h"
|
|---|
| 21 | #include "bits/cfatime.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 | // Preemption rate on this cluster
|
|---|
| 51 | __cfa_time_t preemption_rate;
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | extern __cfa_time_t default_preemption();
|
|---|
| 55 |
|
|---|
| 56 | void ?{} (cluster & this);
|
|---|
| 57 | void ^?{}(cluster & this);
|
|---|
| 58 |
|
|---|
| 59 | //-----------------------------------------------------------------------------
|
|---|
| 60 | // Processor
|
|---|
| 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)
|
|---|
| 64 | struct FinishAction {
|
|---|
| 65 | FinishOpCode action_code;
|
|---|
| 66 | thread_desc * thrd;
|
|---|
| 67 | __spinlock_t * lock;
|
|---|
| 68 | __spinlock_t ** locks;
|
|---|
| 69 | unsigned short lock_count;
|
|---|
| 70 | thread_desc ** thrds;
|
|---|
| 71 | unsigned short thrd_count;
|
|---|
| 72 | };
|
|---|
| 73 | static inline void ?{}(FinishAction & this) {
|
|---|
| 74 | this.action_code = No_Action;
|
|---|
| 75 | this.thrd = NULL;
|
|---|
| 76 | this.lock = NULL;
|
|---|
| 77 | }
|
|---|
| 78 | static inline void ^?{}(FinishAction & this) {}
|
|---|
| 79 |
|
|---|
| 80 | // Processor
|
|---|
| 81 | // Wrapper around kernel threads
|
|---|
| 82 | struct processor {
|
|---|
| 83 | // Main state
|
|---|
| 84 | // Coroutine ctx who does keeps the state of the processor
|
|---|
| 85 | struct processorCtx_t * runner;
|
|---|
| 86 |
|
|---|
| 87 | // Cluster from which to get threads
|
|---|
| 88 | cluster * cltr;
|
|---|
| 89 |
|
|---|
| 90 | // Handle to pthreads
|
|---|
| 91 | pthread_t kernel_thread;
|
|---|
| 92 |
|
|---|
| 93 | // Termination
|
|---|
| 94 | // Set to true to notify the processor should terminate
|
|---|
| 95 | volatile bool do_terminate;
|
|---|
| 96 |
|
|---|
| 97 | // Termination synchronisation
|
|---|
| 98 | semaphore terminated;
|
|---|
| 99 |
|
|---|
| 100 | // RunThread data
|
|---|
| 101 | // Action to do after a thread is ran
|
|---|
| 102 | struct FinishAction finish;
|
|---|
| 103 |
|
|---|
| 104 | // Preemption data
|
|---|
| 105 | // Node which is added in the discrete event simulaiton
|
|---|
| 106 | struct alarm_node_t * preemption_alarm;
|
|---|
| 107 |
|
|---|
| 108 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
|
|---|
| 109 | bool pending_preemption;
|
|---|
| 110 |
|
|---|
| 111 | #ifdef __CFA_DEBUG__
|
|---|
| 112 | // Last function to enable preemption on this processor
|
|---|
| 113 | const char * last_enable;
|
|---|
| 114 | #endif
|
|---|
| 115 | };
|
|---|
| 116 |
|
|---|
| 117 | void ?{}(processor & this);
|
|---|
| 118 | void ?{}(processor & this, cluster * cltr);
|
|---|
| 119 | void ^?{}(processor & this);
|
|---|
| 120 |
|
|---|
| 121 | // Local Variables: //
|
|---|
| 122 | // mode: c //
|
|---|
| 123 | // tab-width: 4 //
|
|---|
| 124 | // End: //
|
|---|