source: src/libcfa/concurrency/invoke.c @ 22bdc34

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 22bdc34 was b69ea6b, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Updated alarm to use bits/cfatime and fixed preemption for coroutines

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[8def349]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// invoke.c --
8//
9// Author           : Thierry Delisle
10// Created On       : Tue Jan 17 12:27:26 2016
[6b0b624]11// Last Modified By : Peter A. Buhr
[bede27b]12// Last Modified On : Fri Feb  9 16:37:42 2018
13// Update Count     : 5
[8def349]14//
[78b3f52]15
16#include <stdbool.h>
17#include <stdlib.h>
18#include <stdio.h>
19
20#include "invoke.h"
21
[5c81105]22#define __CFA_INVOKE_PRIVATE__
23#include "invoke.h"
[78b3f52]24
25// magically invoke the "main" of the most derived class
26// Called from the kernel when starting a coroutine or task so must switch back to user mode.
[5c81105]27
[0c92c9f]28extern void __suspend_internal(void);
[39fea2f]29extern void __leave_coroutine(void);
[b69ea6b]30extern void __finish_creation(void);
[1c273d0]31extern void __leave_thread_monitor( struct thread_desc * this );
[82ff5845]32extern void disable_interrupts();
[36982fc]33extern void enable_interrupts( __cfaabi_dbg_ctx_param );
[78b3f52]34
[b58a5772]35void CtxInvokeCoroutine(
[36982fc]36        void (*main)(void *),
37        struct coroutine_desc *(*get_coroutine)(void *),
38        void *this
[80d9e49]39) {
[36982fc]40        struct coroutine_desc* cor = get_coroutine( this );
[80d9e49]41
[36982fc]42        if(cor->state == Primed) {
43                __suspend_internal();
44        }
[80d9e49]45
[36982fc]46        cor->state = Active;
[78b3f52]47
[b69ea6b]48        enable_interrupts( __cfaabi_dbg_ctx );
49
[36982fc]50        main( this );
[78b3f52]51
[36982fc]52        cor->state = Halted;
[8118303]53
[36982fc]54        //Final suspend, should never return
55        __leave_coroutine();
[169d944]56        __cabi_abort( "Resumed dead coroutine" );
[8118303]57}
58
59void CtxInvokeThread(
[36982fc]60        void (*dtor)(void *),
61        void (*main)(void *),
62        struct thread_desc *(*get_thread)(void *),
63        void *this
[8118303]64) {
[36982fc]65        // First suspend, once the thread arrives here,
66        // the function pointer to main can be invalidated without risk
[b69ea6b]67        __finish_creation();
[36982fc]68
69        // Fetch the thread handle from the user defined thread structure
70        struct thread_desc* thrd = get_thread( this );
71
72        // Officially start the thread by enabling preemption
73        enable_interrupts( __cfaabi_dbg_ctx );
74
75        // Call the main of the thread
76        main( this );
77
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 );
[169d944]86        __cabi_abort( "Resumed dead thread" );
[78b3f52]87}
88
[5c81105]89
[b58a5772]90void CtxStart(
[36982fc]91        void (*main)(void *),
92        struct coroutine_desc *(*get_coroutine)(void *),
93        void *this,
94        void (*invoke)(void *)
[80d9e49]95) {
[36982fc]96        struct coStack_t* stack = &get_coroutine( this )->stack;
[78b3f52]97
[381fdee]98#if defined( __i386 )
[d9c44c3]99
100        struct FakeStack {
101            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
[17af7d1]102            uint32_t mxcr;                        // SSE Status and Control bits (control bits are preserved across function calls)
[36982fc]103            uint16_t fcw;                         // X97 FPU control word (preserved across function calls)
[17af7d1]104            void *rturn;                          // where to go on return from uSwitch
[d9c44c3]105            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
[80d9e49]106            void *argument[3];                          // for 16-byte ABI, 16-byte alignment starts here
107            void *padding;                              // padding to force 16-byte alignment, as "base" is 16-byte aligned
[d9c44c3]108        };
109
110        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
111        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
112
113        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
[80d9e49]114        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
[d9c44c3]115        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
[36982fc]116        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
117        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
[d9c44c3]118
[381fdee]119#elif defined( __x86_64 )
[d9c44c3]120
[36982fc]121        struct FakeStack {
122                void *fixedRegisters[5];            // fixed registers rbx, r12, r13, r14, r15
123                uint32_t mxcr;                      // SSE Status and Control bits (control bits are preserved across function calls)
124                uint16_t fcw;                       // X97 FPU control word (preserved across function calls)
125                void *rturn;                        // where to go on return from uSwitch
126                void *dummyReturn;                  // NULL return address to provide proper alignment
127        };
128
129        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
130        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
131
132        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
133        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
134        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
135        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
136        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
137        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
[b158d8f]138
139#elif defined( __ARM_ARCH )
140
141        struct FakeStack {
142                float fpRegs[16];                       // floating point registers
143                void *intRegs[9];                       // integer/pointer registers
144                void *arg[2];                           // placeholder for this pointer
145        };
146
147        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
148        ((struct machine_context_t *)stack->context)->FP = NULL;
149
150        struct FakeStack *fs = (struct FakeStack *)((struct machine_context_t *)stack->context)->SP;
151
152        fs->intRegs[8] = CtxInvokeStub;
153        fs->arg[0] = this;
154        fs->arg[1] = invoke;
[381fdee]155
[d9c44c3]156#else
[381fdee]157        #error uknown hardware architecture
[d9c44c3]158#endif
[e4745d7a]159}
[6b0b624]160
161// Local Variables: //
162// mode: c //
163// tab-width: 4 //
164// End: //
Note: See TracBrowser for help on using the repository browser.