source: src/libcfa/concurrency/invoke.c @ 4c8621ac

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 4c8621ac was 596f987b, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Some optimization and clean-up of coroutines

  • Property mode set to 100644
File size: 3.0 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
15extern void __suspend_no_inline__F___1(void);
16
17void CtxInvokeCoroutine(
18      void (*main)(void *), 
19      struct coroutine *(*get_coroutine)(void *), 
20      void *this
21) {
22      LIB_DEBUG_PRINTF("Invoke : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
23
24      struct coroutine* cor = get_coroutine( this );
25
26      if(cor->state == Primed) {
27            __suspend_no_inline__F___1();
28      }
29
30      cor->state = Active;
31
32      main( this );
33}
34
35
36void CtxStart(
37      void (*main)(void *), 
38      struct coroutine *(*get_coroutine)(void *), 
39      void *this, 
40      void (*invoke)(void *)
41) {
42      LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p, get_c %p) to %p\n", this, main, get_coroutine, invoke);
43
44      struct coStack_t* stack = &get_coroutine( this )->stack;
45
46#if defined( __i386__ )
47
48        struct FakeStack {
49            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
50            void *rturn;                                      // where to go on return from uSwitch
51            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
52            void *argument[3];                          // for 16-byte ABI, 16-byte alignment starts here
53            void *padding;                              // padding to force 16-byte alignment, as "base" is 16-byte aligned
54        };
55
56        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
57        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
58
59        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
60        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
61        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
62
63#elif defined( __x86_64__ )
64
65      struct FakeStack {
66            void *fixedRegisters[5];                    // fixed registers rbx, r12, r13, r14, r15
67            void *rturn;                                        // where to go on return from uSwitch
68            void *dummyReturn;                          // NULL return address to provide proper alignment
69      };
70
71      ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
72      ((struct machine_context_t *)stack->context)->FP = NULL;          // terminate stack with NULL fp
73
74      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
75      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
76      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
77      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
78#else
79      #error Only __i386__ and __x86_64__ is supported for threads in cfa
80#endif
81}
Note: See TracBrowser for help on using the repository browser.