source: src/libcfa/concurrency/invoke.c@ 2d59d53

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 2d59d53 was 80d9e49, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Coroutines no longer require virtual pointers or any code beyond the get_coroutine routine

  • Property mode set to 100644
File size: 3.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__F___1(void);
16
17void invokeCoroutine(
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) {
27 __suspend__F___1();
28 }
29
30 cor->state = Active;
31
32 main( this );
33}
34
35
36void startCoroutine(
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);
43
44 struct coStack_t* stack = &get_coroutine( this )->stack;
45
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
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
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;
60 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this; // argument to invoke
61 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
62
63#elif defined( __x86_64__ )
64
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
69 };
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;
75 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = coInvokeStub;
76 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
77 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
78#else
79 #error Only __i386__ and __x86_64__ is supported for threads in cfa
80#endif
81}
Note: See TracBrowser for help on using the repository browser.