| 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 | 
 | 
|---|
| 22 | extern "C" {
 | 
|---|
| 23 | #include <pthread.h>
 | 
|---|
| 24 | }
 | 
|---|
| 25 | 
 | 
|---|
| 26 | //-----------------------------------------------------------------------------
 | 
|---|
| 27 | // Locks
 | 
|---|
| 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
 | 
|---|
| 32 | 
 | 
|---|
| 33 | struct semaphore {
 | 
|---|
| 34 |         spinlock lock;
 | 
|---|
| 35 |         int count;
 | 
|---|
| 36 |         __thread_queue_t waiting;
 | 
|---|
| 37 | };
 | 
|---|
| 38 | 
 | 
|---|
| 39 | void  ?{}(semaphore & this, int count = 1);
 | 
|---|
| 40 | void ^?{}(semaphore & this);
 | 
|---|
| 41 | void P(semaphore * this);
 | 
|---|
| 42 | void V(semaphore * this);
 | 
|---|
| 43 | 
 | 
|---|
| 44 | 
 | 
|---|
| 45 | //-----------------------------------------------------------------------------
 | 
|---|
| 46 | // Cluster
 | 
|---|
| 47 | struct cluster {
 | 
|---|
| 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
 | 
|---|
| 51 | };
 | 
|---|
| 52 | 
 | 
|---|
| 53 | void ?{}(cluster & this);
 | 
|---|
| 54 | void ^?{}(cluster & this);
 | 
|---|
| 55 | 
 | 
|---|
| 56 | //-----------------------------------------------------------------------------
 | 
|---|
| 57 | // Processor
 | 
|---|
| 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)
 | 
|---|
| 61 | struct FinishAction {
 | 
|---|
| 62 |         FinishOpCode action_code;
 | 
|---|
| 63 |         thread_desc * thrd;
 | 
|---|
| 64 |         spinlock * lock;
 | 
|---|
| 65 |         spinlock ** locks;
 | 
|---|
| 66 |         unsigned short lock_count;
 | 
|---|
| 67 |         thread_desc ** thrds;
 | 
|---|
| 68 |         unsigned short thrd_count;
 | 
|---|
| 69 | };
 | 
|---|
| 70 | static inline void ?{}(FinishAction & this) {
 | 
|---|
| 71 |         this.action_code = No_Action;
 | 
|---|
| 72 |         this.thrd = NULL;
 | 
|---|
| 73 |         this.lock = NULL;
 | 
|---|
| 74 | }
 | 
|---|
| 75 | static inline void ^?{}(FinishAction & this) {}
 | 
|---|
| 76 | 
 | 
|---|
| 77 | // Processor
 | 
|---|
| 78 | // Wrapper around kernel threads
 | 
|---|
| 79 | struct processor {
 | 
|---|
| 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
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         // Termination
 | 
|---|
| 86 |         volatile bool do_terminate;                     // Set to true to notify the processor should terminate
 | 
|---|
| 87 |         semaphore terminated;                           // Termination synchronisation
 | 
|---|
| 88 | 
 | 
|---|
| 89 |         // RunThread data
 | 
|---|
| 90 |         struct FinishAction finish;                     // Action to do after a thread is ran
 | 
|---|
| 91 | 
 | 
|---|
| 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
 | 
|---|
| 95 | 
 | 
|---|
| 96 | #ifdef __CFA_DEBUG__
 | 
|---|
| 97 |         char * last_enable;                             // Last function to enable preemption on this processor
 | 
|---|
| 98 | #endif
 | 
|---|
| 99 | };
 | 
|---|
| 100 | 
 | 
|---|
| 101 | void ?{}(processor & this);
 | 
|---|
| 102 | void ?{}(processor & this, cluster * cltr);
 | 
|---|
| 103 | void ^?{}(processor & this);
 | 
|---|
| 104 | 
 | 
|---|
| 105 | // Local Variables: //
 | 
|---|
| 106 | // mode: c //
 | 
|---|
| 107 | // tab-width: 4 //
 | 
|---|
| 108 | // End: //
 | 
|---|