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 Feb 4 12:29:26 2020 |
---|
13 | // Update Count : 22 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <stdbool.h> |
---|
19 | |
---|
20 | #include "invoke.h" |
---|
21 | #include "time_t.hfa" |
---|
22 | #include "coroutine.hfa" |
---|
23 | |
---|
24 | extern "C" { |
---|
25 | #include <pthread.h> |
---|
26 | #include <semaphore.h> |
---|
27 | } |
---|
28 | |
---|
29 | //----------------------------------------------------------------------------- |
---|
30 | // Locks |
---|
31 | struct semaphore { |
---|
32 | __spinlock_t lock; |
---|
33 | int count; |
---|
34 | __queue_t($thread) waiting; |
---|
35 | }; |
---|
36 | |
---|
37 | void ?{}(semaphore & this, int count = 1); |
---|
38 | void ^?{}(semaphore & this); |
---|
39 | void P (semaphore & this); |
---|
40 | void V (semaphore & this); |
---|
41 | |
---|
42 | |
---|
43 | //----------------------------------------------------------------------------- |
---|
44 | // Processor |
---|
45 | extern struct cluster * mainCluster; |
---|
46 | |
---|
47 | // Processor |
---|
48 | coroutine processorCtx_t { |
---|
49 | struct processor * proc; |
---|
50 | }; |
---|
51 | |
---|
52 | // Wrapper around kernel threads |
---|
53 | struct processor { |
---|
54 | // Main state |
---|
55 | // Coroutine ctx who does keeps the state of the processor |
---|
56 | struct processorCtx_t runner; |
---|
57 | |
---|
58 | // Cluster from which to get threads |
---|
59 | struct cluster * cltr; |
---|
60 | |
---|
61 | // Name of the processor |
---|
62 | const char * name; |
---|
63 | |
---|
64 | // Handle to pthreads |
---|
65 | pthread_t kernel_thread; |
---|
66 | |
---|
67 | // RunThread data |
---|
68 | // Action to do after a thread is ran |
---|
69 | $thread * destroyer; |
---|
70 | |
---|
71 | // Preemption data |
---|
72 | // Node which is added in the discrete event simulaiton |
---|
73 | struct alarm_node_t * preemption_alarm; |
---|
74 | |
---|
75 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible |
---|
76 | bool pending_preemption; |
---|
77 | |
---|
78 | // Idle lock |
---|
79 | __bin_sem_t idleLock; |
---|
80 | |
---|
81 | // Termination |
---|
82 | // Set to true to notify the processor should terminate |
---|
83 | volatile bool do_terminate; |
---|
84 | |
---|
85 | // Termination synchronisation |
---|
86 | semaphore terminated; |
---|
87 | |
---|
88 | // pthread Stack |
---|
89 | void * stack; |
---|
90 | |
---|
91 | // Link lists fields |
---|
92 | struct __dbg_node_proc { |
---|
93 | struct processor * next; |
---|
94 | struct processor * prev; |
---|
95 | } node; |
---|
96 | |
---|
97 | #ifdef __CFA_DEBUG__ |
---|
98 | // Last function to enable preemption on this processor |
---|
99 | const char * last_enable; |
---|
100 | #endif |
---|
101 | }; |
---|
102 | |
---|
103 | void ?{}(processor & this, const char name[], struct cluster & cltr); |
---|
104 | void ^?{}(processor & this); |
---|
105 | |
---|
106 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } |
---|
107 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } |
---|
108 | static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; } |
---|
109 | |
---|
110 | static inline [processor *&, processor *& ] __get( processor & this ) __attribute__((const)) { return this.node.[next, prev]; } |
---|
111 | |
---|
112 | //----------------------------------------------------------------------------- |
---|
113 | // Cluster |
---|
114 | struct cluster { |
---|
115 | // Ready queue locks |
---|
116 | __spinlock_t ready_queue_lock; |
---|
117 | |
---|
118 | // Ready queue for threads |
---|
119 | __queue_t($thread) ready_queue; |
---|
120 | |
---|
121 | // Name of the cluster |
---|
122 | const char * name; |
---|
123 | |
---|
124 | // Preemption rate on this cluster |
---|
125 | Duration preemption_rate; |
---|
126 | |
---|
127 | // List of processors |
---|
128 | __spinlock_t proc_list_lock; |
---|
129 | __dllist_t(struct processor) procs; |
---|
130 | __dllist_t(struct processor) idles; |
---|
131 | unsigned int nprocessors; |
---|
132 | |
---|
133 | // List of threads |
---|
134 | __spinlock_t thread_list_lock; |
---|
135 | __dllist_t(struct $thread) threads; |
---|
136 | unsigned int nthreads; |
---|
137 | |
---|
138 | // Link lists fields |
---|
139 | struct __dbg_node_cltr { |
---|
140 | cluster * next; |
---|
141 | cluster * prev; |
---|
142 | } node; |
---|
143 | }; |
---|
144 | extern Duration default_preemption(); |
---|
145 | |
---|
146 | void ?{} (cluster & this, const char name[], Duration preemption_rate); |
---|
147 | void ^?{}(cluster & this); |
---|
148 | |
---|
149 | static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption()}; } |
---|
150 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; } |
---|
151 | static inline void ?{} (cluster & this, const char name[]) { this{name, default_preemption()}; } |
---|
152 | |
---|
153 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } |
---|
154 | |
---|
155 | static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE |
---|
156 | static inline struct cluster * active_cluster () { return TL_GET( this_processor )->cltr; } |
---|
157 | |
---|
158 | // Local Variables: // |
---|
159 | // mode: c // |
---|
160 | // tab-width: 4 // |
---|
161 | // End: // |
---|