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

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 8f60f0b 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
RevLine 
[78b3f52]1
2#include <stdbool.h>
3#include <stdlib.h>
4#include <stdio.h>
5
[d9c44c3]6#include "libhdr.h"
[78b3f52]7#include "invoke.h"
8
[5c81105]9#define __CFA_INVOKE_PRIVATE__
10#include "invoke.h"
[78b3f52]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.
[5c81105]14
[596f987b]15extern void __suspend_no_inline__F___1(void);
[78b3f52]16
[b58a5772]17void CtxInvokeCoroutine(
[80d9e49]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) {
[596f987b]27            __suspend_no_inline__F___1();
[80d9e49]28      }
[78b3f52]29
30      cor->state = Active;
31
[80d9e49]32      main( this );
[78b3f52]33}
34
[5c81105]35
[b58a5772]36void CtxStart(
[80d9e49]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);
[5c81105]43
[80d9e49]44      struct coStack_t* stack = &get_coroutine( this )->stack;
[78b3f52]45
[d9c44c3]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
[80d9e49]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
[d9c44c3]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;
[80d9e49]60        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
[d9c44c3]61        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
62
63#elif defined( __x86_64__ )
64
[78b3f52]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
[d9c44c3]69      };
[78b3f52]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;
[b58a5772]75      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
[80d9e49]76      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
[5c81105]77      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
[d9c44c3]78#else
79      #error Only __i386__ and __x86_64__ is supported for threads in cfa
80#endif
[e4745d7a]81}
Note: See TracBrowser for help on using the repository browser.