source: libcfa/src/concurrency/invoke.c @ 52142c2

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 52142c2 was 09f357ec, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Optim : coroutine and thread creation no-longer uses polymorphic call, leading to significant speedup. Breaks Arm support

  • Property mode set to 100644
File size: 5.1 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#define __cforall_thread__
17
18#include <stdbool.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <unwind.h>
22
23#include "invoke.h"
24
25#define __CFA_INVOKE_PRIVATE__
26#include "invoke.h"
27
28// magically invoke the "main" of the most derived class
29// Called from the kernel when starting a coroutine or task so must switch back to user mode.
30
31extern void __leave_coroutine ( struct coroutine_desc * );
32extern struct coroutine_desc * __finish_coroutine(void);
33extern void __leave_thread_monitor();
34extern void disable_interrupts() OPTIONAL_THREAD;
35extern void enable_interrupts( __cfaabi_dbg_ctx_param );
36
37void CtxInvokeCoroutine(
38        void (*main)(void *),
39        void *this
40) {
41        // Finish setting up the coroutine by setting its state
42        struct coroutine_desc * cor = __finish_coroutine();
43
44        // Call the main of the coroutine
45        main( this );
46
47        //Final suspend, should never return
48        __leave_coroutine( cor );
49        __cabi_abort( "Resumed dead coroutine" );
50}
51
52static _Unwind_Reason_Code _CtxCoroutine_UnwindStop(
53        __attribute((__unused__)) int version,
54        _Unwind_Action actions,
55        __attribute((__unused__)) _Unwind_Exception_Class exceptionClass,
56        __attribute((__unused__)) struct _Unwind_Exception * unwind_exception,
57        __attribute((__unused__)) struct _Unwind_Context * context,
58        void * param
59) {
60        if( actions & _UA_END_OF_STACK  ) {
61                // We finished unwinding the coroutine,
62                // leave it
63                __leave_coroutine( param );
64                __cabi_abort( "Resumed dead coroutine" );
65        }
66        if( actions & _UA_CLEANUP_PHASE ) return _URC_NO_REASON;
67
68        return _URC_FATAL_PHASE2_ERROR;
69}
70
71void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc * cor) __attribute__ ((__noreturn__));
72void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc * cor) {
73        _Unwind_Reason_Code ret = _Unwind_ForcedUnwind( storage, _CtxCoroutine_UnwindStop, cor );
74        printf("UNWIND ERROR %d after force unwind\n", ret);
75        abort();
76}
77
78void CtxInvokeThread(
79        void (*main)(void *),
80        void *this
81) {
82        // Officially start the thread by enabling preemption
83        enable_interrupts( __cfaabi_dbg_ctx );
84
85        // Call the main of the thread
86        main( this );
87
88        // To exit a thread we must :
89        // 1 - Mark it as halted
90        // 2 - Leave its monitor
91        // 3 - Disable the interupts
92        // 4 - Final suspend
93        // The order of these 4 operations is very important
94        //Final suspend, should never return
95        __leave_thread_monitor();
96        __cabi_abort( "Resumed dead thread" );
97}
98
99void CtxStart(
100        void (*main)(void *),
101        struct coroutine_desc * cor,
102        void *this,
103        void (*invoke)(void *)
104) {
105        struct __stack_t * stack = cor->stack.storage;
106
107#if defined( __i386 )
108
109        struct FakeStack {
110            void *fixedRegisters[3];              // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
111            void *rturn;                          // where to go on return from uSwitch
112            void *dummyReturn;                    // fake return compiler would have pushed on call to uInvoke
113            void *argument[3];                    // for 16-byte ABI, 16-byte alignment starts here
114            void *padding;                        // padding to force 16-byte alignment, as "base" is 16-byte aligned
115        };
116
117        cor->context.SP = (char *)stack->base - sizeof( struct FakeStack );
118        cor->context.FP = NULL;         // terminate stack with NULL fp
119
120        struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
121
122        fs->dummyReturn = NULL;
123        fs->argument[0] = main;     // argument to invoke
124        fs->argument[1] = this;     // argument to invoke
125        fs->rturn = invoke;
126
127#elif defined( __x86_64 )
128
129        struct FakeStack {
130                void *fixedRegisters[5];            // fixed registers rbx, r12, r13, r14, r15
131                void *rturn;                        // where to go on return from uSwitch
132                void *dummyReturn;                  // NULL return address to provide proper alignment
133        };
134
135        cor->context.SP = (char *)stack->base - sizeof( struct FakeStack );
136        cor->context.FP = NULL;         // terminate stack with NULL fp
137
138        struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
139
140        fs->dummyReturn = NULL;
141        fs->rturn = CtxInvokeStub;
142        fs->fixedRegisters[0] = main;
143        fs->fixedRegisters[1] = this;
144        fs->fixedRegisters[2] = invoke;
145
146#elif defined( __ARM_ARCH )
147#error ARM needs to be upgrade to use to parameters like X86/X64 (A.K.A. : I broke this and do not know how to fix it)
148        struct FakeStack {
149                float fpRegs[16];                       // floating point registers
150                void *intRegs[9];                       // integer/pointer registers
151                void *arg[2];                           // placeholder for this pointer
152        };
153
154        cor->context.SP = (char *)stack->base - sizeof( struct FakeStack );
155        cor->context.FP = NULL;
156
157        struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
158
159        fs->intRegs[8] = CtxInvokeStub;
160        fs->arg[0] = this;
161        fs->arg[1] = invoke;
162
163#else
164        #error uknown hardware architecture
165#endif
166}
167
168// Local Variables: //
169// mode: c //
170// tab-width: 4 //
171// End: //
Note: See TracBrowser for help on using the repository browser.