source: src/libcfa/concurrency/kernel.c @ 72dc82a

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 72dc82a was 690f13c, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

First step in implementing internal scheduling

  • Property mode set to 100644
File size: 13.2 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        if( !thrd ) return;
297
298        assertf( thrd->next == NULL, "Expected null got %p", thrd->next );
299       
300        lock( &systemProcessor->cltr->lock );
301        append( &systemProcessor->cltr->ready_queue, thrd );
302        unlock( &systemProcessor->cltr->lock );
303}
304
305thread_desc * nextThread(cluster * this) {
306        lock( &this->lock );
307        thread_desc * head = pop_head( &this->ready_queue );
308        unlock( &this->lock );
309        return head;
310}
311
312void ScheduleInternal() {
313        suspend();
314}
315
316void ScheduleInternal( spinlock * lock ) {
317        this_processor->finish.action_code = Release;
318        this_processor->finish.lock = lock;
319        suspend();
320}
321
322void ScheduleInternal( thread_desc * thrd ) {
323        this_processor->finish.action_code = Schedule;
324        this_processor->finish.thrd = thrd;
325        suspend();
326}
327
328void ScheduleInternal( spinlock * lock, thread_desc * thrd ) {
329        this_processor->finish.action_code = Release_Schedule;
330        this_processor->finish.lock = lock;
331        this_processor->finish.thrd = thrd;
332        suspend();
333}
334
335//-----------------------------------------------------------------------------
336// Kernel boot procedures
337void kernel_startup(void) {
338        LIB_DEBUG_PRINTF("Kernel : Starting\n");       
339
340        // Start by initializing the main thread
341        // SKULLDUGGERY: the mainThread steals the process main thread
342        // which will then be scheduled by the systemProcessor normally
343        mainThread = (thread_desc *)&mainThread_storage;
344        current_stack_info_t info;
345        mainThread{ &info };
346
347        // Initialize the system cluster
348        systemCluster = (cluster *)&systemCluster_storage;
349        systemCluster{};
350
351        // Initialize the system processor and the system processor ctx
352        // (the coroutine that contains the processing control flow)
353        systemProcessor = (processor *)&systemProcessor_storage;
354        systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
355
356        // Add the main thread to the ready queue
357        // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread
358        ScheduleThread(mainThread);
359
360        //initialize the global state variables
361        this_processor = systemProcessor;
362        this_processor->current_thread = mainThread;
363        this_processor->current_coroutine = &mainThread->cor;
364
365        // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
366        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
367        // mainThread is on the ready queue when this call is made.
368        resume(systemProcessor->runner);
369
370
371
372        // THE SYSTEM IS NOW COMPLETELY RUNNING
373        LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n");
374}
375
376void kernel_shutdown(void) {
377        LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down\n");
378
379        // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
380        // When its coroutine terminates, it return control to the mainThread
381        // which is currently here
382        systemProcessor->is_terminated = true;
383        suspend();
384
385        // THE SYSTEM IS NOW COMPLETELY STOPPED
386
387        // Destroy the system processor and its context in reverse order of construction
388        // These were manually constructed so we need manually destroy them
389        ^(systemProcessor->runner){};
390        ^(systemProcessor){};
391
392        // Final step, destroy the main thread since it is no longer needed
393        // Since we provided a stack to this taxk it will not destroy anything
394        ^(mainThread){};
395
396        LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n");       
397}
398
399//-----------------------------------------------------------------------------
400// Locks
401void ?{}( spinlock * this ) {
402        this->lock = 0;
403}
404void ^?{}( spinlock * this ) {
405
406}
407
408void lock( spinlock * this ) {
409        for ( unsigned int i = 1;; i += 1 ) {
410                if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) break;
411        }
412}
413
414void unlock( spinlock * this ) {
415        __sync_lock_release_4( &this->lock );
416}
417
418void ?{}( signal_once * this ) {
419        this->condition = false;
420}
421void ^?{}( signal_once * this ) {
422
423}
424
425void wait( signal_once * this ) {
426        lock( &this->lock );
427        if( !this->condition ) {
428                append( &this->blocked, this_thread() );
429                ScheduleInternal( &this->lock );
430                lock( &this->lock );
431        }
432        unlock( &this->lock );
433}
434
435void signal( signal_once * this ) {
436        lock( &this->lock );
437        {
438                this->condition = true;
439
440                thread_desc * it;
441                while( it = pop_head( &this->blocked) ) {
442                        ScheduleThread( it );
443                }
444        }
445        unlock( &this->lock );
446}
447
448//-----------------------------------------------------------------------------
449// Queues
450void ?{}( simple_thread_list * this ) {
451        this->head = NULL;
452        this->tail = &this->head;
453}
454
455void append( simple_thread_list * this, thread_desc * t ) {
456        assert(this->tail != NULL);
457        *this->tail = t;
458        this->tail = &t->next;
459}
460
461thread_desc * pop_head( simple_thread_list * this ) {
462        thread_desc * head = this->head;
463        if( head ) {
464                this->head = head->next;
465                if( !head->next ) {
466                        this->tail = &this->head;
467                }
468                head->next = NULL;
469        }       
470        return head;
471}
472
473void ?{}( simple_thread_stack * this ) {
474        this->top = NULL;
475}
476
477void push( simple_thread_stack * this, thread_desc * t ) {
478        assert(t->next != NULL);
479        t->next = this->top;
480        this->top = t;
481}
482
483thread_desc * pop( simple_thread_stack * this ) {
484        thread_desc * top = this->top;
485        if( top ) {
486                this->top = top->next;
487                top->next = NULL;
488        }       
489        return top;
490}
491// Local Variables: //
492// mode: c //
493// tab-width: 4 //
494// End: //
Note: See TracBrowser for help on using the repository browser.