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

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

First prototype of cfa threads running (1 thread on 1 processor)

  • Property mode set to 100644
File size: 3.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
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 Coroutine : 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      //Final suspend, should never return
35      __suspend_no_inline__F___1();
36      assertf(false, "Resumed dead coroutine");
37}
38
39void CtxInvokeThread(
40      void (*main)(void *), 
41      struct thread_h *(*get_thread)(void *), 
42      void *this
43) {
44      LIB_DEBUG_PRINTF("Invoke Thread : Received %p (main %p, get_t %p)\n", this, main, get_thread);
45
46      __suspend_no_inline__F___1();
47
48      struct coroutine* cor = &get_thread( this )->c;
49      cor->state = Active;
50
51      LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
52      main( this );
53
54      //Final suspend, should never return
55      __suspend_no_inline__F___1();
56      assertf(false, "Resumed dead thread");
57}
58
59
60void CtxStart(
61      void (*main)(void *), 
62      struct coroutine *(*get_coroutine)(void *), 
63      void *this, 
64      void (*invoke)(void *)
65) {
66      LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
67
68      struct coStack_t* stack = &get_coroutine( this )->stack;
69
70#if defined( __i386__ )
71
72        struct FakeStack {
73            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
74            void *rturn;                                      // where to go on return from uSwitch
75            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
76            void *argument[3];                          // for 16-byte ABI, 16-byte alignment starts here
77            void *padding;                              // padding to force 16-byte alignment, as "base" is 16-byte aligned
78        };
79
80        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
81        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
82
83        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
84        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
85        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
86
87#elif defined( __x86_64__ )
88
89      struct FakeStack {
90            void *fixedRegisters[5];                    // fixed registers rbx, r12, r13, r14, r15
91            void *rturn;                                        // where to go on return from uSwitch
92            void *dummyReturn;                          // NULL return address to provide proper alignment
93      };
94
95      ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
96      ((struct machine_context_t *)stack->context)->FP = NULL;          // terminate stack with NULL fp
97
98      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
99      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
100      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
101      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
102#else
103      #error Only __i386__ and __x86_64__ is supported for threads in cfa
104#endif
105}
Note: See TracBrowser for help on using the repository browser.