source: src/libcfa/concurrency/invoke.c @ bede27b

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since bede27b was bede27b, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

fix conflicts

  • Property mode set to 100644
File size: 5.6 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);
[1c273d0]30extern void __leave_thread_monitor( struct thread_desc * this );
[82ff5845]31extern void disable_interrupts();
[36982fc]32extern void enable_interrupts( __cfaabi_dbg_ctx_param );
[78b3f52]33
[b58a5772]34void CtxInvokeCoroutine(
[36982fc]35        void (*main)(void *),
36        struct coroutine_desc *(*get_coroutine)(void *),
37        void *this
[80d9e49]38) {
[36982fc]39        struct coroutine_desc* cor = get_coroutine( this );
[80d9e49]40
[36982fc]41        if(cor->state == Primed) {
42                __suspend_internal();
43        }
[80d9e49]44
[36982fc]45        cor->state = Active;
[78b3f52]46
[36982fc]47        main( this );
[78b3f52]48
[36982fc]49        cor->state = Halted;
[8118303]50
[36982fc]51        //Final suspend, should never return
52        __leave_coroutine();
[169d944]53        __cabi_abort( "Resumed dead coroutine" );
[8118303]54}
55
56void CtxInvokeThread(
[36982fc]57        void (*dtor)(void *),
58        void (*main)(void *),
59        struct thread_desc *(*get_thread)(void *),
60        void *this
[8118303]61) {
[36982fc]62        // First suspend, once the thread arrives here,
63        // the function pointer to main can be invalidated without risk
64        __suspend_internal();
65
66        // Fetch the thread handle from the user defined thread structure
67        struct thread_desc* thrd = get_thread( this );
68
69        // Officially start the thread by enabling preemption
70        enable_interrupts( __cfaabi_dbg_ctx );
71
72        // Call the main of the thread
73        main( this );
74
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 );
[169d944]83        __cabi_abort( "Resumed dead thread" );
[78b3f52]84}
85
[5c81105]86
[b58a5772]87void CtxStart(
[36982fc]88        void (*main)(void *),
89        struct coroutine_desc *(*get_coroutine)(void *),
90        void *this,
91        void (*invoke)(void *)
[80d9e49]92) {
[36982fc]93        struct coStack_t* stack = &get_coroutine( this )->stack;
[78b3f52]94
[381fdee]95#if defined( __i386 )
[d9c44c3]96
97        struct FakeStack {
98            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
[17af7d1]99            uint32_t mxcr;                        // SSE Status and Control bits (control bits are preserved across function calls)
[36982fc]100            uint16_t fcw;                         // X97 FPU control word (preserved across function calls)
[17af7d1]101            void *rturn;                          // where to go on return from uSwitch
[d9c44c3]102            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
[80d9e49]103            void *argument[3];                          // for 16-byte ABI, 16-byte alignment starts here
104            void *padding;                              // padding to force 16-byte alignment, as "base" is 16-byte aligned
[d9c44c3]105        };
106
107        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
108        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
109
110        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
[80d9e49]111        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
[d9c44c3]112        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
[36982fc]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
[d9c44c3]115
[381fdee]116#elif defined( __x86_64 )
[d9c44c3]117
[36982fc]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        };
125
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
128
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
[b158d8f]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;
[381fdee]152
[d9c44c3]153#else
[381fdee]154        #error uknown hardware architecture
[d9c44c3]155#endif
[e4745d7a]156}
[6b0b624]157
158// Local Variables: //
159// mode: c //
160// tab-width: 4 //
161// End: //
Note: See TracBrowser for help on using the repository browser.