source: src/libcfa/concurrency/invoke.c@ 1dcd52a3

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 1dcd52a3 was 169d944, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

update abort, remove abortf, add printing exit

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