Ignore:
Timestamp:
Nov 30, 2017, 3:05:25 PM (6 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:
557435e, 5da9d6a
Parents:
dd9b59e (diff), c2b9f21 (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

File:
1 edited

Legend:

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

    rdd9b59e r3d560060  
    1818#include <stdio.h>
    1919
    20 #include "libhdr.h"
    2120#include "invoke.h"
    2221
     
    3130extern void __leave_thread_monitor( struct thread_desc * this );
    3231extern void disable_interrupts();
    33 extern void enable_interrupts( DEBUG_CTX_PARAM );
     32extern void enable_interrupts( __cfaabi_dbg_ctx_param );
    3433
    3534void CtxInvokeCoroutine(
    36       void (*main)(void *),
    37       struct coroutine_desc *(*get_coroutine)(void *),
    38       void *this
     35        void (*main)(void *),
     36        struct coroutine_desc *(*get_coroutine)(void *),
     37        void *this
    3938) {
    40       // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
     39        struct coroutine_desc* cor = get_coroutine( this );
    4140
    42       struct coroutine_desc* cor = get_coroutine( this );
     41        if(cor->state == Primed) {
     42                __suspend_internal();
     43        }
    4344
    44       if(cor->state == Primed) {
    45             __suspend_internal();
    46       }
     45        cor->state = Active;
    4746
    48       cor->state = Active;
     47        main( this );
    4948
    50       main( this );
     49        cor->state = Halted;
    5150
    52       cor->state = Halted;
    53 
    54       //Final suspend, should never return
    55       __leave_coroutine();
    56       abortf("Resumed dead coroutine");
     51        //Final suspend, should never return
     52        __leave_coroutine();
     53        abortf("Resumed dead coroutine");
    5754}
    5855
    5956void CtxInvokeThread(
    60       void (*dtor)(void *),
    61       void (*main)(void *),
    62       struct thread_desc *(*get_thread)(void *),
    63       void *this
     57        void (*dtor)(void *),
     58        void (*main)(void *),
     59        struct thread_desc *(*get_thread)(void *),
     60        void *this
    6461) {
    65       // First suspend, once the thread arrives here,
    66       // the function pointer to main can be invalidated without risk
    67       __suspend_internal();
     62        // First suspend, once the thread arrives here,
     63        // the function pointer to main can be invalidated without risk
     64        __suspend_internal();
    6865
    69       // Fetch the thread handle from the user defined thread structure
    70       struct thread_desc* thrd = get_thread( this );
     66        // Fetch the thread handle from the user defined thread structure
     67        struct thread_desc* thrd = get_thread( this );
    7168
    72       // Officially start the thread by enabling preemption
    73       enable_interrupts( DEBUG_CTX );
     69        // Officially start the thread by enabling preemption
     70        enable_interrupts( __cfaabi_dbg_ctx );
    7471
    75       // Call the main of the thread
    76       main( this );
     72        // Call the main of the thread
     73        main( this );
    7774
    78       // To exit a thread we must :
    79       // 1 - Mark it as halted
    80       // 2 - Leave its monitor
    81       // 3 - Disable the interupts
    82       // 4 - Final suspend
    83       // The order of these 4 operations is very important
    84       //Final suspend, should never return
    85       __leave_thread_monitor( thrd );
    86       abortf("Resumed dead thread");
     75        // To exit a thread we must :
     76        // 1 - Mark it as halted
     77        // 2 - Leave its monitor
     78        // 3 - Disable the interupts
     79        // 4 - Final suspend
     80        // The order of these 4 operations is very important
     81        //Final suspend, should never return
     82        __leave_thread_monitor( thrd );
     83        abortf("Resumed dead thread");
    8784}
    8885
    8986
    9087void CtxStart(
    91       void (*main)(void *),
    92       struct coroutine_desc *(*get_coroutine)(void *),
    93       void *this,
    94       void (*invoke)(void *)
     88        void (*main)(void *),
     89        struct coroutine_desc *(*get_coroutine)(void *),
     90        void *this,
     91        void (*invoke)(void *)
    9592) {
    96       // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
    97 
    98       struct coStack_t* stack = &get_coroutine( this )->stack;
     93        struct coStack_t* stack = &get_coroutine( this )->stack;
    9994
    10095#if defined( __i386__ )
     
    10398            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
    10499            uint32_t mxcr;                        // SSE Status and Control bits (control bits are preserved across function calls)
    105           uint16_t fcw;                         // X97 FPU control word (preserved across function calls)
     100            uint16_t fcw;                         // X97 FPU control word (preserved across function calls)
    106101            void *rturn;                          // where to go on return from uSwitch
    107102            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
     
    116111        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
    117112        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
    118       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
    119       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
     113        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
     114        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
    120115
    121116#elif defined( __x86_64__ )
    122117
    123       struct FakeStack {
    124             void *fixedRegisters[5];            // fixed registers rbx, r12, r13, r14, r15
    125             uint32_t mxcr;                      // SSE Status and Control bits (control bits are preserved across function calls)
    126             uint16_t fcw;                       // X97 FPU control word (preserved across function calls)
    127             void *rturn;                        // where to go on return from uSwitch
    128             void *dummyReturn;                  // NULL return address to provide proper alignment
    129       };
     118        struct FakeStack {
     119                void *fixedRegisters[5];            // fixed registers rbx, r12, r13, r14, r15
     120                uint32_t mxcr;                      // SSE Status and Control bits (control bits are preserved across function calls)
     121                uint16_t fcw;                       // X97 FPU control word (preserved across function calls)
     122                void *rturn;                        // where to go on return from uSwitch
     123                void *dummyReturn;                  // NULL return address to provide proper alignment
     124        };
    130125
    131       ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
    132       ((struct machine_context_t *)stack->context)->FP = NULL;          // terminate stack with NULL fp
     126        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
     127        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
    133128
    134       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
    135       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
    136       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
    137       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
    138       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
    139       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
     129        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
     130        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
     131        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
     132        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
     133        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
     134        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
    140135#else
    141       #error Only __i386__ and __x86_64__ is supported for threads in cfa
     136        #error Only __i386__ and __x86_64__ is supported for threads in cfa
    142137#endif
    143138}
Note: See TracChangeset for help on using the changeset viewer.