Ignore:
Timestamp:
Mar 31, 2017, 12:16:14 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
077810d
Parents:
95448f1e
Message:

Implemented interposing for abort and exit, implemented safer debug output

Location:
src/libcfa/concurrency
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/kernel.c

    r95448f1e r9d944b2  
    1515//
    1616
     17#include "startup.h"
     18
    1719//Start and stop routine for the kernel, declared first to make sure they run first
    18 void kernel_startup(void)  __attribute__((constructor(101)));
    19 void kernel_shutdown(void) __attribute__((destructor(101)));
     20void kernel_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
     21void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
    2022
    2123//Header
     
    2527#include <stddef.h>
    2628extern "C" {
     29#include <stdio.h>
    2730#include <fenv.h>
    2831#include <sys/resource.h>
     32#include <signal.h>
     33#include <unistd.h>
    2934}
    3035
     
    146151
    147152        this->runner = runner;
    148         LIB_DEBUG_PRINTF("Kernel : constructing processor context %p\n", runner);
     153        LIB_DEBUG_PRINT_SAFE("Kernel : constructing processor context %p\n", runner);
    149154        runner{ this };
    150155}
     
    152157void ^?{}(processor * this) {
    153158        if( ! this->is_terminated ) {
    154                 LIB_DEBUG_PRINTF("Kernel : core %p signaling termination\n", this);
     159                LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", this);
    155160                this->is_terminated = true;
    156161                wait( &this->terminated );
     
    173178void main(processorCtx_t * runner) {
    174179        processor * this = runner->proc;
    175         LIB_DEBUG_PRINTF("Kernel : core %p starting\n", this);
     180        LIB_DEBUG_PRINT_SAFE("Kernel : core %p starting\n", this);
    176181
    177182        thread_desc * readyThread = NULL;
     
    195200        }
    196201
    197         LIB_DEBUG_PRINTF("Kernel : core %p unlocking thread\n", this);
     202        LIB_DEBUG_PRINT_SAFE("Kernel : core %p unlocking thread\n", this);
    198203        signal( &this->terminated );
    199         LIB_DEBUG_PRINTF("Kernel : core %p terminated\n", this);
     204        LIB_DEBUG_PRINT_SAFE("Kernel : core %p terminated\n", this);
    200205}
    201206
     
    255260        processorCtx_t proc_cor_storage = { proc, &info };
    256261
    257         LIB_DEBUG_PRINTF("Coroutine : created stack %p\n", proc_cor_storage.__cor.stack.base);
     262        LIB_DEBUG_PRINT_SAFE("Coroutine : created stack %p\n", proc_cor_storage.__cor.stack.base);
    258263
    259264        //Set global state
     
    262267
    263268        //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);
     269        LIB_DEBUG_PRINT_SAFE("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
    265270
    266271        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
     
    273278
    274279        // 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);     
     280        LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner);
    276281
    277282        return NULL;
     
    279284
    280285void start(processor * this) {
    281         LIB_DEBUG_PRINTF("Kernel : Starting core %p\n", this);
     286        LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
    282287       
    283288        // pthread_attr_t attributes;
     
    288293        // pthread_attr_destroy( &attributes );
    289294
    290         LIB_DEBUG_PRINTF("Kernel : core %p started\n", this);   
     295        LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);       
    291296}
    292297
     
    334339// Kernel boot procedures
    335340void kernel_startup(void) {
    336         LIB_DEBUG_PRINTF("Kernel : Starting\n");       
     341        LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");   
    337342
    338343        // Start by initializing the main thread
     
    369374
    370375        // THE SYSTEM IS NOW COMPLETELY RUNNING
    371         LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n");
     376        LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n");
    372377}
    373378
    374379void kernel_shutdown(void) {
    375         LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down\n");
     380        LIB_DEBUG_PRINT_SAFE("\n--------------------------------------------------\nKernel : Shutting down\n");
    376381
    377382        // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
     
    392397        ^(mainThread){};
    393398
    394         LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n");       
     399        LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");   
     400}
     401
     402static spinlock kernel_abort_lock;
     403static spinlock kernel_debug_lock;
     404static bool kernel_abort_called = false;
     405
     406void * kernel_abort    (void) __attribute__ ((__nothrow__)) {
     407        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
     408        // the globalAbort flag is true.
     409        lock( &kernel_abort_lock );
     410
     411        // first task to abort ?
     412        if ( !kernel_abort_called ) {                   // not first task to abort ?
     413                kernel_abort_called = true;
     414                unlock( &kernel_abort_lock );
     415        }
     416        else {
     417                unlock( &kernel_abort_lock );
     418               
     419                sigset_t mask;
     420                sigemptyset( &mask );
     421                sigaddset( &mask, SIGALRM );                    // block SIGALRM signals
     422                sigaddset( &mask, SIGUSR1 );                    // block SIGUSR1 signals
     423                sigsuspend( &mask );                            // block the processor to prevent further damage during abort
     424                _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it             
     425        }
     426
     427        return this_thread();
     428}
     429
     430void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
     431        thread_desc * thrd = kernel_data;
     432
     433        int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->cor.name, thrd );
     434        __lib_debug_write( STDERR_FILENO, abort_text, len );
     435
     436        if ( thrd != this_coroutine() ) {
     437                len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine()->name, this_coroutine() );
     438                __lib_debug_write( STDERR_FILENO, abort_text, len );
     439        }
     440        else {
     441                __lib_debug_write( STDERR_FILENO, ".\n", 2 );
     442        }
     443}
     444
     445extern "C" {
     446        void __lib_debug_acquire() {
     447                lock(&kernel_debug_lock);
     448        }
     449
     450        void __lib_debug_release() {
     451                unlock(&kernel_debug_lock);
     452        }
    395453}
    396454
  • src/libcfa/concurrency/thread.c

    r95448f1e r9d944b2  
    7171        this_processor->current_coroutine = thrd_c;
    7272
    73         LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
     73        LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
    7474
    7575        create_stack(&thrd_c->stack, thrd_c->stack.size);
Note: See TracChangeset for help on using the changeset viewer.