source: libcfa/src/concurrency/kernel.hfa @ 0f89d4f

arm-ehjacob/cs343-translationnew-astnew-ast-unique-expr
Last change on this file since 0f89d4f was 0f89d4f, checked in by Thierry Delisle <tdelisle@…>, 18 months ago

Modified StackLF to use `next instead of getNext

  • 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 : Tue Feb  4 12:29:26 2020
13// Update Count     : 22
14//
15
16#pragma once
17
18#include <stdbool.h>
19#include <stdint.h>
20
21#include "invoke.h"
22#include "time_t.hfa"
23#include "coroutine.hfa"
24
25#include "containers/stackLockFree.hfa"
26
27extern "C" {
28#include <pthread.h>
29#include <semaphore.h>
30}
31
32//-----------------------------------------------------------------------------
33// Locks
34struct semaphore {
35        __spinlock_t lock;
36        int count;
37        __queue_t($thread) waiting;
38};
39
40void  ?{}(semaphore & this, int count = 1);
41void ^?{}(semaphore & this);
42bool   P (semaphore & this);
43bool   V (semaphore & this);
44bool   V (semaphore & this, unsigned count);
45
46
47//-----------------------------------------------------------------------------
48// Processor
49extern struct cluster * mainCluster;
50
51// Processor id, required for scheduling threads
52struct __processor_id_t {
53        unsigned id;
54
55        #if !defined(__CFA_NO_STATISTICS__)
56                struct __stats_t * stats;
57        #endif
58};
59
60coroutine processorCtx_t {
61        struct processor * proc;
62};
63
64// Wrapper around kernel threads
65struct __attribute__((aligned(128))) processor {
66        // Main state
67        inline __processor_id_t;
68
69        // Cluster from which to get threads
70        struct cluster * cltr;
71
72        // Set to true to notify the processor should terminate
73        volatile bool do_terminate;
74
75        // Coroutine ctx who does keeps the state of the processor
76        struct processorCtx_t runner;
77
78        // Name of the processor
79        const char * name;
80
81        // Handle to pthreads
82        pthread_t kernel_thread;
83
84        // RunThread data
85        // Action to do after a thread is ran
86        $thread * destroyer;
87
88        // Preemption data
89        // Node which is added in the discrete event simulaiton
90        struct alarm_node_t * preemption_alarm;
91
92        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
93        bool pending_preemption;
94
95        // Idle lock (kernel semaphore)
96        __bin_sem_t idle;
97
98        // Termination synchronisation (user semaphore)
99        semaphore terminated;
100
101        // pthread Stack
102        void * stack;
103
104        // Link lists fields
105        Link(processor) link;
106
107#ifdef __CFA_DEBUG__
108        // Last function to enable preemption on this processor
109        const char * last_enable;
110#endif
111};
112
113void  ?{}(processor & this, const char name[], struct cluster & cltr);
114void ^?{}(processor & this);
115
116static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; }
117static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; }
118static inline void  ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
119
120static inline Link(processor) * ?`next( processor * this ) { return &this->link; }
121
122//-----------------------------------------------------------------------------
123// I/O
124struct __io_data;
125
126#define CFA_CLUSTER_IO_POLLER_USER_THREAD    1 << 0 // 0x1
127#define CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS 1 << 1 // 0x2
128// #define CFA_CLUSTER_IO_POLLER_KERNEL_SIDE 1 << 2 // 0x4
129#define CFA_CLUSTER_IO_BUFFLEN_OFFSET        16
130
131
132//-----------------------------------------------------------------------------
133// Cluster Tools
134
135// Intrusives lanes which are used by the relaxed ready queue
136struct __attribute__((aligned(128))) __intrusive_lane_t;
137void  ?{}(__intrusive_lane_t & this);
138void ^?{}(__intrusive_lane_t & this);
139
140// Counter used for wether or not the lanes are all empty
141struct __attribute__((aligned(128))) __snzi_node_t;
142struct __snzi_t {
143        unsigned mask;
144        int root;
145        __snzi_node_t * nodes;
146};
147
148void  ?{}( __snzi_t & this, unsigned depth );
149void ^?{}( __snzi_t & this );
150
151//TODO adjust cache size to ARCHITECTURE
152// Structure holding the relaxed ready queue
153struct __ready_queue_t {
154        // Data tracking how many/which lanes are used
155        // Aligned to 128 for cache locality
156        __snzi_t snzi;
157
158        // Data tracking the actual lanes
159        // On a seperate cacheline from the used struct since
160        // used can change on each push/pop but this data
161        // only changes on shrink/grow
162        struct {
163                // Arary of lanes
164                __intrusive_lane_t * volatile data;
165
166                // Number of lanes (empty or not)
167                volatile size_t count;
168        } lanes;
169};
170
171void  ?{}(__ready_queue_t & this);
172void ^?{}(__ready_queue_t & this);
173
174//-----------------------------------------------------------------------------
175// Cluster
176struct __attribute__((aligned(128))) cluster {
177        // Ready queue for threads
178        __ready_queue_t ready_queue;
179
180        // Name of the cluster
181        const char * name;
182
183        // Preemption rate on this cluster
184        Duration preemption_rate;
185
186        // List of idle processors
187        StackLF(processor) idles;
188        unsigned int nprocessors;
189
190        // List of threads
191        __spinlock_t thread_list_lock;
192        __dllist_t(struct $thread) threads;
193        unsigned int nthreads;
194
195        // Link lists fields
196        struct __dbg_node_cltr {
197                cluster * next;
198                cluster * prev;
199        } node;
200
201        struct __io_data * io;
202
203        #if !defined(__CFA_NO_STATISTICS__)
204                bool print_stats;
205                struct __stats_t * stats;
206        #endif
207};
208extern Duration default_preemption();
209
210void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned flags);
211void ^?{}(cluster & this);
212
213static inline void ?{} (cluster & this)                                           { this{"Anonymous Cluster", default_preemption(), 0}; }
214static inline void ?{} (cluster & this, Duration preemption_rate)                 { this{"Anonymous Cluster", preemption_rate, 0}; }
215static inline void ?{} (cluster & this, const char name[])                        { this{name, default_preemption(), 0}; }
216static inline void ?{} (cluster & this, unsigned flags)                           { this{"Anonymous Cluster", default_preemption(), flags}; }
217static inline void ?{} (cluster & this, Duration preemption_rate, unsigned flags) { this{"Anonymous Cluster", preemption_rate, flags}; }
218static inline void ?{} (cluster & this, const char name[], unsigned flags)        { this{name, default_preemption(), flags}; }
219
220static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
221
222static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
223static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
224
225#if !defined(__CFA_NO_STATISTICS__)
226        static inline void print_stats_at_exit( cluster & this ) {
227                this.print_stats = true;
228        }
229#endif
230
231// Local Variables: //
232// mode: c //
233// tab-width: 4 //
234// End: //
Note: See TracBrowser for help on using the repository browser.