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

aaron-thesisarm-ehcleanup-dtorsdeferred_resnjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexer
Last change on this file since f23b685 was f23b685, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

added some comments and moved a line to ease coroutine cancellation

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