source: src/libcfa/concurrency/kernel.c @ 17af7d1

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 17af7d1 was 17af7d1, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Some clean-up of runtime code

  • Property mode set to 100644
File size: 12.9 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 2017
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count     : 0
15//
16
17//Start and stop routine for the kernel, declared first to make sure they run first
18void kernel_startup(void)  __attribute__((constructor(101)));
19void kernel_shutdown(void) __attribute__((destructor(101)));
20
21//Header
22#include "kernel_private.h"
23
24//C Includes
25#include <stddef.h>
26extern "C" {
27#include <fenv.h>
28#include <sys/resource.h>
29}
30
31//CFA Includes
32#include "libhdr.h"
33
34//Private includes
35#define __CFA_INVOKE_PRIVATE__
36#include "invoke.h"
37
38//-----------------------------------------------------------------------------
39// Kernel storage
40#define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)]
41
42KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
43KERNEL_STORAGE(cluster, systemCluster);
44KERNEL_STORAGE(processor, systemProcessor);
45KERNEL_STORAGE(thread_desc, mainThread);
46KERNEL_STORAGE(machine_context_t, mainThread_context);
47
48cluster * systemCluster;
49processor * systemProcessor;
50thread_desc * mainThread;
51
52//-----------------------------------------------------------------------------
53// Global state
54
55thread_local processor * this_processor;
56
57coroutine_desc * this_coroutine(void) {
58        return this_processor->current_coroutine;
59}
60
61thread_desc * this_thread(void) {
62        return this_processor->current_thread;
63}
64
65//-----------------------------------------------------------------------------
66// Main thread construction
67struct current_stack_info_t {
68        machine_context_t ctx; 
69        unsigned int size;              // size of stack
70        void *base;                             // base of stack
71        void *storage;                  // pointer to stack
72        void *limit;                    // stack grows towards stack limit
73        void *context;                  // address of cfa_context_t
74        void *top;                              // address of top of storage
75};
76
77void ?{}( current_stack_info_t * this ) {
78        CtxGet( &this->ctx );
79        this->base = this->ctx.FP;
80        this->storage = this->ctx.SP;
81
82        rlimit r;
83        getrlimit( RLIMIT_STACK, &r);
84        this->size = r.rlim_cur;
85
86        this->limit = (void *)(((intptr_t)this->base) - this->size);
87        this->context = &mainThread_context_storage;
88        this->top = this->base;
89}
90
91void ?{}( coStack_t * this, current_stack_info_t * info) {
92        this->size = info->size;
93        this->storage = info->storage;
94        this->limit = info->limit;
95        this->base = info->base;
96        this->context = info->context;
97        this->top = info->top;
98        this->userStack = true;
99}
100
101void ?{}( coroutine_desc * this, current_stack_info_t * info) {
102        (&this->stack){ info }; 
103        this->name = "Main Thread";
104        this->errno_ = 0;
105        this->state = Start;
106}
107
108void ?{}( thread_desc * this, current_stack_info_t * info) {
109        (&this->cor){ info };
110}
111
112//-----------------------------------------------------------------------------
113// Processor coroutine
114void ?{}(processorCtx_t * this, processor * proc) {
115        (&this->__cor){};
116        this->proc = proc;
117        proc->runner = this;
118}
119
120void ?{}(processorCtx_t * this, processor * proc, current_stack_info_t * info) {
121        (&this->__cor){ info };
122        this->proc = proc;
123        proc->runner = this;
124}
125
126void ?{}(processor * this) {
127        this{ systemCluster };
128}
129
130void ?{}(processor * this, cluster * cltr) {
131        this->cltr = cltr;
132        this->current_coroutine = NULL;
133        this->current_thread = NULL;
134        (&this->terminated){};
135        this->is_terminated = false;
136
137        start( this );
138}
139
140void ?{}(processor * this, cluster * cltr, processorCtx_t * runner) {
141        this->cltr = cltr;
142        this->current_coroutine = NULL;
143        this->current_thread = NULL;
144        (&this->terminated){};
145        this->is_terminated = false;
146
147        this->runner = runner;
148        LIB_DEBUG_PRINTF("Kernel : constructing processor context %p\n", runner);
149        runner{ this };
150}
151
152void ^?{}(processor * this) {
153        if( ! this->is_terminated ) {
154                LIB_DEBUG_PRINTF("Kernel : core %p signaling termination\n", this);
155                this->is_terminated = true;
156                wait( &this->terminated );
157        }
158}
159
160void ?{}(cluster * this) {
161        ( &this->ready_queue ){};
162        ( &this->lock ){};
163}
164
165void ^?{}(cluster * this) {
166       
167}
168
169//=============================================================================================
170// Kernel Scheduling logic
171//=============================================================================================
172//Main of the processor contexts
173void main(processorCtx_t * runner) {
174        processor * this = runner->proc;
175        LIB_DEBUG_PRINTF("Kernel : core %p starting\n", this);
176
177        thread_desc * readyThread = NULL;
178        for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ ) 
179        {
180                readyThread = nextThread( this->cltr );
181
182                if(readyThread) 
183                {
184                        runThread(this, readyThread);
185
186                        //Some actions need to be taken from the kernel
187                        finishRunning(this);
188
189                        spin_count = 0;
190                } 
191                else 
192                {
193                        spin(this, &spin_count);
194                }               
195        }
196
197        LIB_DEBUG_PRINTF("Kernel : core %p unlocking thread\n", this);
198        signal( &this->terminated );
199        LIB_DEBUG_PRINTF("Kernel : core %p terminated\n", this);
200}
201
202// runThread runs a thread by context switching
203// from the processor coroutine to the target thread
204void runThread(processor * this, thread_desc * dst) {
205        coroutine_desc * proc_cor = get_coroutine(this->runner);
206        coroutine_desc * thrd_cor = get_coroutine(dst);
207       
208        //Reset the terminating actions here
209        this->finish.action_code = No_Action;
210
211        //Update global state
212        this->current_thread = dst;
213
214        // Context Switch to the thread
215        ThreadCtxSwitch(proc_cor, thrd_cor);
216        // when ThreadCtxSwitch returns we are back in the processor coroutine
217}
218
219// Once a thread has finished running, some of
220// its final actions must be executed from the kernel
221void finishRunning(processor * this) {
222        if( this->finish.action_code == Release ) {
223                unlock( this->finish.lock );
224        }
225        else if( this->finish.action_code == Schedule ) {
226                ScheduleThread( this->finish.thrd );
227        }
228        else if( this->finish.action_code == Release_Schedule ) {
229                unlock( this->finish.lock );           
230                ScheduleThread( this->finish.thrd );
231        }
232        else {
233                assert(this->finish.action_code == No_Action);
234        }
235}
236
237// Handles spinning logic
238// TODO : find some strategy to put cores to sleep after some time
239void spin(processor * this, unsigned int * spin_count) {
240        (*spin_count)++;
241}
242
243// Context invoker for processors
244// This is the entry point for processors (kernel threads)
245// It effectively constructs a coroutine by stealing the pthread stack
246void * CtxInvokeProcessor(void * arg) {
247        processor * proc = (processor *) arg;
248        this_processor = proc;
249        // SKULLDUGGERY: We want to create a context for the processor coroutine
250        // which is needed for the 2-step context switch. However, there is no reason
251        // to waste the perfectly valid stack create by pthread.
252        current_stack_info_t info;
253        machine_context_t ctx;
254        info.context = &ctx;
255        processorCtx_t proc_cor_storage = { proc, &info };
256
257        LIB_DEBUG_PRINTF("Coroutine : created stack %p\n", proc_cor_storage.__cor.stack.base);
258
259        //Set global state
260        proc->current_coroutine = &proc->runner->__cor;
261        proc->current_thread = NULL;
262
263        //We now have a proper context from which to schedule threads
264        LIB_DEBUG_PRINTF("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
265
266        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
267        // resume it to start it like it normally would, it will just context switch
268        // back to here. Instead directly call the main since we already are on the
269        // appropriate stack.
270        proc_cor_storage.__cor.state = Active;
271      main( &proc_cor_storage );
272      proc_cor_storage.__cor.state = Halted;
273
274        // Main routine of the core returned, the core is now fully terminated
275        LIB_DEBUG_PRINTF("Kernel : core %p main ended (%p)\n", proc, proc->runner);     
276
277        return NULL;
278}
279
280void start(processor * this) {
281        LIB_DEBUG_PRINTF("Kernel : Starting core %p\n", this);
282       
283        // pthread_attr_t attributes;
284        // pthread_attr_init( &attributes );
285
286        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
287
288        // pthread_attr_destroy( &attributes );
289
290        LIB_DEBUG_PRINTF("Kernel : core %p started\n", this);   
291}
292
293//-----------------------------------------------------------------------------
294// Scheduler routines
295void ScheduleThread( thread_desc * thrd ) {
296        assertf( thrd->next == NULL, "Expected null got %p", thrd->next );
297       
298        lock( &systemProcessor->cltr->lock );
299        append( &systemProcessor->cltr->ready_queue, thrd );
300        unlock( &systemProcessor->cltr->lock );
301}
302
303thread_desc * nextThread(cluster * this) {
304        lock( &this->lock );
305        thread_desc * head = pop_head( &this->ready_queue );
306        unlock( &this->lock );
307        return head;
308}
309
310void ScheduleInternal() {
311        suspend();
312}
313
314void ScheduleInternal( spinlock * lock ) {
315        this_processor->finish.action_code = Release;
316        this_processor->finish.lock = lock;
317        suspend();
318}
319
320void ScheduleInternal( thread_desc * thrd ) {
321        this_processor->finish.action_code = Schedule;
322        this_processor->finish.thrd = thrd;
323        suspend();
324}
325
326void ScheduleInternal( spinlock * lock, thread_desc * thrd ) {
327        this_processor->finish.action_code = Release_Schedule;
328        this_processor->finish.lock = lock;
329        this_processor->finish.thrd = thrd;
330        suspend();
331}
332
333//-----------------------------------------------------------------------------
334// Kernel boot procedures
335void kernel_startup(void) {
336        LIB_DEBUG_PRINTF("Kernel : Starting\n");       
337
338        // Start by initializing the main thread
339        // SKULLDUGGERY: the mainThread steals the process main thread
340        // which will then be scheduled by the systemProcessor normally
341        mainThread = (thread_desc *)&mainThread_storage;
342        current_stack_info_t info;
343        mainThread{ &info };
344
345        // Initialize the system cluster
346        systemCluster = (cluster *)&systemCluster_storage;
347        systemCluster{};
348
349        // Initialize the system processor and the system processor ctx
350        // (the coroutine that contains the processing control flow)
351        systemProcessor = (processor *)&systemProcessor_storage;
352        systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
353
354        // Add the main thread to the ready queue
355        // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread
356        ScheduleThread(mainThread);
357
358        //initialize the global state variables
359        this_processor = systemProcessor;
360        this_processor->current_thread = mainThread;
361        this_processor->current_coroutine = &mainThread->cor;
362
363        // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
364        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
365        // mainThread is on the ready queue when this call is made.
366        resume(systemProcessor->runner);
367
368
369
370        // THE SYSTEM IS NOW COMPLETELY RUNNING
371        LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n");
372}
373
374void kernel_shutdown(void) {
375        LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down\n");
376
377        // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
378        // When its coroutine terminates, it return control to the mainThread
379        // which is currently here
380        systemProcessor->is_terminated = true;
381        suspend();
382
383        // THE SYSTEM IS NOW COMPLETELY STOPPED
384
385        // Destroy the system processor and its context in reverse order of construction
386        // These were manually constructed so we need manually destroy them
387        ^(systemProcessor->runner){};
388        ^(systemProcessor){};
389
390        // Final step, destroy the main thread since it is no longer needed
391        // Since we provided a stack to this taxk it will not destroy anything
392        ^(mainThread){};
393
394        LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n");       
395}
396
397//-----------------------------------------------------------------------------
398// Locks
399void ?{}( spinlock * this ) {
400        this->lock = 0;
401}
402void ^?{}( spinlock * this ) {
403
404}
405
406void lock( spinlock * this ) {
407        for ( unsigned int i = 1;; i += 1 ) {
408                if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) break;
409        }
410}
411
412void unlock( spinlock * this ) {
413        __sync_lock_release_4( &this->lock );
414}
415
416void ?{}( signal_once * this ) {
417        this->condition = false;
418}
419void ^?{}( signal_once * this ) {
420
421}
422
423void wait( signal_once * this ) {
424        lock( &this->lock );
425        if( !this->condition ) {
426                append( &this->blocked, this_thread() );
427                ScheduleInternal( &this->lock );
428                lock( &this->lock );
429        }
430        unlock( &this->lock );
431}
432
433void signal( signal_once * this ) {
434        lock( &this->lock );
435        {
436                this->condition = true;
437
438                thread_desc * it;
439                while( it = pop_head( &this->blocked) ) {
440                        ScheduleThread( it );
441                }
442        }
443        unlock( &this->lock );
444}
445
446//-----------------------------------------------------------------------------
447// Queues
448void ?{}( simple_thread_list * this ) {
449        this->head = NULL;
450        this->tail = &this->head;
451}
452
453void append( simple_thread_list * this, thread_desc * t ) {
454        assert(this->tail != NULL);
455        *this->tail = t;
456        this->tail = &t->next;
457}
458
459thread_desc * pop_head( simple_thread_list * this ) {
460        thread_desc * head = this->head;
461        if( head ) {
462                this->head = head->next;
463                if( !head->next ) {
464                        this->tail = &this->head;
465                }
466                head->next = NULL;
467        }       
468        return head;
469}
470// Local Variables: //
471// mode: c //
472// tab-width: 4 //
473// End: //
Note: See TracBrowser for help on using the repository browser.