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 : Tue Apr 10 14:46:49 2018
|
---|
13 | // Update Count : 10
|
---|
14 | //
|
---|
15 |
|
---|
16 | #pragma once
|
---|
17 |
|
---|
18 | #include <stdbool.h>
|
---|
19 |
|
---|
20 | #include "invoke.h"
|
---|
21 | #include "time_t.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 | // Name of the cluster
|
---|
51 | const char * name;
|
---|
52 |
|
---|
53 | // Preemption rate on this cluster
|
---|
54 | Duration preemption_rate;
|
---|
55 |
|
---|
56 | // List of idle processors
|
---|
57 | // __dllist_t(struct processor) idles;
|
---|
58 | };
|
---|
59 |
|
---|
60 | extern struct cluster * mainCluster;
|
---|
61 | extern Duration default_preemption();
|
---|
62 |
|
---|
63 | void ?{} (cluster & this, const char * name, Duration preemption_rate);
|
---|
64 | void ^?{}(cluster & this);
|
---|
65 |
|
---|
66 | static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption()}; }
|
---|
67 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; }
|
---|
68 | static inline void ?{} (cluster & this, const char * name) { this{name, default_preemption()}; }
|
---|
69 |
|
---|
70 | //-----------------------------------------------------------------------------
|
---|
71 | // Processor
|
---|
72 | enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule };
|
---|
73 |
|
---|
74 | //TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI)
|
---|
75 | struct FinishAction {
|
---|
76 | FinishOpCode action_code;
|
---|
77 | thread_desc * thrd;
|
---|
78 | __spinlock_t * lock;
|
---|
79 | __spinlock_t ** locks;
|
---|
80 | unsigned short lock_count;
|
---|
81 | thread_desc ** thrds;
|
---|
82 | unsigned short thrd_count;
|
---|
83 | };
|
---|
84 | static inline void ?{}(FinishAction & this) {
|
---|
85 | this.action_code = No_Action;
|
---|
86 | this.thrd = NULL;
|
---|
87 | this.lock = NULL;
|
---|
88 | }
|
---|
89 | static inline void ^?{}(FinishAction & this) {}
|
---|
90 |
|
---|
91 | // Processor
|
---|
92 | coroutine processorCtx_t {
|
---|
93 | struct processor * proc;
|
---|
94 | };
|
---|
95 |
|
---|
96 | // Wrapper around kernel threads
|
---|
97 | struct processor {
|
---|
98 | // Main state
|
---|
99 | // Coroutine ctx who does keeps the state of the processor
|
---|
100 | struct processorCtx_t runner;
|
---|
101 |
|
---|
102 | // Cluster from which to get threads
|
---|
103 | cluster * cltr;
|
---|
104 |
|
---|
105 | // Name of the processor
|
---|
106 | const char * name;
|
---|
107 |
|
---|
108 | // Handle to pthreads
|
---|
109 | pthread_t kernel_thread;
|
---|
110 |
|
---|
111 | // Termination
|
---|
112 | // Set to true to notify the processor should terminate
|
---|
113 | volatile bool do_terminate;
|
---|
114 |
|
---|
115 | // Termination synchronisation
|
---|
116 | semaphore terminated;
|
---|
117 |
|
---|
118 | // RunThread data
|
---|
119 | // Action to do after a thread is ran
|
---|
120 | struct FinishAction finish;
|
---|
121 |
|
---|
122 | // Preemption data
|
---|
123 | // Node which is added in the discrete event simulaiton
|
---|
124 | struct alarm_node_t * preemption_alarm;
|
---|
125 |
|
---|
126 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
|
---|
127 | bool pending_preemption;
|
---|
128 |
|
---|
129 | struct {
|
---|
130 | pthread_mutex_t lock;
|
---|
131 | pthread_cond_t cond;
|
---|
132 | } idle;
|
---|
133 |
|
---|
134 | #ifdef __CFA_DEBUG__
|
---|
135 | // Last function to enable preemption on this processor
|
---|
136 | const char * last_enable;
|
---|
137 | #endif
|
---|
138 | };
|
---|
139 |
|
---|
140 | void ?{}(processor & this, const char * name, cluster & cltr);
|
---|
141 | void ^?{}(processor & this);
|
---|
142 |
|
---|
143 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
|
---|
144 | static inline void ?{}(processor & this, cluster & cltr) { this{ "Anonymous Processor", cltr}; }
|
---|
145 | static inline void ?{}(processor & this, const char * name) { this{name, *mainCluster }; }
|
---|
146 |
|
---|
147 | // Local Variables: //
|
---|
148 | // mode: c //
|
---|
149 | // tab-width: 4 //
|
---|
150 | // End: //
|
---|