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

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

Kernel now uses intrusive lists and blocking locks for ready queue management.
Update the plan for concurrency.

  • Property mode set to 100644
File size: 3.9 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 __signal_termination__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      __signal_termination__F_P9sthread_h__1(thrd);
60
61      //Final suspend, should never return
62      __suspend_no_inline__F___1();
63      assertf(false, "Resumed dead thread");
64}
65
66
67void CtxStart(
68      void (*main)(void *), 
69      struct coroutine *(*get_coroutine)(void *), 
70      void *this, 
71      void (*invoke)(void *)
72) {
73      // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
74
75      struct coStack_t* stack = &get_coroutine( this )->stack;
76
77#if defined( __i386__ )
78
79        struct FakeStack {
80            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
81            void *rturn;                                      // where to go on return from uSwitch
82            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
83            void *argument[3];                          // for 16-byte ABI, 16-byte alignment starts here
84            void *padding;                              // padding to force 16-byte alignment, as "base" is 16-byte aligned
85        };
86
87        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
88        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
89
90        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
91        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
92        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
93
94#elif defined( __x86_64__ )
95
96      struct FakeStack {
97            void *fixedRegisters[5];                    // fixed registers rbx, r12, r13, r14, r15
98            void *rturn;                                        // where to go on return from uSwitch
99            void *dummyReturn;                          // NULL return address to provide proper alignment
100      };
101
102      ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
103      ((struct machine_context_t *)stack->context)->FP = NULL;          // terminate stack with NULL fp
104
105      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
106      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
107      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
108      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
109#else
110      #error Only __i386__ and __x86_64__ is supported for threads in cfa
111#endif
112}
Note: See TracBrowser for help on using the repository browser.