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

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

Merge branch 'master' into relaxed_ready

  • Property mode set to 100644
File size: 6.5 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#include "coroutine.hfa"
23
24extern "C" {
25#include <pthread.h>
26#include <semaphore.h>
27}
28
29//-----------------------------------------------------------------------------
30// Locks
31struct semaphore {
32        __spinlock_t lock;
33        int count;
34        __queue_t(thread_desc) waiting;
35};
36
37void  ?{}(semaphore & this, int count = 1);
38void ^?{}(semaphore & this);
39void   P (semaphore & this);
40void   V (semaphore & this);
41
42
43//-----------------------------------------------------------------------------
44// Processor
45extern struct cluster * mainCluster;
46
47enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule, Callback };
48
49typedef void (*__finish_callback_fptr_t)(void);
50
51//TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI)
52struct FinishAction {
53        FinishOpCode action_code;
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        /*/
80        thread_desc * thrd;
81        thread_desc ** thrds;
82        unsigned short thrd_count;
83        __spinlock_t * lock;
84        __spinlock_t ** locks;
85        unsigned short lock_count;
86        __finish_callback_fptr_t callback;
87        //*/
88};
89static inline void ?{}(FinishAction & this) {
90        this.action_code = No_Action;
91        this.thrd = NULL;
92        this.lock = NULL;
93}
94static inline void ^?{}(FinishAction &) {}
95
96// Processor
97coroutine processorCtx_t {
98        struct processor * proc;
99};
100
101// Wrapper around kernel threads
102struct processor {
103        // Main state
104        // Coroutine ctx who does keeps the state of the processor
105        struct processorCtx_t runner;
106
107        // Cluster from which to get threads
108        struct cluster * cltr;
109        unsigned int id;
110
111        // Name of the processor
112        const char * name;
113
114        // Handle to pthreads
115        pthread_t kernel_thread;
116
117        // RunThread data
118        // Action to do after a thread is ran
119        struct FinishAction finish;
120
121        // Preemption data
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;
127
128        // Idle lock
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;
137
138        // Link lists fields
139        struct __dbg_node_proc {
140                struct processor * next;
141                struct processor * prev;
142        } node;
143
144#ifdef __CFA_DEBUG__
145        // Last function to enable preemption on this processor
146        const char * last_enable;
147#endif
148};
149
150void  ?{}(processor & this, const char * name, struct cluster & cltr);
151void ^?{}(processor & this);
152
153static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; }
154static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; }
155static inline void  ?{}(processor & this, const char * name) { this{name, *mainCluster }; }
156
157static inline [processor *&, processor *& ] __get( processor & this ) {
158        return this.node.[next, prev];
159}
160
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;
192
193        // anchor for the head and the tail of the queue
194        struct __sentinel_t {
195                struct thread_desc * next;
196                struct thread_desc * prev;
197                unsigned long long ts;
198        } before, after;
199
200        // Optional statistic counters
201        #ifndef __CFA_NO_SCHED_STATS__
202                struct __attribute__((aligned(64))) {
203                        // difference between number of push and pops
204                        ssize_t diff;
205
206                        // total number of pushes and pops
207                        size_t  push;
208                        size_t  pop ;
209                } stat;
210        #endif
211};
212
213void  ?{}(__intrusive_ready_queue_t & this);
214void ^?{}(__intrusive_ready_queue_t & this);
215
216//-----------------------------------------------------------------------------
217// Cluster
218struct cluster {
219        // Ready queue locks
220        __clusterRWLock_t ready_lock;
221
222        // Ready queue for threads
223        __intrusive_ready_queue_t ready_queue;
224
225        // Name of the cluster
226        const char * name;
227
228        // Preemption rate on this cluster
229        Duration preemption_rate;
230
231        // List of processors
232        __spinlock_t proc_list_lock;
233        __dllist_t(struct processor) idles;
234
235        // List of threads
236        __spinlock_t thread_list_lock;
237        __dllist_t(struct thread_desc) threads;
238        unsigned int nthreads;
239
240        // Link lists fields
241        struct __dbg_node_cltr {
242                cluster * next;
243                cluster * prev;
244        } node;
245};
246extern Duration default_preemption();
247
248void ?{} (cluster & this, const char * name, Duration preemption_rate);
249void ^?{}(cluster & this);
250
251static inline void ?{} (cluster & this)                           { this{"Anonymous Cluster", default_preemption()}; }
252static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; }
253static inline void ?{} (cluster & this, const char * name)        { this{name, default_preemption()}; }
254
255static inline [cluster *&, cluster *& ] __get( cluster & this ) {
256        return this.node.[next, prev];
257}
258
259static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
260static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
261
262// Local Variables: //
263// mode: c //
264// tab-width: 4 //
265// End: //
Note: See TracBrowser for help on using the repository browser.