[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" |
---|
| 21 | |
---|
[8def349] | 22 | extern "C" { |
---|
| 23 | #include <pthread.h> |
---|
| 24 | } |
---|
| 25 | |
---|
[db6f06a] | 26 | //----------------------------------------------------------------------------- |
---|
| 27 | // Locks |
---|
[bdeba0b] | 28 | struct semaphore { |
---|
[ea7d2b0] | 29 | __spinlock_t lock; |
---|
[bdeba0b] | 30 | int count; |
---|
[0cf5b79] | 31 | __queue_t(thread_desc) waiting; |
---|
[9c31349] | 32 | }; |
---|
| 33 | |
---|
[242a902] | 34 | void ?{}(semaphore & this, int count = 1); |
---|
| 35 | void ^?{}(semaphore & this); |
---|
[4cedd9f] | 36 | void P (semaphore & this); |
---|
| 37 | void V (semaphore & this); |
---|
[9c31349] | 38 | |
---|
[db6f06a] | 39 | |
---|
[bd98b58] | 40 | //----------------------------------------------------------------------------- |
---|
| 41 | // Cluster |
---|
| 42 | struct cluster { |
---|
[025278e] | 43 | // Ready queue locks |
---|
[ea7d2b0] | 44 | __spinlock_t ready_queue_lock; |
---|
[025278e] | 45 | |
---|
| 46 | // Ready queue for threads |
---|
[0cf5b79] | 47 | __queue_t(thread_desc) ready_queue; |
---|
[025278e] | 48 | |
---|
| 49 | // Preemption rate on this cluster |
---|
| 50 | unsigned long long int preemption; |
---|
[bd98b58] | 51 | }; |
---|
| 52 | |
---|
[4cedd9f] | 53 | void ?{} (cluster & this); |
---|
[242a902] | 54 | void ^?{}(cluster & this); |
---|
[bd98b58] | 55 | |
---|
| 56 | //----------------------------------------------------------------------------- |
---|
| 57 | // Processor |
---|
[0c78741] | 58 | enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule }; |
---|
| 59 | |
---|
| 60 | //TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI) |
---|
[db6f06a] | 61 | struct FinishAction { |
---|
| 62 | FinishOpCode action_code; |
---|
[348006f] | 63 | thread_desc * thrd; |
---|
[ea7d2b0] | 64 | __spinlock_t * lock; |
---|
| 65 | __spinlock_t ** locks; |
---|
[0c78741] | 66 | unsigned short lock_count; |
---|
| 67 | thread_desc ** thrds; |
---|
| 68 | unsigned short thrd_count; |
---|
[8fcbb4c] | 69 | }; |
---|
[242a902] | 70 | static inline void ?{}(FinishAction & this) { |
---|
| 71 | this.action_code = No_Action; |
---|
| 72 | this.thrd = NULL; |
---|
| 73 | this.lock = NULL; |
---|
[db6f06a] | 74 | } |
---|
[242a902] | 75 | static inline void ^?{}(FinishAction & this) {} |
---|
[8fcbb4c] | 76 | |
---|
[e60e0dc] | 77 | // Processor |
---|
| 78 | // Wrapper around kernel threads |
---|
[c84e80a] | 79 | struct processor { |
---|
[e60e0dc] | 80 | // Main state |
---|
[025278e] | 81 | // Coroutine ctx who does keeps the state of the processor |
---|
| 82 | struct processorCtx_t * runner; |
---|
| 83 | |
---|
| 84 | // Cluster from which to get threads |
---|
| 85 | cluster * cltr; |
---|
| 86 | |
---|
| 87 | // Handle to pthreads |
---|
| 88 | pthread_t kernel_thread; |
---|
[2ac095d] | 89 | |
---|
[e60e0dc] | 90 | // Termination |
---|
[025278e] | 91 | // Set to true to notify the processor should terminate |
---|
| 92 | volatile bool do_terminate; |
---|
| 93 | |
---|
| 94 | // Termination synchronisation |
---|
| 95 | semaphore terminated; |
---|
[db6f06a] | 96 | |
---|
[e60e0dc] | 97 | // RunThread data |
---|
[025278e] | 98 | // Action to do after a thread is ran |
---|
| 99 | struct FinishAction finish; |
---|
[c81ebf9] | 100 | |
---|
[e60e0dc] | 101 | // Preemption data |
---|
[025278e] | 102 | // Node which is added in the discrete event simulaiton |
---|
| 103 | struct alarm_node_t * preemption_alarm; |
---|
| 104 | |
---|
| 105 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible |
---|
| 106 | bool pending_preemption; |
---|
[c81ebf9] | 107 | |
---|
[e60e0dc] | 108 | #ifdef __CFA_DEBUG__ |
---|
[025278e] | 109 | // Last function to enable preemption on this processor |
---|
[cdbfab0] | 110 | const char * last_enable; |
---|
[e60e0dc] | 111 | #endif |
---|
[c84e80a] | 112 | }; |
---|
| 113 | |
---|
[8fc45b7] | 114 | void ?{}(processor & this); |
---|
| 115 | void ?{}(processor & this, cluster * cltr); |
---|
[242a902] | 116 | void ^?{}(processor & this); |
---|
[c84e80a] | 117 | |
---|
[8118303] | 118 | // Local Variables: // |
---|
[6b0b624] | 119 | // mode: c // |
---|
| 120 | // tab-width: 4 // |
---|
[8118303] | 121 | // End: // |
---|