source: src/libcfa/concurrency/kernel @ 6c7099a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 6c7099a was 09800e9, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

First draft at locks in Cforall

  • Property mode set to 100644
File size: 4.9 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 Apr 10 14:46:49 2018
13// Update Count     : 10
14//
15
16#pragma once
17
18#include <stdbool.h>
19
20#include "invoke.h"
21#include "time_t.h"
22
23extern "C" {
24#include <pthread.h>
25}
26
27//-----------------------------------------------------------------------------
28// Locks
29struct semaphore {
30        __spinlock_t lock;
31        int count;
32        __queue_t(thread_desc) waiting;
33};
34
35void  ?{}(semaphore & this, int count = 1);
36void ^?{}(semaphore & this);
37void   P (semaphore & this);
38void   V (semaphore & this);
39
40
41//-----------------------------------------------------------------------------
42// Processor
43extern struct cluster * mainCluster;
44
45enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule, Callback };
46
47typedef void (*__finish_callback_fptr_t)(void);
48
49//TODO use union, many of these fields are mutually exclusive (i.e. MULTI vs NOMULTI)
50struct FinishAction {
51        FinishOpCode action_code;
52        /*
53        // Union of possible actions
54        union {
55                // Option 1 : locks and threads
56                struct {
57                        // 1 thread or N thread
58                        union {
59                                thread_desc * thrd;
60                                struct {
61                                        thread_desc ** thrds;
62                                        unsigned short thrd_count;
63                                };
64                        };
65                        // 1 lock or N lock
66                        union {
67                                __spinlock_t * lock;
68                                struct {
69                                        __spinlock_t ** locks;
70                                        unsigned short lock_count;
71                                };
72                        };
73                };
74                // Option 2 : action pointer
75                __finish_callback_fptr_t callback;
76        };
77        /*/
78        thread_desc * thrd;
79        thread_desc ** thrds;
80        unsigned short thrd_count;
81        __spinlock_t * lock;
82        __spinlock_t ** locks;
83        unsigned short lock_count;
84        __finish_callback_fptr_t callback;
85        //*/
86};
87static inline void ?{}(FinishAction & this) {
88        this.action_code = No_Action;
89        this.thrd = NULL;
90        this.lock = NULL;
91}
92static inline void ^?{}(FinishAction & this) {}
93
94// Processor
95coroutine processorCtx_t {
96        struct processor * proc;
97};
98
99// Wrapper around kernel threads
100struct processor {
101        // Main state
102        // Coroutine ctx who does keeps the state of the processor
103        struct processorCtx_t runner;
104
105        // Cluster from which to get threads
106        struct cluster * cltr;
107
108        // Name of the processor
109        const char * name;
110
111        // Handle to pthreads
112        pthread_t kernel_thread;
113
114        // Termination
115        // Set to true to notify the processor should terminate
116        volatile bool do_terminate;
117
118        // Termination synchronisation
119        semaphore terminated;
120
121        // RunThread data
122        // Action to do after a thread is ran
123        struct FinishAction finish;
124
125        // Preemption data
126        // Node which is added in the discrete event simulaiton
127        struct alarm_node_t * preemption_alarm;
128
129        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
130        bool pending_preemption;
131
132        // Idle lock
133
134        // Link lists fields
135        struct {
136                struct processor * next;
137                struct processor * prev;
138        } node;
139
140#ifdef __CFA_DEBUG__
141        // Last function to enable preemption on this processor
142        const char * last_enable;
143#endif
144};
145
146void  ?{}(processor & this, const char * name, struct cluster & cltr);
147void ^?{}(processor & this);
148
149static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; }
150static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; }
151static inline void  ?{}(processor & this, const char * name) { this{name, *mainCluster }; }
152
153static inline [processor *&, processor *& ] __get( processor & this ) {
154        return this.node.[next, prev];
155}
156
157//-----------------------------------------------------------------------------
158// Cluster
159struct cluster {
160        // Ready queue locks
161        __spinlock_t ready_queue_lock;
162
163        // Ready queue for threads
164        __queue_t(thread_desc) ready_queue;
165
166        // Name of the cluster
167        const char * name;
168
169        // Preemption rate on this cluster
170        Duration preemption_rate;
171
172        // List of processors
173        __spinlock_t proc_list_lock;
174        __dllist_t(struct processor) procs;
175        __dllist_t(struct processor) idles;
176
177        // List of processors
178        __spinlock_t thread_list_lock;
179        __dllist_t(struct thread_desc) threads;
180
181        // Link lists fields
182        struct {
183                cluster * next;
184                cluster * prev;
185        } node;
186};
187extern Duration default_preemption();
188
189void ?{} (cluster & this, const char * name, Duration preemption_rate);
190void ^?{}(cluster & this);
191
192static inline void ?{} (cluster & this)                           { this{"Anonymous Cluster", default_preemption()}; }
193static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; }
194static inline void ?{} (cluster & this, const char * name)        { this{name, default_preemption()}; }
195
196static inline [cluster *&, cluster *& ] __get( cluster & this ) {
197        return this.node.[next, prev];
198}
199
200// Local Variables: //
201// mode: c //
202// tab-width: 4 //
203// End: //
Note: See TracBrowser for help on using the repository browser.