source: src/libcfa/concurrency/invoke.c @ 5c81105

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 5c81105 was 5c81105, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

cleaned-up coroutines code to no longer need a manual start

  • Property mode set to 100644
File size: 2.7 KB
Line 
1
2#include <stdbool.h>
3#include <stdlib.h>
4#include <stdio.h>
5
6#include "libhdr.h"
7#include "invoke.h"
8
9#define __CFA_INVOKE_PRIVATE__
10#include "invoke.h"
11
12// magically invoke the "main" of the most derived class
13// Called from the kernel when starting a coroutine or task so must switch back to user mode.
14
15void invokeCoroutine(covptr_t* vthis)
16{
17      LIB_DEBUG_PRINTF("Invoke : Received %p (v %p)\n", vthis, *vthis);
18
19      struct coroutine* cor = get_coroutine( vthis );
20
21      cor->state = Active;
22
23      (*vthis)->main( get_object(vthis) );
24}
25
26
27void startCoroutine(covptr_t* vthis, void (*invoke)(covptr_t*)) {
28      LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (v %p) to %p\n", vthis, *vthis, invoke);
29
30      struct coroutine* this = get_coroutine( vthis );
31      struct coStack_t* stack = &this->stack;
32
33#if defined( __i386__ )
34
35        struct FakeStack {
36            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
37            void *rturn;                                      // where to go on return from uSwitch
38            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
39            void *argument;                             // for 16-byte ABI, 16-byte alignment starts here
40            void *padding[3];                           // padding to force 16-byte alignment, as "base" is 16-byte aligned
41        };
42
43        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
44        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
45
46        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
47        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument = vthis;     // argument to invoke
48        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
49
50#elif defined( __x86_64__ )
51
52      struct FakeStack {
53            void *fixedRegisters[5];                    // fixed registers rbx, r12, r13, r14, r15
54            void *rturn;                                        // where to go on return from uSwitch
55            void *dummyReturn;                          // NULL return address to provide proper alignment
56      };
57
58      ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
59      ((struct machine_context_t *)stack->context)->FP = NULL;          // terminate stack with NULL fp
60
61      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
62      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = coInvokeStub;
63      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = vthis;
64      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
65#else
66      #error Only __i386__ and __x86_64__ is supported for threads in cfa
67#endif
68}
Note: See TracBrowser for help on using the repository browser.