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

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 8dbedfc was a1a17a7, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Moved thread list to cluster, all concurrency object should be accessible through gdb

  • Property mode set to 100644
File size: 22.9 KB
RevLine 
[8118303]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.c --
8//
9// Author           : Thierry Delisle
[75f3522]10// Created On       : Tue Jan 17 12:27:26 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
[0f56058]12// Last Modified On : Mon Apr  9 16:11:46 2018
13// Update Count     : 24
[8118303]14//
15
16//C Includes
[c84e80a]17#include <stddef.h>
[eb2e723]18extern "C" {
[9d944b2]19#include <stdio.h>
[8fcbb4c]20#include <fenv.h>
[eb2e723]21#include <sys/resource.h>
[9d944b2]22#include <signal.h>
23#include <unistd.h>
[eb2e723]24}
[8118303]25
26//CFA Includes
[0f56058]27#include "time"
[2ac095d]28#include "kernel_private.h"
[c81ebf9]29#include "preemption.h"
[2ac095d]30#include "startup.h"
[8118303]31
32//Private includes
33#define __CFA_INVOKE_PRIVATE__
34#include "invoke.h"
35
[2ac095d]36//Start and stop routine for the kernel, declared first to make sure they run first
37void kernel_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
38void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
39
[8def349]40//-----------------------------------------------------------------------------
41// Kernel storage
[969b3fe]42KERNEL_STORAGE(cluster,           mainCluster);
43KERNEL_STORAGE(processor,         mainProcessor);
44KERNEL_STORAGE(thread_desc,       mainThread);
[f2b12406]45KERNEL_STORAGE(machine_context_t, mainThreadCtx);
[8def349]46
[de6319f]47cluster     * mainCluster;
48processor   * mainProcessor;
[348006f]49thread_desc * mainThread;
[eb2e723]50
[de94a60]51struct { __dllist_t(cluster    ) list; __spinlock_t lock; } global_clusters;
52
[bd98b58]53//-----------------------------------------------------------------------------
54// Global state
[14a61b5]55thread_local struct KernelThreadData kernelTLS = {
[b10affd]56        NULL,
57        NULL,
58        NULL,
59        { 1, false, false }
60};
[c84e80a]61
62//-----------------------------------------------------------------------------
[de6319f]63// Struct to steal stack
[8def349]64struct current_stack_info_t {
[1c273d0]65        machine_context_t ctx;
[8def349]66        unsigned int size;              // size of stack
67        void *base;                             // base of stack
68        void *storage;                  // pointer to stack
69        void *limit;                    // stack grows towards stack limit
70        void *context;                  // address of cfa_context_t
71        void *top;                              // address of top of storage
[c84e80a]72};
73
[242a902]74void ?{}( current_stack_info_t & this ) {
75        CtxGet( this.ctx );
76        this.base = this.ctx.FP;
77        this.storage = this.ctx.SP;
[8def349]78
79        rlimit r;
[132fad4]80        getrlimit( RLIMIT_STACK, &r);
[242a902]81        this.size = r.rlim_cur;
[8def349]82
[242a902]83        this.limit = (void *)(((intptr_t)this.base) - this.size);
[9236060]84        this.context = &storage_mainThreadCtx;
[242a902]85        this.top = this.base;
[8def349]86}
87
[de6319f]88//-----------------------------------------------------------------------------
89// Main thread construction
[65deb18]90void ?{}( coStack_t & this, current_stack_info_t * info) with( this ) {
91        size      = info->size;
92        storage   = info->storage;
93        limit     = info->limit;
94        base      = info->base;
95        context   = info->context;
96        top       = info->top;
97        userStack = true;
[8def349]98}
99
[65deb18]100void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) {
101        stack{ info };
102        name = "Main Thread";
103        errno_ = 0;
104        state = Start;
105        starter = NULL;
[8def349]106}
107
[65deb18]108void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) {
109        self_cor{ info };
[82c948c]110        curr_cor = &self_cor;
[de6319f]111        curr_cluster = mainCluster;
[82c948c]112        self_mon.owner = &this;
113        self_mon.recursion = 1;
114        self_mon_p = &self_mon;
115        next = NULL;
[de94a60]116
117        node.next = NULL;
118        node.prev = NULL;
[a1a17a7]119        doregister(curr_cluster, this);
[82c948c]120
121        monitors{ &self_mon_p, 1, (fptr_t)0 };
[8def349]122}
[c84e80a]123
[8def349]124//-----------------------------------------------------------------------------
125// Processor coroutine
[de6319f]126void ?{}(processorCtx_t & this) {
[39fea2f]127
[8def349]128}
129
[39fea2f]130// Construct the processor context of non-main processors
[242a902]131void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
132        (this.__cor){ info };
133        this.proc = proc;
[8def349]134}
135
[de6319f]136void ?{}(processor & this, const char * name, cluster & cltr) with( this ) {
137        this.name = name;
138        this.cltr = &cltr;
[c40e7c5]139        terminated{ 0 };
140        do_terminate = false;
141        preemption_alarm = NULL;
142        pending_preemption = false;
[094476d]143        runner.proc = &this;
[8def349]144
[242a902]145        start( &this );
[c84e80a]146}
147
[65deb18]148void ^?{}(processor & this) with( this ){
149        if( ! do_terminate ) {
[36982fc]150                __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
[4dad189]151                terminate(&this);
[1f37ed02]152                verify(this.do_terminate);
[14a61b5]153                verify( kernelTLS.this_processor != &this);
[65deb18]154                P( terminated );
[14a61b5]155                verify( kernelTLS.this_processor != &this);
[65deb18]156                pthread_join( kernel_thread, NULL );
[8def349]157        }
158}
159
[de6319f]160void ?{}(cluster & this, const char * name, Duration preemption_rate) with( this ) {
161        this.name = name;
162        this.preemption_rate = preemption_rate;
[65deb18]163        ready_queue{};
164        ready_queue_lock{};
[de94a60]165
166        procs{ __get };
167        idles{ __get };
[a1a17a7]168        threads{ __get };
[de94a60]169
170        doregister(this);
[8def349]171}
172
[242a902]173void ^?{}(cluster & this) {
[de94a60]174        unregister(this);
[c84e80a]175}
176
[75f3522]177//=============================================================================================
178// Kernel Scheduling logic
179//=============================================================================================
[8fcbb4c]180//Main of the processor contexts
[83a071f9]181void main(processorCtx_t & runner) {
182        processor * this = runner.proc;
[094476d]183        verify(this);
[c81ebf9]184
[36982fc]185        __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
[8118303]186
[de94a60]187        doregister(this->cltr, this);
188
[75f3522]189        {
[c81ebf9]190                // Setup preemption data
191                preemption_scope scope = { this };
192
[36982fc]193                __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
[8118303]194
[c81ebf9]195                thread_desc * readyThread = NULL;
[e60e0dc]196                for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
[75f3522]197                {
[c81ebf9]198                        readyThread = nextThread( this->cltr );
[75f3522]199
[c81ebf9]200                        if(readyThread)
201                        {
[14a61b5]202                                verify( ! kernelTLS.preemption_state.enabled );
[4e6fb8e]203
[c81ebf9]204                                runThread(this, readyThread);
[75f3522]205
[14a61b5]206                                verify( ! kernelTLS.preemption_state.enabled );
[4e6fb8e]207
[c81ebf9]208                                //Some actions need to be taken from the kernel
209                                finishRunning(this);
210
211                                spin_count = 0;
212                        }
213                        else
214                        {
215                                spin(this, &spin_count);
216                        }
217                }
218
[36982fc]219                __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
[c84e80a]220        }
[8118303]221
[de94a60]222        unregister(this->cltr, this);
223
[4cedd9f]224        V( this->terminated );
[bdeba0b]225
[36982fc]226        __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
[c84e80a]227}
228
[14a61b5]229// KERNEL ONLY
[1c273d0]230// runThread runs a thread by context switching
231// from the processor coroutine to the target thread
[348006f]232void runThread(processor * this, thread_desc * dst) {
[82c948c]233        assert(dst->curr_cor);
[094476d]234        coroutine_desc * proc_cor = get_coroutine(this->runner);
[82c948c]235        coroutine_desc * thrd_cor = dst->curr_cor;
[1c273d0]236
[14a61b5]237        // Reset the terminating actions here
[db6f06a]238        this->finish.action_code = No_Action;
[8fcbb4c]239
[14a61b5]240        // Update global state
241        kernelTLS.this_thread = dst;
[75f3522]242
243        // Context Switch to the thread
244        ThreadCtxSwitch(proc_cor, thrd_cor);
245        // when ThreadCtxSwitch returns we are back in the processor coroutine
246}
247
[14a61b5]248// KERNEL_ONLY
[82c948c]249void returnToKernel() {
[14a61b5]250        coroutine_desc * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
251        coroutine_desc * thrd_cor = kernelTLS.this_thread->curr_cor = kernelTLS.this_coroutine;
[82c948c]252        ThreadCtxSwitch(thrd_cor, proc_cor);
253}
254
[14a61b5]255// KERNEL_ONLY
[1c273d0]256// Once a thread has finished running, some of
[75f3522]257// its final actions must be executed from the kernel
[65deb18]258void finishRunning(processor * this) with( this->finish ) {
259        if( action_code == Release ) {
[14a61b5]260                verify( ! kernelTLS.preemption_state.enabled );
[65deb18]261                unlock( *lock );
[db6f06a]262        }
[65deb18]263        else if( action_code == Schedule ) {
264                ScheduleThread( thrd );
[db6f06a]265        }
[65deb18]266        else if( action_code == Release_Schedule ) {
[14a61b5]267                verify( ! kernelTLS.preemption_state.enabled );
[65deb18]268                unlock( *lock );
269                ScheduleThread( thrd );
[db6f06a]270        }
[65deb18]271        else if( action_code == Release_Multi ) {
[14a61b5]272                verify( ! kernelTLS.preemption_state.enabled );
[65deb18]273                for(int i = 0; i < lock_count; i++) {
274                        unlock( *locks[i] );
[0c78741]275                }
276        }
[65deb18]277        else if( action_code == Release_Multi_Schedule ) {
278                for(int i = 0; i < lock_count; i++) {
279                        unlock( *locks[i] );
[0c78741]280                }
[65deb18]281                for(int i = 0; i < thrd_count; i++) {
282                        ScheduleThread( thrds[i] );
[0c78741]283                }
284        }
[db6f06a]285        else {
[65deb18]286                assert(action_code == No_Action);
[8fcbb4c]287        }
[c84e80a]288}
289
[0c92c9f]290// Handles spinning logic
291// TODO : find some strategy to put cores to sleep after some time
[c84e80a]292void spin(processor * this, unsigned int * spin_count) {
293        (*spin_count)++;
294}
295
[14a61b5]296// KERNEL_ONLY
[0c92c9f]297// Context invoker for processors
298// This is the entry point for processors (kernel threads)
299// It effectively constructs a coroutine by stealing the pthread stack
[8def349]300void * CtxInvokeProcessor(void * arg) {
301        processor * proc = (processor *) arg;
[14a61b5]302        kernelTLS.this_processor = proc;
303        kernelTLS.this_coroutine = NULL;
304        kernelTLS.this_thread    = NULL;
305        kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
[8def349]306        // SKULLDUGGERY: We want to create a context for the processor coroutine
307        // which is needed for the 2-step context switch. However, there is no reason
[1c273d0]308        // to waste the perfectly valid stack create by pthread.
[8def349]309        current_stack_info_t info;
310        machine_context_t ctx;
311        info.context = &ctx;
[094476d]312        (proc->runner){ proc, &info };
[8def349]313
[094476d]314        __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.base);
[8fcbb4c]315
[0c92c9f]316        //Set global state
[14a61b5]317        kernelTLS.this_coroutine = get_coroutine(proc->runner);
318        kernelTLS.this_thread    = NULL;
[8def349]319
320        //We now have a proper context from which to schedule threads
[094476d]321        __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
[8def349]322
[1c273d0]323        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
324        // resume it to start it like it normally would, it will just context switch
325        // back to here. Instead directly call the main since we already are on the
[8def349]326        // appropriate stack.
[094476d]327        get_coroutine(proc->runner)->state = Active;
328        main( proc->runner );
329        get_coroutine(proc->runner)->state = Halted;
[8def349]330
[0c92c9f]331        // Main routine of the core returned, the core is now fully terminated
[094476d]332        __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
[8def349]333
334        return NULL;
[c84e80a]335}
336
[8def349]337void start(processor * this) {
[36982fc]338        __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
[82ff5845]339
[8fcbb4c]340        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
[eb2e723]341
[36982fc]342        __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
[eb2e723]343}
344
[14a61b5]345// KERNEL_ONLY
[b69ea6b]346void kernel_first_resume(processor * this) {
[14a61b5]347        coroutine_desc * src = kernelTLS.this_coroutine;
[094476d]348        coroutine_desc * dst = get_coroutine(this->runner);
[b69ea6b]349
[14a61b5]350        verify( ! kernelTLS.preemption_state.enabled );
[b69ea6b]351
352        create_stack(&dst->stack, dst->stack.size);
[094476d]353        CtxStart(&this->runner, CtxInvokeCoroutine);
[b69ea6b]354
[14a61b5]355        verify( ! kernelTLS.preemption_state.enabled );
[b69ea6b]356
357        dst->last = src;
358        dst->starter = dst->starter ? dst->starter : src;
359
360        // set state of current coroutine to inactive
361        src->state = src->state == Halted ? Halted : Inactive;
362
363        // set new coroutine that task is executing
[14a61b5]364        kernelTLS.this_coroutine = dst;
[b69ea6b]365
366        // SKULLDUGGERY normally interrupts are enable before leaving a coroutine ctxswitch.
367        // Therefore, when first creating a coroutine, interrupts are enable before calling the main.
368        // This is consistent with thread creation. However, when creating the main processor coroutine,
369        // we wan't interrupts to be disabled. Therefore, we double-disable interrupts here so they will
370        // stay disabled.
371        disable_interrupts();
372
373        // context switch to specified coroutine
374        assert( src->stack.context );
375        CtxSwitch( src->stack.context, dst->stack.context );
376        // when CtxSwitch returns we are back in the src coroutine
377
378        // set state of new coroutine to active
379        src->state = Active;
380
[14a61b5]381        verify( ! kernelTLS.preemption_state.enabled );
[b69ea6b]382}
383
[8def349]384//-----------------------------------------------------------------------------
385// Scheduler routines
[14a61b5]386
387// KERNEL ONLY
[348006f]388void ScheduleThread( thread_desc * thrd ) {
[135b431]389        verify( thrd );
[b18830e]390        verify( thrd->self_cor.state != Halted );
[1c273d0]391
[14a61b5]392        verify( ! kernelTLS.preemption_state.enabled );
[690f13c]393
[4aa2fb2]394        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
[1c273d0]395
[de6319f]396        with( *thrd->curr_cluster ) {
[65deb18]397                lock  ( ready_queue_lock __cfaabi_dbg_ctx2 );
398                append( ready_queue, thrd );
399                unlock( ready_queue_lock );
400        }
[1c273d0]401
[14a61b5]402        verify( ! kernelTLS.preemption_state.enabled );
[db6f06a]403}
404
[14a61b5]405// KERNEL ONLY
[65deb18]406thread_desc * nextThread(cluster * this) with( *this ) {
[14a61b5]407        verify( ! kernelTLS.preemption_state.enabled );
[65deb18]408        lock( ready_queue_lock __cfaabi_dbg_ctx2 );
409        thread_desc * head = pop_head( ready_queue );
410        unlock( ready_queue_lock );
[14a61b5]411        verify( ! kernelTLS.preemption_state.enabled );
[db6f06a]412        return head;
[eb2e723]413}
414
[82ff5845]415void BlockInternal() {
416        disable_interrupts();
[14a61b5]417        verify( ! kernelTLS.preemption_state.enabled );
[82c948c]418        returnToKernel();
[14a61b5]419        verify( ! kernelTLS.preemption_state.enabled );
[36982fc]420        enable_interrupts( __cfaabi_dbg_ctx );
[75f3522]421}
422
[ea7d2b0]423void BlockInternal( __spinlock_t * lock ) {
[82ff5845]424        disable_interrupts();
[14a61b5]425        with( *kernelTLS.this_processor ) {
[de6319f]426                finish.action_code = Release;
427                finish.lock        = lock;
428        }
[0b33412]429
[afd550c]430        verify( ! kernelTLS.preemption_state.enabled );
[82c948c]431        returnToKernel();
[afd550c]432        verify( ! kernelTLS.preemption_state.enabled );
[0b33412]433
[36982fc]434        enable_interrupts( __cfaabi_dbg_ctx );
[db6f06a]435}
436
[82ff5845]437void BlockInternal( thread_desc * thrd ) {
438        disable_interrupts();
[14a61b5]439        with( * kernelTLS.this_processor ) {
[de6319f]440                finish.action_code = Schedule;
441                finish.thrd        = thrd;
442        }
[0b33412]443
[14a61b5]444        verify( ! kernelTLS.preemption_state.enabled );
[82c948c]445        returnToKernel();
[14a61b5]446        verify( ! kernelTLS.preemption_state.enabled );
[0b33412]447
[36982fc]448        enable_interrupts( __cfaabi_dbg_ctx );
[db6f06a]449}
450
[ea7d2b0]451void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
[97e3296]452        assert(thrd);
[82ff5845]453        disable_interrupts();
[14a61b5]454        with( * kernelTLS.this_processor ) {
[de6319f]455                finish.action_code = Release_Schedule;
456                finish.lock        = lock;
457                finish.thrd        = thrd;
458        }
[0b33412]459
[14a61b5]460        verify( ! kernelTLS.preemption_state.enabled );
[82c948c]461        returnToKernel();
[14a61b5]462        verify( ! kernelTLS.preemption_state.enabled );
[0b33412]463
[36982fc]464        enable_interrupts( __cfaabi_dbg_ctx );
[eb2e723]465}
466
[ea7d2b0]467void BlockInternal(__spinlock_t * locks [], unsigned short count) {
[82ff5845]468        disable_interrupts();
[14a61b5]469        with( * kernelTLS.this_processor ) {
[de6319f]470                finish.action_code = Release_Multi;
471                finish.locks       = locks;
472                finish.lock_count  = count;
473        }
[0b33412]474
[14a61b5]475        verify( ! kernelTLS.preemption_state.enabled );
[82c948c]476        returnToKernel();
[14a61b5]477        verify( ! kernelTLS.preemption_state.enabled );
[0b33412]478
[36982fc]479        enable_interrupts( __cfaabi_dbg_ctx );
[0c78741]480}
481
[ea7d2b0]482void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
[82ff5845]483        disable_interrupts();
[14a61b5]484        with( *kernelTLS.this_processor ) {
[de6319f]485                finish.action_code = Release_Multi_Schedule;
486                finish.locks       = locks;
487                finish.lock_count  = lock_count;
488                finish.thrds       = thrds;
489                finish.thrd_count  = thrd_count;
490        }
[0b33412]491
[14a61b5]492        verify( ! kernelTLS.preemption_state.enabled );
[82c948c]493        returnToKernel();
[14a61b5]494        verify( ! kernelTLS.preemption_state.enabled );
[0b33412]495
[36982fc]496        enable_interrupts( __cfaabi_dbg_ctx );
[0c78741]497}
498
[14a61b5]499// KERNEL ONLY
[ea7d2b0]500void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
[14a61b5]501        verify( ! kernelTLS.preemption_state.enabled );
502        with( * kernelTLS.this_processor ) {
[de6319f]503                finish.action_code = thrd ? Release_Schedule : Release;
504                finish.lock        = lock;
505                finish.thrd        = thrd;
506        }
[f2b12406]507
[82c948c]508        returnToKernel();
[f2b12406]509}
510
[fa21ac9]511//=============================================================================================
512// Kernel Setup logic
513//=============================================================================================
[eb2e723]514//-----------------------------------------------------------------------------
515// Kernel boot procedures
516void kernel_startup(void) {
[14a61b5]517        verify( ! kernelTLS.preemption_state.enabled );
[36982fc]518        __cfaabi_dbg_print_safe("Kernel : Starting\n");
[eb2e723]519
[de94a60]520        global_clusters.list{ __get };
521        global_clusters.lock{};
522
[de6319f]523        // Initialize the main cluster
524        mainCluster = (cluster *)&storage_mainCluster;
525        (*mainCluster){"Main Cluster"};
526
527        __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n");
528
[eb2e723]529        // Start by initializing the main thread
[1c273d0]530        // SKULLDUGGERY: the mainThread steals the process main thread
[969b3fe]531        // which will then be scheduled by the mainProcessor normally
532        mainThread = (thread_desc *)&storage_mainThread;
[8fcbb4c]533        current_stack_info_t info;
[83a071f9]534        (*mainThread){ &info };
[eb2e723]535
[36982fc]536        __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
[fa21ac9]537
[bd98b58]538
[de6319f]539
540        // Construct the processor context of the main processor
541        void ?{}(processorCtx_t & this, processor * proc) {
542                (this.__cor){ "Processor" };
543                this.__cor.starter = NULL;
544                this.proc = proc;
545        }
546
547        void ?{}(processor & this) with( this ) {
548                name = "Main Processor";
549                cltr = mainCluster;
550                terminated{ 0 };
551                do_terminate = false;
552                preemption_alarm = NULL;
553                pending_preemption = false;
554                kernel_thread = pthread_self();
555
556                runner{ &this };
557                __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner);
558        }
[fa21ac9]559
[969b3fe]560        // Initialize the main processor and the main processor ctx
[eb2e723]561        // (the coroutine that contains the processing control flow)
[969b3fe]562        mainProcessor = (processor *)&storage_mainProcessor;
[de6319f]563        (*mainProcessor){};
[eb2e723]564
[dcb42b8]565        //initialize the global state variables
[14a61b5]566        kernelTLS.this_processor = mainProcessor;
567        kernelTLS.this_thread    = mainThread;
568        kernelTLS.this_coroutine = &mainThread->self_cor;
[eb2e723]569
[82ff5845]570        // Enable preemption
571        kernel_start_preemption();
572
[969b3fe]573        // Add the main thread to the ready queue
574        // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
575        ScheduleThread(mainThread);
576
577        // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
[dcb42b8]578        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
[1c273d0]579        // mainThread is on the ready queue when this call is made.
[14a61b5]580        kernel_first_resume( kernelTLS.this_processor );
[eb2e723]581
[dcb42b8]582
583
584        // THE SYSTEM IS NOW COMPLETELY RUNNING
[36982fc]585        __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
[82ff5845]586
[14a61b5]587        verify( ! kernelTLS.preemption_state.enabled );
[36982fc]588        enable_interrupts( __cfaabi_dbg_ctx );
[afd550c]589        verify( TL_GET( preemption_state.enabled ) );
[eb2e723]590}
591
[dcb42b8]592void kernel_shutdown(void) {
[36982fc]593        __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
[eb2e723]594
[afd550c]595        verify( TL_GET( preemption_state.enabled ) );
[4e6fb8e]596        disable_interrupts();
[14a61b5]597        verify( ! kernelTLS.preemption_state.enabled );
[4e6fb8e]598
[969b3fe]599        // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
[dcb42b8]600        // When its coroutine terminates, it return control to the mainThread
601        // which is currently here
[969b3fe]602        mainProcessor->do_terminate = true;
[82c948c]603        returnToKernel();
[eb2e723]604
[dcb42b8]605        // THE SYSTEM IS NOW COMPLETELY STOPPED
[eb2e723]606
[82ff5845]607        // Disable preemption
608        kernel_stop_preemption();
609
[969b3fe]610        // Destroy the main processor and its context in reverse order of construction
[dcb42b8]611        // These were manually constructed so we need manually destroy them
[094476d]612        ^(mainProcessor->runner){};
[969b3fe]613        ^(mainProcessor){};
[eb2e723]614
[dcb42b8]615        // Final step, destroy the main thread since it is no longer needed
616        // Since we provided a stack to this taxk it will not destroy anything
[eb2e723]617        ^(mainThread){};
618
[a1a17a7]619        ^(global_clusters.list){};
620        ^(global_clusters.lock){};
621
[36982fc]622        __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
[9d944b2]623}
624
[14a61b5]625//=============================================================================================
626// Kernel Quiescing
627//=============================================================================================
628
629// void halt(processor * this) with( this ) {
630//      pthread_mutex_lock( &idle.lock );
631
632
633
634//      // SKULLDUGGERY: Even if spurious wake-up is a thing
635//      // spuriously waking up a kernel thread is not a big deal
636//      // if it is very rare.
637//      pthread_cond_wait( &idle.cond, &idle.lock);
638//      pthread_mutex_unlock( &idle.lock );
639// }
640
641// void wake(processor * this) with( this ) {
642//      pthread_mutex_lock  (&idle.lock);
643//      pthread_cond_signal (&idle.cond);
644//      pthread_mutex_unlock(&idle.lock);
645// }
646
[dbe9b08]647//=============================================================================================
648// Unexpected Terminating logic
649//=============================================================================================
650
651
[ea7d2b0]652static __spinlock_t kernel_abort_lock;
[9d944b2]653static bool kernel_abort_called = false;
654
[afd550c]655void * kernel_abort(void) __attribute__ ((__nothrow__)) {
[9d944b2]656        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
657        // the globalAbort flag is true.
[36982fc]658        lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
[9d944b2]659
660        // first task to abort ?
[de94a60]661        if ( kernel_abort_called ) {                    // not first task to abort ?
[ea7d2b0]662                unlock( kernel_abort_lock );
[1c273d0]663
[9d944b2]664                sigset_t mask;
665                sigemptyset( &mask );
[de94a60]666                sigaddset( &mask, SIGALRM );            // block SIGALRM signals
667                sigsuspend( &mask );                    // block the processor to prevent further damage during abort
668                _exit( EXIT_FAILURE );                  // if processor unblocks before it is killed, terminate it
669        }
670        else {
671                kernel_abort_called = true;
672                unlock( kernel_abort_lock );
[9d944b2]673        }
674
[14a61b5]675        return kernelTLS.this_thread;
[9d944b2]676}
677
678void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
679        thread_desc * thrd = kernel_data;
680
[de94a60]681        if(thrd) {
682                int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
[36982fc]683                __cfaabi_dbg_bits_write( abort_text, len );
[de94a60]684
685                if ( get_coroutine(thrd) != kernelTLS.this_coroutine ) {
686                        len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", kernelTLS.this_coroutine->name, kernelTLS.this_coroutine );
687                        __cfaabi_dbg_bits_write( abort_text, len );
688                }
689                else {
690                        __cfaabi_dbg_bits_write( ".\n", 2 );
691                }
[1c273d0]692        }
[9d944b2]693        else {
[de94a60]694                int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
[639991a]695                __cfaabi_dbg_bits_write( abort_text, len );
[9d944b2]696        }
697}
698
[2b8bc41]699int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
[14a61b5]700        return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
[2b8bc41]701}
702
[de94a60]703static __spinlock_t kernel_debug_lock;
704
[9d944b2]705extern "C" {
[36982fc]706        void __cfaabi_dbg_bits_acquire() {
707                lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
[9d944b2]708        }
709
[36982fc]710        void __cfaabi_dbg_bits_release() {
[ea7d2b0]711                unlock( kernel_debug_lock );
[9d944b2]712        }
[8118303]713}
714
[fa21ac9]715//=============================================================================================
716// Kernel Utilities
717//=============================================================================================
[bd98b58]718//-----------------------------------------------------------------------------
719// Locks
[242a902]720void  ?{}( semaphore & this, int count = 1 ) {
721        (this.lock){};
722        this.count = count;
723        (this.waiting){};
[db6f06a]724}
[242a902]725void ^?{}(semaphore & this) {}
[db6f06a]726
[65deb18]727void P(semaphore & this) with( this ){
728        lock( lock __cfaabi_dbg_ctx2 );
729        count -= 1;
730        if ( count < 0 ) {
[bdeba0b]731                // queue current task
[14a61b5]732                append( waiting, kernelTLS.this_thread );
[bdeba0b]733
734                // atomically release spin lock and block
[65deb18]735                BlockInternal( &lock );
[8def349]736        }
[4e6fb8e]737        else {
[65deb18]738            unlock( lock );
[4e6fb8e]739        }
[bd98b58]740}
741
[65deb18]742void V(semaphore & this) with( this ) {
[bdeba0b]743        thread_desc * thrd = NULL;
[65deb18]744        lock( lock __cfaabi_dbg_ctx2 );
745        count += 1;
746        if ( count <= 0 ) {
[bdeba0b]747                // remove task at head of waiting list
[65deb18]748                thrd = pop_head( waiting );
[bd98b58]749        }
[bdeba0b]750
[65deb18]751        unlock( lock );
[bdeba0b]752
753        // make new owner
754        WakeThread( thrd );
[bd98b58]755}
756
[f7d6bb0]757//-----------------------------------------------------------------------------
[de94a60]758// Global Queues
759void doregister( cluster     & cltr ) {
[639991a]760        lock      ( global_clusters.lock __cfaabi_dbg_ctx2);
761        push_front( global_clusters.list, cltr );
762        unlock    ( global_clusters.lock );
[de94a60]763}
[f7d6bb0]764
[de94a60]765void unregister( cluster     & cltr ) {
[639991a]766        lock  ( global_clusters.lock __cfaabi_dbg_ctx2);
767        remove( global_clusters.list, cltr );
768        unlock( global_clusters.lock );
[de94a60]769}
[f7d6bb0]770
[a1a17a7]771void doregister( cluster * cltr, thread_desc & thrd ) {
772        lock      (cltr->thread_list_lock __cfaabi_dbg_ctx2);
773        push_front(cltr->threads, thrd);
774        unlock    (cltr->thread_list_lock);
775}
776
777void unregister( cluster * cltr, thread_desc & thrd ) {
778        lock  (cltr->thread_list_lock __cfaabi_dbg_ctx2);
779        remove(cltr->threads, thrd );
780        unlock(cltr->thread_list_lock);
781}
[9181f1d]782
[de94a60]783void doregister( cluster * cltr, processor * proc ) {
[639991a]784        lock      (cltr->proc_list_lock __cfaabi_dbg_ctx2);
785        push_front(cltr->procs, *proc);
786        unlock    (cltr->proc_list_lock);
[de94a60]787}
788
789void unregister( cluster * cltr, processor * proc ) {
[639991a]790        lock  (cltr->proc_list_lock __cfaabi_dbg_ctx2);
791        remove(cltr->procs, *proc );
792        unlock(cltr->proc_list_lock);
[de94a60]793}
794
795//-----------------------------------------------------------------------------
796// Debug
797__cfaabi_dbg_debug_do(
[9181f1d]798        void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) {
799                this.prev_name = prev_name;
[14a61b5]800                this.prev_thrd = kernelTLS.this_thread;
[9181f1d]801        }
[f7d6bb0]802)
[8118303]803// Local Variables: //
804// mode: c //
805// tab-width: 4 //
806// End: //
Note: See TracBrowser for help on using the repository browser.