source: libcfa/src/concurrency/kernel.hfa@ b798713

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since b798713 was b798713, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Working ready queue

  • Property mode set to 100644
File size: 7.7 KB
RevLine 
[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
[d4e68a6]12// Last Modified On : Sat Jun 22 11:39:17 2019
13// Update Count : 16
[8118303]14//
15
[6b0b624]16#pragma once
[8118303]17
[c84e80a]18#include <stdbool.h>
[8118303]19
[bd98b58]20#include "invoke.h"
[73abe95]21#include "time_t.hfa"
[d76bd79]22#include "coroutine.hfa"
[bd98b58]23
[8def349]24extern "C" {
25#include <pthread.h>
[6b4cdd3]26#include <semaphore.h>
[8def349]27}
28
[db6f06a]29//-----------------------------------------------------------------------------
30// Locks
[bdeba0b]31struct semaphore {
[ea7d2b0]32 __spinlock_t lock;
[bdeba0b]33 int count;
[0cf5b79]34 __queue_t(thread_desc) waiting;
[9c31349]35};
36
[242a902]37void ?{}(semaphore & this, int count = 1);
38void ^?{}(semaphore & this);
[4cedd9f]39void P (semaphore & this);
40void V (semaphore & this);
[9c31349]41
[db6f06a]42
[bd98b58]43//-----------------------------------------------------------------------------
[de94a60]44// Processor
[de6319f]45extern struct cluster * mainCluster;
[bd98b58]46
[09800e9]47enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule, Callback };
48
49typedef void (*__finish_callback_fptr_t)(void);
[0c78741]50
51//TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI)
[db6f06a]52struct FinishAction {
53 FinishOpCode action_code;
[09800e9]54 /*
55 // Union of possible actions
56 union {
57 // Option 1 : locks and threads
58 struct {
59 // 1 thread or N thread
60 union {
61 thread_desc * thrd;
62 struct {
63 thread_desc ** thrds;
64 unsigned short thrd_count;
65 };
66 };
67 // 1 lock or N lock
68 union {
69 __spinlock_t * lock;
70 struct {
71 __spinlock_t ** locks;
72 unsigned short lock_count;
73 };
74 };
75 };
76 // Option 2 : action pointer
77 __finish_callback_fptr_t callback;
78 };
79 /*/
[348006f]80 thread_desc * thrd;
[09800e9]81 thread_desc ** thrds;
82 unsigned short thrd_count;
[ea7d2b0]83 __spinlock_t * lock;
84 __spinlock_t ** locks;
[0c78741]85 unsigned short lock_count;
[09800e9]86 __finish_callback_fptr_t callback;
87 //*/
[8fcbb4c]88};
[242a902]89static inline void ?{}(FinishAction & this) {
90 this.action_code = No_Action;
91 this.thrd = NULL;
92 this.lock = NULL;
[db6f06a]93}
[d4e68a6]94static inline void ^?{}(FinishAction &) {}
[8fcbb4c]95
[e60e0dc]96// Processor
[094476d]97coroutine processorCtx_t {
98 struct processor * proc;
99};
100
[e60e0dc]101// Wrapper around kernel threads
[c84e80a]102struct processor {
[e60e0dc]103 // Main state
[025278e]104 // Coroutine ctx who does keeps the state of the processor
[094476d]105 struct processorCtx_t runner;
[025278e]106
107 // Cluster from which to get threads
[de94a60]108 struct cluster * cltr;
[7768b8d]109 unsigned int id;
[025278e]110
[de6319f]111 // Name of the processor
112 const char * name;
113
[025278e]114 // Handle to pthreads
115 pthread_t kernel_thread;
[2ac095d]116
[e60e0dc]117 // RunThread data
[025278e]118 // Action to do after a thread is ran
119 struct FinishAction finish;
[c81ebf9]120
[e60e0dc]121 // Preemption data
[025278e]122 // Node which is added in the discrete event simulaiton
123 struct alarm_node_t * preemption_alarm;
124
125 // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
126 bool pending_preemption;
[c81ebf9]127
[de94a60]128 // Idle lock
[85b1deb]129 __bin_sem_t idleLock;
130
131 // Termination
132 // Set to true to notify the processor should terminate
133 volatile bool do_terminate;
134
135 // Termination synchronisation
136 semaphore terminated;
[de94a60]137
138 // Link lists fields
[ea8b2f7]139 struct __dbg_node_proc {
[de94a60]140 struct processor * next;
141 struct processor * prev;
142 } node;
[14a61b5]143
[e60e0dc]144#ifdef __CFA_DEBUG__
[025278e]145 // Last function to enable preemption on this processor
[cdbfab0]146 const char * last_enable;
[e60e0dc]147#endif
[c84e80a]148};
149
[de94a60]150void ?{}(processor & this, const char * name, struct cluster & cltr);
[242a902]151void ^?{}(processor & this);
[c84e80a]152
[de6319f]153static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
[de94a60]154static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
[de6319f]155static inline void ?{}(processor & this, const char * name) { this{name, *mainCluster }; }
156
[de94a60]157static inline [processor *&, processor *& ] __get( processor & this ) {
158 return this.node.[next, prev];
159}
160
[7768b8d]161
162//-----------------------------------------------------------------------------
163// Cluster Tools
164struct __processor_id;
165
166// Reader-Writer lock protecting the ready-queue
167struct __clusterRWLock_t {
168 // total cachelines allocated
169 unsigned int max;
170
171 // cachelines currently in use
172 volatile unsigned int alloc;
173
174 // cachelines ready to itereate over
175 // (!= to alloc when thread is in second half of doregister)
176 volatile unsigned int ready;
177
178 // writer lock
179 volatile bool lock;
180
181 // data pointer
182 __processor_id * data;
183};
184
185void ?{}(__clusterRWLock_t & this);
186void ^?{}(__clusterRWLock_t & this);
187
188// Underlying sub quues of the ready queue
189struct __attribute__((aligned(128))) __intrusive_ready_queue_t {
190 // spin lock protecting the queue
191 volatile bool lock;
[b798713]192 unsigned int last_id;
[7768b8d]193
194 // anchor for the head and the tail of the queue
195 struct __sentinel_t {
[b798713]196 // Link lists fields
197 // instrusive link field for threads
198 // must be exactly as in thread_desc
199 __thread_desc_link link;
[7768b8d]200 } before, after;
201
202 // Optional statistic counters
[b798713]203 #if !defined(__CFA_NO_SCHED_STATS__)
[7768b8d]204 struct __attribute__((aligned(64))) {
205 // difference between number of push and pops
206 ssize_t diff;
207
208 // total number of pushes and pops
209 size_t push;
210 size_t pop ;
211 } stat;
212 #endif
213};
214
215void ?{}(__intrusive_ready_queue_t & this);
216void ^?{}(__intrusive_ready_queue_t & this);
217
[b798713]218typedef unsigned long long __cfa_readyQ_mask_t;
219
220// enum {
221// __cfa_ready_queue_mask_size = (64 - sizeof(size_t)) / sizeof(size_t),
222// __cfa_max_ready_queues = __cfa_ready_queue_mask_size * 8 * sizeof(size_t)
223// };
224
225#define __cfa_readyQ_mask_size ((64 - sizeof(size_t)) / sizeof(__cfa_readyQ_mask_t))
226#define __cfa_max_readyQs (__cfa_readyQ_mask_size * 8 * sizeof(__cfa_readyQ_mask_t))
227
228//TODO adjust cache size to ARCHITECTURE
229struct __attribute__((aligned(128))) __ready_queue_t {
230 struct {
231 volatile size_t count;
232 volatile __cfa_readyQ_mask_t mask[ __cfa_readyQ_mask_size ];
233 } empty;
234
235 struct __attribute__((aligned(64))) {
236 __intrusive_ready_queue_t * volatile data;
237 volatile size_t count;
238 } list;
239
240 #if !defined(__CFA_NO_STATISTICS__)
241 __attribute__((aligned(64))) struct {
242 struct {
243 struct {
244 volatile size_t attempt;
245 volatile size_t success;
246 } push;
247 struct {
248 volatile size_t maskrds;
249 volatile size_t attempt;
250 volatile size_t success;
251 } pop;
252 } pick;
253 struct {
254 volatile size_t value;
255 volatile size_t count;
256 } full;
257 } global_stats;
258
259 #endif
260};
261
262void ?{}(__ready_queue_t & this);
263void ^?{}(__ready_queue_t & this);
264
[de94a60]265//-----------------------------------------------------------------------------
266// Cluster
267struct cluster {
268 // Ready queue locks
[7768b8d]269 __clusterRWLock_t ready_lock;
[de94a60]270
271 // Ready queue for threads
[b798713]272 __ready_queue_t ready_queue;
[de94a60]273
274 // Name of the cluster
275 const char * name;
276
277 // Preemption rate on this cluster
278 Duration preemption_rate;
279
280 // List of processors
281 __spinlock_t proc_list_lock;
282 __dllist_t(struct processor) idles;
283
[d4e68a6]284 // List of threads
[a1a17a74]285 __spinlock_t thread_list_lock;
286 __dllist_t(struct thread_desc) threads;
[d4e68a6]287 unsigned int nthreads;
[a1a17a74]288
[de94a60]289 // Link lists fields
[ea8b2f7]290 struct __dbg_node_cltr {
[de94a60]291 cluster * next;
292 cluster * prev;
293 } node;
294};
295extern Duration default_preemption();
296
297void ?{} (cluster & this, const char * name, Duration preemption_rate);
298void ^?{}(cluster & this);
299
300static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption()}; }
301static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; }
302static inline void ?{} (cluster & this, const char * name) { this{name, default_preemption()}; }
303
304static inline [cluster *&, cluster *& ] __get( cluster & this ) {
305 return this.node.[next, prev];
306}
307
[d4e68a6]308static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
309static inline struct cluster * active_cluster () { return TL_GET( this_processor )->cltr; }
310
[8118303]311// Local Variables: //
[6b0b624]312// mode: c //
313// tab-width: 4 //
[8118303]314// End: //
Note: See TracBrowser for help on using the repository browser.