source: src/libcfa/concurrency/invoke.c @ 8f49a54

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

Clean-up thread, kernel and examples

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