source: src/libcfa/concurrency/kernel.c @ eeaea53

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 eeaea53 was 77e6fcb, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Removed trailing semi-colon on thread/coroutine declaration macro

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