Ignore:
Timestamp:
Dec 16, 2020, 4:01:57 PM (4 years ago)
Author:
Colby Alexander Parsons <caparsons@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
8ba363e, c8025a21
Parents:
b3c8496 (diff), 3e5dd913 (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:
libcfa/src/concurrency
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/coroutine.cfa

    rb3c8496 r53449a4  
    1010// Created On       : Mon Nov 28 12:27:26 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Oct 23 23:05:24 2020
    13 // Update Count     : 22
     12// Last Modified On : Tue Dec 15 12:06:04 2020
     13// Update Count     : 23
    1414//
    1515
     
    2828#include "kernel_private.hfa"
    2929#include "exception.hfa"
     30#include "math.hfa"
     31
     32#define CFA_COROUTINE_USE_MMAP 0
    3033
    3134#define __CFA_INVOKE_PRIVATE__
     
    8588static const size_t MinStackSize = 1000;
    8689extern size_t __page_size;                              // architecture pagesize HACK, should go in proper runtime singleton
     90extern int __map_prot;
    8791
    8892void __stack_prepare( __stack_info_t * this, size_t create_size );
     93void __stack_clean  ( __stack_info_t * this );
    8994
    9095//-----------------------------------------------------------------------------
     
    107112        bool userStack = ((intptr_t)this.storage & 0x1) != 0;
    108113        if ( ! userStack && this.storage ) {
    109                 __attribute__((may_alias)) intptr_t * istorage = (intptr_t *)&this.storage;
    110                 *istorage &= (intptr_t)-1;
    111 
    112                 void * storage = this.storage->limit;
    113                 __cfaabi_dbg_debug_do(
    114                         storage = (char*)(storage) - __page_size;
    115                         if ( mprotect( storage, __page_size, PROT_READ | PROT_WRITE ) == -1 ) {
    116                                 abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
    117                         }
    118                 );
    119                 __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage);
    120                 free( storage );
     114                __stack_clean( &this );
    121115        }
    122116}
     
    167161        assert(__page_size != 0l);
    168162        size_t size = libCeiling( storageSize, 16 ) + stack_data_size;
     163        size = ceiling(size, __page_size);
    169164
    170165        // If we are running debug, we also need to allocate a guardpage to catch stack overflows.
    171166        void * storage;
    172         __cfaabi_dbg_debug_do(
    173                 storage = memalign( __page_size, size + __page_size );
    174         );
    175         __cfaabi_dbg_no_debug_do(
    176                 storage = (void*)malloc(size);
    177         );
    178 
     167        #if CFA_COROUTINE_USE_MMAP
     168                storage = mmap(0p, size + __page_size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
     169                if(storage == ((void*)-1)) {
     170                        abort( "coroutine stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
     171                }
     172                if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
     173                        abort( "coroutine stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
     174                } // if
     175                storage = (void *)(((intptr_t)storage) + __page_size);
     176        #else
     177                __cfaabi_dbg_debug_do(
     178                        storage = memalign( __page_size, size + __page_size );
     179                );
     180                __cfaabi_dbg_no_debug_do(
     181                        storage = (void*)malloc(size);
     182                );
     183
     184                __cfaabi_dbg_debug_do(
     185                        if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
     186                                abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) );
     187                        }
     188                        storage = (void *)(((intptr_t)storage) + __page_size);
     189                );
     190        #endif
    179191        __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size);
    180         __cfaabi_dbg_debug_do(
    181                 if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
    182                         abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) );
    183                 }
    184                 storage = (void *)(((intptr_t)storage) + __page_size);
    185         );
    186192
    187193        verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul );
    188194        return [storage, size];
     195}
     196
     197void __stack_clean  ( __stack_info_t * this ) {
     198        size_t size = ((intptr_t)this->storage->base) - ((intptr_t)this->storage->limit) + sizeof(__stack_t);
     199        void * storage = this->storage->limit;
     200
     201        #if CFA_COROUTINE_USE_MMAP
     202                storage = (void *)(((intptr_t)storage) - __page_size);
     203                if(munmap(storage, size + __page_size) == -1) {
     204                        abort( "coroutine stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
     205                }
     206        #else
     207                __cfaabi_dbg_debug_do(
     208                        storage = (char*)(storage) - __page_size;
     209                        if ( mprotect( storage, __page_size, __map_prot ) == -1 ) {
     210                                abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
     211                        }
     212                );
     213
     214                free( storage );
     215        #endif
     216        __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage);
    189217}
    190218
     
    210238        assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize );
    211239
    212         this->storage = (__stack_t *)((intptr_t)storage + size);
     240        this->storage = (__stack_t *)((intptr_t)storage + size - sizeof(__stack_t));
    213241        this->storage->limit = storage;
    214         this->storage->base  = (void*)((intptr_t)storage + size);
     242        this->storage->base  = (void*)((intptr_t)storage + size - sizeof(__stack_t));
    215243        this->storage->exception_context.top_resume = 0p;
    216244        this->storage->exception_context.current_exception = 0p;
  • libcfa/src/concurrency/coroutine.hfa

    rb3c8496 r53449a4  
    102102}
    103103
    104 extern void __stack_prepare   ( __stack_info_t * this, size_t size /* ignored if storage already allocated */);
     104extern void __stack_prepare( __stack_info_t * this, size_t size /* ignored if storage already allocated */);
     105extern void __stack_clean  ( __stack_info_t * this );
     106
    105107
    106108// Suspend implementation inlined for performance
     
    142144
    143145        if( unlikely(dst->context.SP == 0p) ) {
    144                 active_thread()->curr_cor = dst;
    145146                __stack_prepare(&dst->stack, 65000);
    146147                __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine);
    147                 active_thread()->curr_cor = src;
    148148        }
    149149
  • libcfa/src/concurrency/io/setup.cfa

    rb3c8496 r53449a4  
    1717#define _GNU_SOURCE         /* See feature_test_macros(7) */
    1818
     19#if defined(__CFA_DEBUG__)
     20        // #define __CFA_DEBUG_PRINT_IO__
     21        // #define __CFA_DEBUG_PRINT_IO_CORE__
     22#endif
     23
    1924#include "io/types.hfa"
    2025#include "kernel.hfa"
     
    111116
    112117        void __kernel_io_startup(void) {
    113                 __cfaabi_dbg_print_safe( "Kernel : Creating EPOLL instance\n" );
     118                __cfadbg_print_safe(io_core, "Kernel : Creating EPOLL instance\n" );
    114119
    115120                iopoll.epollfd = epoll_create1(0);
     
    118123                }
    119124
    120                 __cfaabi_dbg_print_safe( "Kernel : Starting io poller thread\n" );
     125                __cfadbg_print_safe(io_core, "Kernel : Starting io poller thread\n" );
    121126
    122127                iopoll.run = true;
     
    132137                // Wait for the io poller thread to finish
    133138
    134                 pthread_join( iopoll.thrd, 0p );
    135                 free( iopoll.stack );
     139                __destroy_pthread( iopoll.thrd, iopoll.stack, 0p );
    136140
    137141                int ret = close(iopoll.epollfd);
     
    142146                // Io polling is now fully stopped
    143147
    144                 __cfaabi_dbg_print_safe( "Kernel : IO poller stopped\n" );
     148                __cfadbg_print_safe(io_core, "Kernel : IO poller stopped\n" );
    145149        }
    146150
     
    150154                id.id = doregister(&id);
    151155                __cfaabi_tls.this_proc_id = &id;
    152                 __cfaabi_dbg_print_safe( "Kernel : IO poller thread starting\n" );
     156                __cfadbg_print_safe(io_core, "Kernel : IO poller thread starting\n" );
    153157
    154158                // Block signals to control when they arrive
     
    185189                }
    186190
    187                 __cfaabi_dbg_print_safe( "Kernel : IO poller thread stopping\n" );
     191                __cfadbg_print_safe(io_core, "Kernel : IO poller thread stopping\n" );
    188192                unregister(&id);
    189193                return 0p;
  • libcfa/src/concurrency/kernel/startup.cfa

    rb3c8496 r53449a4  
    2929#include "kernel_private.hfa"
    3030#include "startup.hfa"          // STARTUP_PRIORITY_XXX
     31#include "math.hfa"
     32
     33#define CFA_PROCESSOR_USE_MMAP 0
    3134
    3235//-----------------------------------------------------------------------------
     
    114117}
    115118
    116 size_t __page_size = 0;
     119extern size_t __page_size;
    117120
    118121//-----------------------------------------------------------------------------
     
    158161        /* paranoid */ verify( ! __preemption_enabled() );
    159162        __cfadbg_print_safe(runtime_core, "Kernel : Starting\n");
    160 
    161         __page_size = sysconf( _SC_PAGESIZE );
    162163
    163164        __cfa_dbg_global_clusters.list{ __get };
     
    539540}
    540541
     542extern size_t __page_size;
    541543void ^?{}(processor & this) with( this ){
    542544        if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
     
    550552        }
    551553
    552         int err = pthread_join( kernel_thread, 0p );
    553         if( err != 0 ) abort("KERNEL ERROR: joining processor %p caused error %s\n", &this, strerror(err));
    554 
    555         free( this.stack );
     554        __destroy_pthread( kernel_thread, this.stack, 0p );
    556555
    557556        disable_interrupts();
     
    678677
    679678        void * stack;
    680         __cfaabi_dbg_debug_do(
    681                 stack = memalign( __page_size, stacksize + __page_size );
    682                 // pthread has no mechanism to create the guard page in user supplied stack.
     679        #if CFA_PROCESSOR_USE_MMAP
     680                stacksize = ceiling( stacksize, __page_size ) + __page_size;
     681                stack = mmap(0p, stacksize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
     682                if(stack == ((void*)-1)) {
     683                        abort( "pthread stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
     684                }
    683685                if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
    684                         abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
     686                        abort( "pthread stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
    685687                } // if
    686         );
    687         __cfaabi_dbg_no_debug_do(
    688                 stack = malloc( stacksize );
    689         );
     688        #else
     689                __cfaabi_dbg_debug_do(
     690                        stack = memalign( __page_size, stacksize + __page_size );
     691                        // pthread has no mechanism to create the guard page in user supplied stack.
     692                        if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
     693                                abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
     694                        } // if
     695                );
     696                __cfaabi_dbg_no_debug_do(
     697                        stack = malloc( stacksize );
     698                );
     699        #endif
     700
    690701
    691702        check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
     
    694705        return stack;
    695706}
     707
     708void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) {
     709        int err = pthread_join( pthread, retval );
     710        if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err));
     711
     712        #if CFA_PROCESSOR_USE_MMAP
     713                pthread_attr_t attr;
     714
     715                check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
     716
     717                size_t stacksize;
     718                // default stack size, normally defined by shell limit
     719                check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
     720                assert( stacksize >= PTHREAD_STACK_MIN );
     721                stacksize += __page_size;
     722
     723                if(munmap(stack, stacksize) == -1) {
     724                        abort( "pthread stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
     725                }
     726        #else
     727                free( stack );
     728        #endif
     729}
     730
    696731
    697732#if defined(__CFA_WITH_VERIFY__)
  • libcfa/src/concurrency/kernel_private.hfa

    rb3c8496 r53449a4  
    4949
    5050void * __create_pthread( pthread_t *, void * (*)(void *), void * );
     51void __destroy_pthread( pthread_t pthread, void * stack, void ** retval );
    5152
    5253
  • libcfa/src/concurrency/preemption.cfa

    rb3c8496 r53449a4  
    575575        // Wait for the preemption thread to finish
    576576
    577         pthread_join( alarm_thread, 0p );
    578         free( alarm_stack );
     577        __destroy_pthread( alarm_thread, alarm_stack, 0p );
    579578
    580579        // Preemption is now fully stopped
Note: See TracChangeset for help on using the changeset viewer.