source: src/libcfa/concurrency/invoke.c@ 6a5be52

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

Implemented proper support for full-coroutines. Stack unwinding still not done.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// invoke.c --
8//
9// Author : Thierry Delisle
10// Created On : Tue Jan 17 12:27:26 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jul 21 22:28:33 2017
13// Update Count : 1
14//
15
16#include <stdbool.h>
17#include <stdlib.h>
18#include <stdio.h>
19
20#include "libhdr.h"
21#include "invoke.h"
22
23#define __CFA_INVOKE_PRIVATE__
24#include "invoke.h"
25
26// magically invoke the "main" of the most derived class
27// Called from the kernel when starting a coroutine or task so must switch back to user mode.
28
29extern void __suspend_internal(void);
30extern void __leave_coroutine(void);
31extern void __leave_thread_monitor( struct thread_desc * this );
32extern void disable_interrupts();
33extern void enable_interrupts( DEBUG_CTX_PARAM );
34
35void CtxInvokeCoroutine(
36 void (*main)(void *),
37 struct coroutine_desc *(*get_coroutine)(void *),
38 void *this
39) {
40 // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
41
42 struct coroutine_desc* cor = get_coroutine( this );
43
44 if(cor->state == Primed) {
45 __suspend_internal();
46 }
47
48 cor->state = Active;
49
50 main( this );
51
52 cor->state = Halted;
53
54 //Final suspend, should never return
55 __leave_coroutine();
56 abortf("Resumed dead coroutine");
57}
58
59void CtxInvokeThread(
60 void (*dtor)(void *),
61 void (*main)(void *),
62 struct thread_desc *(*get_thread)(void *),
63 void *this
64) {
65 // First suspend, once the thread arrives here,
66 // the function pointer to main can be invalidated without risk
67 __suspend_internal();
68
69 // Fetch the thread handle from the user defined thread structure
70 struct thread_desc* thrd = get_thread( this );
71
72 // Officially start the thread by enabling preemption
73 enable_interrupts( DEBUG_CTX );
74
75 // Call the main of the thread
76 main( this );
77
78 // To exit a thread we must :
79 // 1 - Mark it as halted
80 // 2 - Leave its monitor
81 // 3 - Disable the interupts
82 // 4 - Final suspend
83 // The order of these 4 operations is very important
84 //Final suspend, should never return
85 __leave_thread_monitor( thrd );
86 abortf("Resumed dead thread");
87}
88
89
90void CtxStart(
91 void (*main)(void *),
92 struct coroutine_desc *(*get_coroutine)(void *),
93 void *this,
94 void (*invoke)(void *)
95) {
96 // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
97
98 struct coStack_t* stack = &get_coroutine( this )->stack;
99
100#if defined( __i386__ )
101
102 struct FakeStack {
103 void *fixedRegisters[3]; // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
104 uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls)
105 uint16_t fcw; // X97 FPU control word (preserved across function calls)
106 void *rturn; // where to go on return from uSwitch
107 void *dummyReturn; // fake return compiler would have pushed on call to uInvoke
108 void *argument[3]; // for 16-byte ABI, 16-byte alignment starts here
109 void *padding; // padding to force 16-byte alignment, as "base" is 16-byte aligned
110 };
111
112 ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
113 ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp
114
115 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
116 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this; // argument to invoke
117 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
118 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
119 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F; //Vol. 1 8-7
120
121#elif defined( __x86_64__ )
122
123 struct FakeStack {
124 void *fixedRegisters[5]; // fixed registers rbx, r12, r13, r14, r15
125 uint32_t mxcr; // SSE Status and Control bits (control bits are preserved across function calls)
126 uint16_t fcw; // X97 FPU control word (preserved across function calls)
127 void *rturn; // where to go on return from uSwitch
128 void *dummyReturn; // NULL return address to provide proper alignment
129 };
130
131 ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
132 ((struct machine_context_t *)stack->context)->FP = NULL; // terminate stack with NULL fp
133
134 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
135 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
136 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
137 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
138 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
139 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F; //Vol. 1 8-7
140#else
141 #error Only __i386__ and __x86_64__ is supported for threads in cfa
142#endif
143}
144
145// Local Variables: //
146// mode: c //
147// tab-width: 4 //
148// End: //
Note: See TracBrowser for help on using the repository browser.