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

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since b69ea6b was b69ea6b, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

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

  • 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        cor->state = Halted;
53
54        //Final suspend, should never return
55        __leave_coroutine();
56        __cabi_abort( "Resumed dead coroutine" );
57}
58
59void CtxInvokeThread(
60        void (*dtor)(void *),
61        void (*main)(void *),
62        struct thread_desc *(*get_thread)(void *),
63        void *this
64) {
65        // First suspend, once the thread arrives here,
66        // the function pointer to main can be invalidated without risk
67        __finish_creation();
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 );
86        __cabi_abort( "Resumed dead thread" );
87}
88
89
90void CtxStart(
91        void (*main)(void *),
92        struct coroutine_desc *(*get_coroutine)(void *),
93        void *this,
94        void (*invoke)(void *)
95) {
96        struct coStack_t* stack = &get_coroutine( this )->stack;
97
98#if defined( __i386 )
99
100        struct FakeStack {
101            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
102            uint32_t mxcr;                        // SSE Status and Control bits (control bits are preserved across function calls)
103            uint16_t fcw;                         // X97 FPU control word (preserved across function calls)
104            void *rturn;                          // where to go on return from uSwitch
105            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
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
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;
114        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
115        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
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
118
119#elif defined( __x86_64 )
120
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
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;
155
156#else
157        #error uknown hardware architecture
158#endif
159}
160
161// Local Variables: //
162// mode: c //
163// tab-width: 4 //
164// End: //
Note: See TracBrowser for help on using the repository browser.