source: src/libcfa/concurrency/kernel.c @ 8f49a54

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 8f49a54 was 8f49a54, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Clean-up thread, kernel and examples

  • Property mode set to 100644
File size: 7.4 KB
Line 
1//                              -*- Mode: CFA -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// kernel.c --
9//
10// Author           : Thierry Delisle
11// Created On       : Tue Jan 17 12:27:26 2016
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count     : 0
15//
16
17//Header
18#include "kernel"
19
20//C Includes
21#include <stddef.h>
22extern "C" {
23#include <sys/resource.h>
24}
25
26//CFA Includes
27#include "libhdr.h"
28#include "threads"
29
30//Private includes
31#define __CFA_INVOKE_PRIVATE__
32#include "invoke.h"
33
34processor * systemProcessor;
35thread_h * mainThread;
36
37void kernel_startup(void)  __attribute__((constructor(101)));
38void kernel_shutdown(void) __attribute__((destructor(101)));
39
40void ?{}(processor * this) {
41        this->ctx = NULL;
42        this->thread_index = 0;
43        this->thread_count = 10;
44        this->terminated = false;
45
46        for(int i = 0; i < 10; i++) {
47                this->threads[i] = NULL;
48        }
49
50        LIB_DEBUG_PRINTF("Processor : ctor for core %p (core spots %d)\n", this, this->thread_count);
51}
52
53void ^?{}(processor * this) {
54
55}
56
57//-----------------------------------------------------------------------------
58// Processor coroutine
59struct processorCtx_t {
60        processor * proc;
61        coroutine c;
62};
63
64DECL_COROUTINE(processorCtx_t)
65
66void ?{}(processorCtx_t * this, processor * proc) {
67        (&this->c){};
68        this->proc = proc;
69}
70
71void CtxInvokeProcessor(processor * proc) {
72        processorCtx_t proc_cor_storage = {proc};
73        resume( &proc_cor_storage );
74}
75
76//-----------------------------------------------------------------------------
77// Processor running routines
78void main(processorCtx_t * ctx);
79thread_h * nextThread(processor * this);
80void runThread(processor * this, thread_h * dst);
81void spin(processor * this, unsigned int * spin_count);
82
83void main(processorCtx_t * ctx) {
84        processor * this = ctx->proc;
85        LIB_DEBUG_PRINTF("Kernel : core %p starting\n", this);
86
87        thread_h * readyThread = NULL;
88        for( unsigned int spin_count = 0; ! this->terminated; spin_count++ ) {
89               
90                readyThread = nextThread(this);
91
92                if(readyThread) {
93                        runThread(this, readyThread);
94                        spin_count = 0;
95                } else {
96                        spin(this, &spin_count);
97                }               
98        }
99
100        LIB_DEBUG_PRINTF("Kernel : core %p terminated\n", this);
101}
102
103thread_h * nextThread(processor * this) {
104        for(int i = 0; i < this->thread_count; i++) {
105                this->thread_index = (this->thread_index + 1) % this->thread_count;     
106               
107                thread_h * thrd = this->threads[this->thread_index];
108                if(thrd) return thrd;
109        }
110
111        return NULL;
112}
113
114void runThread(processor * this, thread_h * dst) {
115        coroutine * proc_ctx = get_coroutine(this->ctx);
116        coroutine * thrd_ctx = get_coroutine(dst);
117        thrd_ctx->last = proc_ctx;
118
119        // context switch to specified coroutine
120        // Which is now the current_coroutine
121        // LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
122        current_coroutine = thrd_ctx;
123        CtxSwitch( proc_ctx->stack.context, thrd_ctx->stack.context );
124        current_coroutine = proc_ctx;
125        // LIB_DEBUG_PRINTF("Kernel : returned from ctx %p (to %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
126
127        // when CtxSwitch returns we are back in the processor coroutine
128}
129
130void spin(processor * this, unsigned int * spin_count) {
131        (*spin_count)++;
132}
133
134//-----------------------------------------------------------------------------
135// Kernel runner (Temporary)
136
137void scheduler_add( thread_h * thrd ) {
138        for(int i = 0; i < systemProcessor->thread_count; i++) {
139                if(systemProcessor->threads[i] == NULL) {
140                        systemProcessor->threads[i] = thrd;
141                        return;
142                }
143        }
144        assertf(false, "Scheduler full");
145}
146
147void scheduler_remove( thread_h * thrd ) {
148        for(int i = 0; i < systemProcessor->thread_count; i++) {
149                if(systemProcessor->threads[i] == thrd) {
150                        systemProcessor->threads[i] = NULL;
151                        return;
152                }
153        }
154        assertf(false, "Trying to unschedule unkown thread");
155}
156
157//-----------------------------------------------------------------------------
158// Kernel storage
159#define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)]
160
161KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
162KERNEL_STORAGE(processor, systemProcessor);
163KERNEL_STORAGE(thread_h, mainThread);
164KERNEL_STORAGE(machine_context_t, mainThread_context);
165
166//-----------------------------------------------------------------------------
167// Main thread construction
168struct mainThread_info_t {
169        machine_context_t ctx; 
170        unsigned int size;              // size of stack
171        void *base;                             // base of stack
172        void *storage;                  // pointer to stack
173        void *limit;                    // stack grows towards stack limit
174        void *context;                  // address of cfa_context_t
175        void *top;                              // address of top of storage
176};
177
178void ?{}( mainThread_info_t * this ) {
179        CtxGet( &this->ctx );
180        this->base = this->ctx.FP;
181        this->storage = this->ctx.SP;
182
183        rlimit r;
184        int ret = getrlimit( RLIMIT_STACK, &r);
185        this->size = r.rlim_cur;
186
187        this->limit = (void *)(((intptr_t)this->base) - this->size);
188        this->context = &mainThread_context_storage;
189        this->top = this->base;
190}
191
192void ?{}( coStack_t * this, mainThread_info_t * info) {
193        this->size = info->size;
194        this->storage = info->storage;
195        this->limit = info->limit;
196        this->base = info->base;
197        this->context = info->context;
198        this->top = info->top;
199        this->userStack = true;
200}
201
202void ?{}( coroutine * this, mainThread_info_t * info) {
203        (&this->stack){ info }; 
204        this->name = "Main Thread";
205        this->errno_ = 0;
206        this->state = Inactive;
207        this->notHalted = true;
208}
209
210void ?{}( thread_h * this, mainThread_info_t * info) {
211        (&this->c){ info };
212}
213
214//-----------------------------------------------------------------------------
215// Kernel boot procedures
216void kernel_startup(void) {
217
218        // SKULLDUGGERY: the mainThread steals the process main thread
219        // which will then be scheduled by the systemProcessor normally
220        LIB_DEBUG_PRINTF("Kernel : Starting\n");       
221
222        mainThread_info_t ctx;
223        // LIB_DEBUG_PRINTF("Kernel :    base : %p\n", ctx.base );
224        // LIB_DEBUG_PRINTF("Kernel :     top : %p\n", ctx.top );
225        // LIB_DEBUG_PRINTF("Kernel :   limit : %p\n", ctx.limit );
226        // LIB_DEBUG_PRINTF("Kernel :    size : %x\n", ctx.size );
227        // LIB_DEBUG_PRINTF("Kernel : storage : %p\n", ctx.storage );
228        // LIB_DEBUG_PRINTF("Kernel : context : %p\n", ctx.context );
229
230        // Start by initializing the main thread
231        mainThread = (thread_h *)&mainThread_storage;
232        LIB_DEBUG_PRINTF("Kernel : Main thread : %p\n", mainThread );
233        mainThread{ &ctx };
234
235        // // Initialize the system processor
236        systemProcessor = (processor *)&systemProcessor_storage;
237        systemProcessor{};
238
239        // Initialize the system processor ctx
240        // (the coroutine that contains the processing control flow)
241        systemProcessor->ctx = (processorCtx_t *)&systemProcessorCtx_storage;
242        systemProcessor->ctx{ systemProcessor };
243
244        scheduler_add(mainThread);
245
246        current_coroutine = &mainThread->c;
247
248        LIB_DEBUG_PRINTF("Kernel : Starting system processor\n");       
249        resume(systemProcessor->ctx);
250
251        LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n");
252}
253void kernel_shutdown(void) {
254        LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down");
255
256        LIB_DEBUG_PRINTF("Unscheduling main thread\n");
257        scheduler_remove(mainThread);
258
259        LIB_DEBUG_PRINTF("Kernel : Terminating system processor\n");           
260        systemProcessor->terminated = true;
261
262        suspend();
263
264        LIB_DEBUG_PRINTF("Kernel : Control returned to initial process thread\n");
265
266        ^(systemProcessor->ctx){};
267        ^(systemProcessor){};
268
269        ^(mainThread){};
270
271        LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n");       
272}
273
274// Local Variables: //
275// mode: c //
276// tab-width: 4 //
277// End: //
Note: See TracBrowser for help on using the repository browser.