source: libcfa/src/concurrency/kernel.hfa @ 7768b8d

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 7768b8d was 7768b8d, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

First step at adding the new ready queue to Cforall

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