source: src/libcfa/concurrency/kernel.c@ 4ea632e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since 4ea632e was de6319f, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Implemented clusters and added many constructors for threads/coroutines/processors

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