[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 |
---|
[e60e0dc] | 28 | void lock ( spinlock * DEBUG_CTX_PARAM2 ); // Lock the spinlock, spin if already acquired |
---|
| 29 | void lock_yield( spinlock * DEBUG_CTX_PARAM2 ); // Lock the spinlock, yield repeatedly if already acquired |
---|
| 30 | bool try_lock ( spinlock * DEBUG_CTX_PARAM2 ); // Lock the spinlock, return false if already acquired |
---|
| 31 | void unlock ( spinlock * ); // Unlock the spinlock |
---|
[db6f06a] | 32 | |
---|
[bdeba0b] | 33 | struct semaphore { |
---|
| 34 | spinlock lock; |
---|
| 35 | int count; |
---|
| 36 | __thread_queue_t waiting; |
---|
[9c31349] | 37 | }; |
---|
| 38 | |
---|
[242a902] | 39 | void ?{}(semaphore & this, int count = 1); |
---|
| 40 | void ^?{}(semaphore & this); |
---|
[bdeba0b] | 41 | void P(semaphore * this); |
---|
| 42 | void V(semaphore * this); |
---|
[9c31349] | 43 | |
---|
[db6f06a] | 44 | |
---|
[bd98b58] | 45 | //----------------------------------------------------------------------------- |
---|
| 46 | // Cluster |
---|
| 47 | struct cluster { |
---|
[e60e0dc] | 48 | spinlock ready_queue_lock; // Ready queue locks |
---|
| 49 | __thread_queue_t ready_queue; // Ready queue for threads |
---|
| 50 | unsigned long long int preemption; // Preemption rate on this cluster |
---|
[bd98b58] | 51 | }; |
---|
| 52 | |
---|
[242a902] | 53 | void ?{}(cluster & this); |
---|
| 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; |
---|
[db6f06a] | 64 | spinlock * lock; |
---|
[0c78741] | 65 | spinlock ** locks; |
---|
| 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 |
---|
| 81 | struct processorCtx_t * runner; // Coroutine ctx who does keeps the state of the processor |
---|
| 82 | cluster * cltr; // Cluster from which to get threads |
---|
| 83 | pthread_t kernel_thread; // Handle to pthreads |
---|
[2ac095d] | 84 | |
---|
[e60e0dc] | 85 | // Termination |
---|
| 86 | volatile bool do_terminate; // Set to true to notify the processor should terminate |
---|
| 87 | semaphore terminated; // Termination synchronisation |
---|
[db6f06a] | 88 | |
---|
[e60e0dc] | 89 | // RunThread data |
---|
| 90 | struct FinishAction finish; // Action to do after a thread is ran |
---|
[c81ebf9] | 91 | |
---|
[e60e0dc] | 92 | // Preemption data |
---|
| 93 | struct alarm_node_t * preemption_alarm; // Node which is added in the discrete event simulaiton |
---|
| 94 | bool pending_preemption; // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible |
---|
[c81ebf9] | 95 | |
---|
[e60e0dc] | 96 | #ifdef __CFA_DEBUG__ |
---|
| 97 | char * last_enable; // Last function to enable preemption on this processor |
---|
| 98 | #endif |
---|
[c84e80a] | 99 | }; |
---|
| 100 | |
---|
[242a902] | 101 | void ?{}(processor & this); |
---|
| 102 | void ?{}(processor & this, cluster * cltr); |
---|
| 103 | void ^?{}(processor & this); |
---|
[c84e80a] | 104 | |
---|
[8118303] | 105 | // Local Variables: // |
---|
[6b0b624] | 106 | // mode: c // |
---|
| 107 | // tab-width: 4 // |
---|
[8118303] | 108 | // End: // |
---|