source: src/libcfa/concurrency/invoke.c @ 36982fc

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

Renamed internal stuff to cfaabi_...

  • 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 Jul 21 22:28:33 2017
13// Update Count     : 1
14//
15
16#include <stdbool.h>
17#include <stdlib.h>
18#include <stdio.h>
19
20#include "libhdr.h"
21#include "invoke.h"
22
23#define __CFA_INVOKE_PRIVATE__
24#include "invoke.h"
25
26// magically invoke the "main" of the most derived class
27// Called from the kernel when starting a coroutine or task so must switch back to user mode.
28
29extern void __suspend_internal(void);
30extern void __leave_coroutine(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        main( this );
49
50        cor->state = Halted;
51
52        //Final suspend, should never return
53        __leave_coroutine();
54        abortf("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        __suspend_internal();
66
67        // Fetch the thread handle from the user defined thread structure
68        struct thread_desc* thrd = get_thread( this );
69
70        // Officially start the thread by enabling preemption
71        enable_interrupts( __cfaabi_dbg_ctx );
72
73        // Call the main of the thread
74        main( this );
75
76        // To exit a thread we must :
77        // 1 - Mark it as halted
78        // 2 - Leave its monitor
79        // 3 - Disable the interupts
80        // 4 - Final suspend
81        // The order of these 4 operations is very important
82        //Final suspend, should never return
83        __leave_thread_monitor( thrd );
84        abortf("Resumed dead thread");
85}
86
87
88void CtxStart(
89        void (*main)(void *),
90        struct coroutine_desc *(*get_coroutine)(void *),
91        void *this,
92        void (*invoke)(void *)
93) {
94        struct coStack_t* stack = &get_coroutine( this )->stack;
95
96#if defined( __i386__ )
97
98        struct FakeStack {
99            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
100            uint32_t mxcr;                        // SSE Status and Control bits (control bits are preserved across function calls)
101            uint16_t fcw;                         // X97 FPU control word (preserved across function calls)
102            void *rturn;                          // where to go on return from uSwitch
103            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
104            void *argument[3];                          // for 16-byte ABI, 16-byte alignment starts here
105            void *padding;                              // padding to force 16-byte alignment, as "base" is 16-byte aligned
106        };
107
108        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
109        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
110
111        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
112        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
113        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
114        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
115        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
116
117#elif defined( __x86_64__ )
118
119        struct FakeStack {
120                void *fixedRegisters[5];            // fixed registers rbx, r12, r13, r14, r15
121                uint32_t mxcr;                      // SSE Status and Control bits (control bits are preserved across function calls)
122                uint16_t fcw;                       // X97 FPU control word (preserved across function calls)
123                void *rturn;                        // where to go on return from uSwitch
124                void *dummyReturn;                  // NULL return address to provide proper alignment
125        };
126
127        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
128        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
129
130        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
131        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
132        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
133        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
134        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
135        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
136#else
137        #error Only __i386__ and __x86_64__ is supported for threads in cfa
138#endif
139}
140
141// Local Variables: //
142// mode: c //
143// tab-width: 4 //
144// End: //
Note: See TracBrowser for help on using the repository browser.