Ignore:
Timestamp:
Jan 30, 2018, 3:54:32 PM (8 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:
633a642
Parents:
f792cb8 (diff), 42be3c3 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/libcfa/concurrency
Files:
1 added
7 edited

Legend:

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

    rf792cb8 r7416d46a  
    1010// Created On       : Tue Jan 17 12:27:26 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 21 22:28:33 2017
    13 // Update Count     : 1
     12// Last Modified On : Tue Jan 23 14:04:56 2018
     13// Update Count     : 2
    1414//
    1515
     
    133133        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
    134134        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
     135
     136#elif defined( __ARM_ARCH )
     137
     138        struct FakeStack {
     139                float fpRegs[16];                       // floating point registers
     140                void *intRegs[9];                       // integer/pointer registers
     141                void *arg[2];                           // placeholder for this pointer
     142        };
     143
     144        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
     145        ((struct machine_context_t *)stack->context)->FP = NULL;
     146
     147        struct FakeStack *fs = (struct FakeStack *)((struct machine_context_t *)stack->context)->SP;
     148
     149        fs->intRegs[8] = CtxInvokeStub;
     150        fs->arg[0] = this;
     151        fs->arg[1] = invoke;
    135152#else
    136153        #error Only __i386__ and __x86_64__ is supported for threads in cfa
  • src/libcfa/concurrency/invoke.h

    rf792cb8 r7416d46a  
    1010// Created On       : Tue Jan 17 12:27:26 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 21 22:28:56 2017
    13 // Update Count     : 1
     12// Last Modified On : Tue Jan 23 14:55:46 2018
     13// Update Count     : 3
    1414//
    1515
     
    134134                // instrusive link field for threads
    135135                struct thread_desc * next;
     136
     137                __cfaabi_dbg_debug_do(
     138                        // instrusive link field for debugging
     139                        struct thread_desc * dbg_next;
     140                        struct thread_desc * dbg_prev;
     141                )
    136142     };
    137143
     
    203209                        "movl %%ebp,%1\n"   \
    204210                : "=rm" (ctx.SP), "=rm" (ctx.FP) )
     211        #elif defined( __ARM_ARCH )
     212        #define CtxGet( ctx ) __asm__ ( \
     213                        "mov %0,%%sp\n"   \
     214                        "mov %1,%%r11\n"   \
     215                : "=rm" (ctx.SP), "=rm" (ctx.FP) )
    205216        #endif
    206217
  • src/libcfa/concurrency/kernel.c

    rf792cb8 r7416d46a  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 21 22:33:18 2017
    13 // Update Count     : 2
     12// Last Modified On : Fri Dec  8 16:23:33 2017
     13// Update Count     : 3
    1414//
    1515
    1616//C Includes
    1717#include <stddef.h>
     18#define ftype `ftype`
    1819extern "C" {
    1920#include <stdio.h>
     
    2324#include <unistd.h>
    2425}
     26#undef ftype
    2527
    2628//CFA Includes
     
    240242void finishRunning(processor * this) with( this->finish ) {
    241243        if( action_code == Release ) {
     244                verify( disable_preempt_count > 1 );
    242245                unlock( *lock );
    243246        }
     
    246249        }
    247250        else if( action_code == Release_Schedule ) {
     251                verify( disable_preempt_count > 1 );
    248252                unlock( *lock );
    249253                ScheduleThread( thrd );
    250254        }
    251255        else if( action_code == Release_Multi ) {
     256                verify( disable_preempt_count > lock_count );
    252257                for(int i = 0; i < lock_count; i++) {
    253258                        unlock( *locks[i] );
     
    363368        this_processor->finish.lock        = lock;
    364369
    365         verify( disable_preempt_count > 0 );
     370        verify( disable_preempt_count > 1 );
    366371        suspend();
    367372        verify( disable_preempt_count > 0 );
     
    389394        this_processor->finish.thrd        = thrd;
    390395
    391         verify( disable_preempt_count > 0 );
     396        verify( disable_preempt_count > 1 );
    392397        suspend();
    393398        verify( disable_preempt_count > 0 );
     
    514519}
    515520
     521//=============================================================================================
     522// Unexpected Terminating logic
     523//=============================================================================================
     524
     525
    516526static __spinlock_t kernel_abort_lock;
    517527static __spinlock_t kernel_debug_lock;
     
    609619}
    610620
     621//-----------------------------------------------------------------------------
     622// Debug
     623__cfaabi_dbg_debug_do(
     624        struct {
     625                thread_desc * tail;
     626        } __cfaabi_dbg_thread_list = { NULL };
     627
     628        void __cfaabi_dbg_thread_register( thread_desc * thrd ) {
     629                if( !__cfaabi_dbg_thread_list.tail ) {
     630                        __cfaabi_dbg_thread_list.tail = thrd;
     631                        return;
     632                }
     633                __cfaabi_dbg_thread_list.tail->dbg_next = thrd;
     634                thrd->dbg_prev = __cfaabi_dbg_thread_list.tail;
     635                __cfaabi_dbg_thread_list.tail = thrd;
     636        }
     637
     638        void __cfaabi_dbg_thread_unregister( thread_desc * thrd ) {
     639                thread_desc * prev = thrd->dbg_prev;
     640                thread_desc * next = thrd->dbg_next;
     641
     642                if( next ) { next->dbg_prev = prev; }
     643                else       {
     644                        assert( __cfaabi_dbg_thread_list.tail == thrd );
     645                        __cfaabi_dbg_thread_list.tail = prev;
     646                }
     647
     648                if( prev ) { prev->dbg_next = next; }
     649
     650                thrd->dbg_prev = NULL;
     651                thrd->dbg_next = NULL;
     652        }
     653)
    611654// Local Variables: //
    612655// mode: c //
  • src/libcfa/concurrency/kernel_private.h

    rf792cb8 r7416d46a  
    8585extern void ThreadCtxSwitch(coroutine_desc * src, coroutine_desc * dst);
    8686
     87__cfaabi_dbg_debug_do(
     88        extern void __cfaabi_dbg_thread_register  ( thread_desc * thrd );
     89        extern void __cfaabi_dbg_thread_unregister( thread_desc * thrd );
     90)
     91
    8792//-----------------------------------------------------------------------------
    8893// Utils
  • src/libcfa/concurrency/monitor.c

    rf792cb8 r7416d46a  
    5353static inline __lock_size_t aggregate    ( monitor_desc * storage [], const __waitfor_mask_t & mask );
    5454
    55 #ifndef __CFA_LOCK_NO_YIELD
    56 #define DO_LOCK lock_yield
    57 #else
    58 #define DO_LOCK lock
    59 #endif
    60 
    6155//-----------------------------------------------------------------------------
    6256// Useful defines
     
    9084        static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) {
    9185                // Lock the monitor spinlock
    92                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
     86                lock( this->lock __cfaabi_dbg_ctx2 );
    9387                thread_desc * thrd = this_thread;
     88
     89                verify( disable_preempt_count > 0 );
    9490
    9591                __cfaabi_dbg_print_safe("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
     
    121117                        // Some one else has the monitor, wait in line for it
    122118                        append( this->entry_queue, thrd );
     119
     120                        verify( disable_preempt_count > 0 );
     121
    123122                        BlockInternal( &this->lock );
    124123
     
    138137        static void __enter_monitor_dtor( monitor_desc * this, fptr_t func ) {
    139138                // Lock the monitor spinlock
    140                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
     139                lock( this->lock __cfaabi_dbg_ctx2 );
    141140                thread_desc * thrd = this_thread;
    142141
     
    201200        // Leave single monitor
    202201        void __leave_monitor_desc( monitor_desc * this ) {
    203                 // Lock the monitor spinlock, DO_LOCK to reduce contention
    204                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
     202                // Lock the monitor spinlock
     203                lock( this->lock __cfaabi_dbg_ctx2 );
    205204
    206205                __cfaabi_dbg_print_safe("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner);
     
    248247
    249248                // Lock the monitor now
    250                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
     249                lock( this->lock __cfaabi_dbg_ctx2 );
    251250
    252251                disable_interrupts();
     
    397396        append( this.blocked, &waiter );
    398397
     398        verify( disable_preempt_count == 0 );
     399
    399400        // Lock all monitors (aggregates the locks as well)
    400401        lock_all( monitors, locks, count );
     402
     403        // verifyf( disable_preempt_count == count, "Got %d, expected %d\n", disable_preempt_count, count );
     404        if(disable_preempt_count != count) { __cfaabi_dbg_print_buffer_decl("----------Gonna crash\n"); }
    401405
    402406        // Find the next thread(s) to run
     
    473477        monitor_ctx( this.monitors, this.monitor_count );
    474478
     479        verify( disable_preempt_count == 0 );
     480
    475481        // Lock all monitors (aggregates the locks them as well)
    476482        lock_all( monitors, locks, count );
     483
     484        // verify( disable_preempt_count == count );
     485        if(disable_preempt_count != count) { __cfaabi_dbg_print_buffer_decl("----------Gonna crash\n"); }
     486
    477487
    478488        // Create the node specific to this wait operation
     
    737747static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) {
    738748        for( __lock_size_t i = 0; i < count; i++ ) {
    739                 DO_LOCK( *locks[i] __cfaabi_dbg_ctx2 );
     749                lock( *locks[i] __cfaabi_dbg_ctx2 );
    740750        }
    741751}
     
    744754        for( __lock_size_t i = 0; i < count; i++ ) {
    745755                __spinlock_t * l = &source[i]->lock;
    746                 DO_LOCK( *l __cfaabi_dbg_ctx2 );
     756                lock( *l __cfaabi_dbg_ctx2 );
    747757                if(locks) locks[i] = l;
    748758        }
  • src/libcfa/concurrency/preemption.c

    rf792cb8 r7416d46a  
    1010// Created On       : Mon Jun 5 14:20:42 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 21 22:36:05 2017
    13 // Update Count     : 2
     12// Last Modified On : Tue Jan 23 17:59:30 2018
     13// Update Count     : 7
    1414//
    1515
    1616#include "preemption.h"
    1717
     18#define ftype `ftype`
    1819extern "C" {
    1920#include <errno.h>
    20 #include <execinfo.h>
    21 #define __USE_GNU
    22 #include <signal.h>
    23 #undef __USE_GNU
    2421#include <stdio.h>
    2522#include <string.h>
    2623#include <unistd.h>
    2724}
    28 
    29 
    30 #ifdef __USE_STREAM__
    31 #include "fstream"
    32 #endif
     25#undef ftype
     26
     27#include "bits/signal.h"
    3328
    3429//TODO move to defaults
     
    3934        return __CFA_DEFAULT_PREEMPTION__;
    4035}
    41 
    42 // Short hands for signal context information
    43 #define __CFA_SIGCXT__ ucontext_t *
    44 #define __CFA_SIGPARMS__ __attribute__((unused)) int sig, __attribute__((unused)) siginfo_t *sfp, __attribute__((unused)) __CFA_SIGCXT__ cxt
    4536
    4637// FwdDeclarations : timeout handlers
     
    5344void sigHandler_abort    ( __CFA_SIGPARMS__ );
    5445
    55 // FwdDeclarations : sigaction wrapper
    56 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags );
    57 
    5846// FwdDeclarations : alarm thread main
    5947void * alarm_loop( __attribute__((unused)) void * args );
    6048
    6149// Machine specific register name
    62 #ifdef __x86_64__
     50#if   defined(__x86_64__)
    6351#define CFA_REG_IP REG_RIP
    64 #else
     52#elif defined(__i386__)
    6553#define CFA_REG_IP REG_EIP
     54#elif defined(__ARM_ARCH__)
     55#define CFA_REG_IP REG_R15
    6656#endif
    6757
     
    179169        void enable_interrupts_noPoll() {
    180170                __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
    181                 verify( prev != 0u );                     // If this triggers someone is enabled already enabled interrupts
     171                verifyf( prev != 0u, "Incremented from %u\n", prev );                     // If this triggers someone is enabled already enabled interrupts
    182172        }
    183173}
     
    243233        // Setup proper signal handlers
    244234        __kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART );         // CtxSwitch handler
    245         // __kernel_sigaction( SIGSEGV, sigHandler_segv     , SA_SIGINFO );      // Failure handler
    246         // __kernel_sigaction( SIGBUS , sigHandler_segv     , SA_SIGINFO );      // Failure handler
    247235
    248236        signal_block( SIGALRM );
     
    296284// Receives SIGUSR1 signal and causes the current thread to yield
    297285void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
     286#if defined( __ARM_ARCH )
     287        __cfaabi_dbg_debug_do( last_interrupt = (void *)(cxt->uc_mcontext.arm_pc); )
     288#else
    298289        __cfaabi_dbg_debug_do( last_interrupt = (void *)(cxt->uc_mcontext.gregs[CFA_REG_IP]); )
    299 
    300         // Check if it is safe to preempt here
     290#endif
     291
     292                // Check if it is safe to preempt here
    301293        if( !preemption_ready() ) { return; }
     294
     295        __cfaabi_dbg_print_buffer_decl(" KERNEL: preempting core %p (%p).\n", this_processor, this_thread);
    302296
    303297        preemption_in_progress = true;                      // Sync flag : prevent recursive calls to the signal handler
     
    371365}
    372366
    373 // Sigaction wrapper : register an signal handler
    374 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ) {
    375         struct sigaction act;
    376 
    377         act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
    378         act.sa_flags = flags;
    379 
    380         if ( sigaction( sig, &act, NULL ) == -1 ) {
    381                 __cfaabi_dbg_print_buffer_decl(
    382                         " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",
    383                         sig, handler, flags, errno, strerror( errno )
    384                 );
    385                 _exit( EXIT_FAILURE );
    386         }
    387 }
    388 
    389 // Sigaction wrapper : restore default handler
    390 static void __kernel_sigdefault( int sig ) {
    391         struct sigaction act;
    392 
    393         act.sa_handler = SIG_DFL;
    394         act.sa_flags = 0;
    395         sigemptyset( &act.sa_mask );
    396 
    397         if ( sigaction( sig, &act, NULL ) == -1 ) {
    398                 __cfaabi_dbg_print_buffer_decl(
    399                         " __kernel_sigdefault( sig:%d ), problem reseting signal handler, error(%d) %s.\n",
    400                         sig, errno, strerror( errno )
    401                 );
    402                 _exit( EXIT_FAILURE );
    403         }
    404 }
    405 
    406 //=============================================================================================
    407 // Terminating Signals logic
    408 //=============================================================================================
    409 
    410 __cfaabi_dbg_debug_do(
    411         static void __kernel_backtrace( int start ) {
    412                 // skip first N stack frames
    413 
    414                 enum { Frames = 50 };
    415                 void * array[Frames];
    416                 int size = backtrace( array, Frames );
    417                 char ** messages = backtrace_symbols( array, size );
    418 
    419                 // find executable name
    420                 *index( messages[0], '(' ) = '\0';
    421                 #ifdef __USE_STREAM__
    422                 serr | "Stack back trace for:" | messages[0] | endl;
    423                 #else
    424                 fprintf( stderr, "Stack back trace for: %s\n", messages[0]);
    425                 #endif
    426 
    427                 // skip last 2 stack frames after main
    428                 for ( int i = start; i < size && messages != NULL; i += 1 ) {
    429                         char * name = NULL;
    430                         char * offset_begin = NULL;
    431                         char * offset_end = NULL;
    432 
    433                         for ( char *p = messages[i]; *p; ++p ) {
    434                                 // find parantheses and +offset
    435                                 if ( *p == '(' ) {
    436                                         name = p;
    437                                 }
    438                                 else if ( *p == '+' ) {
    439                                         offset_begin = p;
    440                                 }
    441                                 else if ( *p == ')' ) {
    442                                         offset_end = p;
    443                                         break;
    444                                 }
    445                         }
    446 
    447                         // if line contains symbol print it
    448                         int frameNo = i - start;
    449                         if ( name && offset_begin && offset_end && name < offset_begin ) {
    450                                 // delimit strings
    451                                 *name++ = '\0';
    452                                 *offset_begin++ = '\0';
    453                                 *offset_end++ = '\0';
    454 
    455                                 #ifdef __USE_STREAM__
    456                                 serr    | "("  | frameNo | ")" | messages[i] | ":"
    457                                         | name | "+" | offset_begin | offset_end | endl;
    458                                 #else
    459                                 fprintf( stderr, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
    460                                 #endif
    461                         }
    462                         // otherwise, print the whole line
    463                         else {
    464                                 #ifdef __USE_STREAM__
    465                                 serr | "(" | frameNo | ")" | messages[i] | endl;
    466                                 #else
    467                                 fprintf( stderr, "(%i) %s\n", frameNo, messages[i] );
    468                                 #endif
    469                         }
    470                 }
    471 
    472                 free( messages );
    473         }
    474 )
    475 
    476 // void sigHandler_segv( __CFA_SIGPARMS__ ) {
    477 //      __cfaabi_dbg_debug_do(
    478 //              #ifdef __USE_STREAM__
    479 //              serr    | "*CFA runtime error* program cfa-cpp terminated with"
    480 //                      | (sig == SIGSEGV ? "segment fault." : "bus error.")
    481 //                      | endl;
    482 //              #else
    483 //              fprintf( stderr, "*CFA runtime error* program cfa-cpp terminated with %s\n", sig == SIGSEGV ? "segment fault." : "bus error." );
    484 //              #endif
    485 
    486 //              // skip first 2 stack frames
    487 //              __kernel_backtrace( 1 );
    488 //      )
    489 //      exit( EXIT_FAILURE );
    490 // }
    491 
    492 // void sigHandler_abort( __CFA_SIGPARMS__ ) {
    493 //      // skip first 6 stack frames
    494 //      __cfaabi_dbg_debug_do( __kernel_backtrace( 6 ); )
    495 
    496 //      // reset default signal handler
    497 //      __kernel_sigdefault( SIGABRT );
    498 
    499 //      raise( SIGABRT );
    500 // }
    501 
    502367// Local Variables: //
    503368// mode: c //
  • src/libcfa/concurrency/thread.c

    rf792cb8 r7416d46a  
    3838        self_mon_p = &self_mon;
    3939        next = NULL;
     40        __cfaabi_dbg_debug_do(
     41                dbg_next = NULL;
     42                dbg_prev = NULL;
     43                __cfaabi_dbg_thread_register(&this);
     44        )
    4045
    4146        monitors{ &self_mon_p, 1, (fptr_t)0 };
Note: See TracChangeset for help on using the changeset viewer.