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

arm-ehjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since d76bd79 was d76bd79, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

Added missing include in kernel.hfa which lead to memory leaks in kernel startup

  • Property mode set to 100644
File size: 5.2 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
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// Cluster
162struct cluster {
163        // Ready queue locks
164        __spinlock_t ready_queue_lock;
165
166        // Ready queue for threads
167        __queue_t(thread_desc) ready_queue;
168
169        // Name of the cluster
170        const char * name;
171
172        // Preemption rate on this cluster
173        Duration preemption_rate;
174
175        // List of processors
176        __spinlock_t proc_list_lock;
177        __dllist_t(struct processor) procs;
178        __dllist_t(struct processor) idles;
179        unsigned int nprocessors;
180
181        // List of threads
182        __spinlock_t thread_list_lock;
183        __dllist_t(struct thread_desc) threads;
184        unsigned int nthreads;
185
186        // Link lists fields
187        struct __dbg_node_cltr {
188                cluster * next;
189                cluster * prev;
190        } node;
191};
192extern Duration default_preemption();
193
194void ?{} (cluster & this, const char * name, Duration preemption_rate);
195void ^?{}(cluster & this);
196
197static inline void ?{} (cluster & this)                           { this{"Anonymous Cluster", default_preemption()}; }
198static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; }
199static inline void ?{} (cluster & this, const char * name)        { this{name, default_preemption()}; }
200
201static inline [cluster *&, cluster *& ] __get( cluster & this ) {
202        return this.node.[next, prev];
203}
204
205static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
206static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
207
208// Local Variables: //
209// mode: c //
210// tab-width: 4 //
211// End: //
Note: See TracBrowser for help on using the repository browser.