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

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

Kernel now supports [0-9] cfa threads on a single core, using round-robin scheduling and no preemption

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