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

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

Fixed incorrectly hand-mangled name

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[0157ca7]1//                              -*- Mode: C -*-
[8def349]2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// invoke.c --
9//
10// Author           : Thierry Delisle
11// Created On       : Tue Jan 17 12:27:26 2016
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count     : 0
15//
[78b3f52]16
17#include <stdbool.h>
18#include <stdlib.h>
19#include <stdio.h>
20
[d9c44c3]21#include "libhdr.h"
[78b3f52]22#include "invoke.h"
23
[5c81105]24#define __CFA_INVOKE_PRIVATE__
25#include "invoke.h"
[78b3f52]26
27// magically invoke the "main" of the most derived class
28// Called from the kernel when starting a coroutine or task so must switch back to user mode.
[5c81105]29
[596f987b]30extern void __suspend_no_inline__F___1(void);
[0157ca7]31extern void __signal_termination__F_P7sthread__1(struct thread*);
[78b3f52]32
[b58a5772]33void CtxInvokeCoroutine(
[80d9e49]34      void (*main)(void *), 
35      struct coroutine *(*get_coroutine)(void *), 
36      void *this
37) {
[c84e80a]38      // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
[80d9e49]39
40      struct coroutine* cor = get_coroutine( this );
41
42      if(cor->state == Primed) {
[596f987b]43            __suspend_no_inline__F___1();
[80d9e49]44      }
[78b3f52]45
46      cor->state = Active;
47
[80d9e49]48      main( this );
[8118303]49
[8f49a54]50      cor->state = Halt;
51      cor->notHalted = false;
52
[8118303]53      //Final suspend, should never return
54      __suspend_no_inline__F___1();
55      assertf(false, "Resumed dead coroutine");
56}
57
58void CtxInvokeThread(
59      void (*main)(void *), 
[e15df4c]60      struct thread *(*get_thread)(void *), 
[8118303]61      void *this
62) {
[c84e80a]63      // LIB_DEBUG_PRINTF("Invoke Thread : Received %p (main %p, get_t %p)\n", this, main, get_thread);
[8118303]64
65      __suspend_no_inline__F___1();
66
[e15df4c]67      struct thread* thrd = get_thread( this );
[c84e80a]68      struct coroutine* cor = &thrd->c;
[8118303]69      cor->state = Active;
70
[c84e80a]71      // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
[8118303]72      main( this );
73
[0157ca7]74      __signal_termination__F_P7sthread__1(thrd);
[c84e80a]75
[8118303]76      //Final suspend, should never return
77      __suspend_no_inline__F___1();
78      assertf(false, "Resumed dead thread");
[78b3f52]79}
80
[5c81105]81
[b58a5772]82void CtxStart(
[80d9e49]83      void (*main)(void *), 
84      struct coroutine *(*get_coroutine)(void *), 
85      void *this, 
86      void (*invoke)(void *)
87) {
[c84e80a]88      // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
[5c81105]89
[80d9e49]90      struct coStack_t* stack = &get_coroutine( this )->stack;
[78b3f52]91
[d9c44c3]92#if defined( __i386__ )
93
94        struct FakeStack {
95            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
96            void *rturn;                                      // where to go on return from uSwitch
97            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
[80d9e49]98            void *argument[3];                          // for 16-byte ABI, 16-byte alignment starts here
99            void *padding;                              // padding to force 16-byte alignment, as "base" is 16-byte aligned
[d9c44c3]100        };
101
102        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
103        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
104
105        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
[80d9e49]106        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
[d9c44c3]107        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
108
109#elif defined( __x86_64__ )
110
[78b3f52]111      struct FakeStack {
112            void *fixedRegisters[5];                    // fixed registers rbx, r12, r13, r14, r15
113            void *rturn;                                        // where to go on return from uSwitch
114            void *dummyReturn;                          // NULL return address to provide proper alignment
[d9c44c3]115      };
[78b3f52]116
117      ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
118      ((struct machine_context_t *)stack->context)->FP = NULL;          // terminate stack with NULL fp
119
120      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
[b58a5772]121      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
[80d9e49]122      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
[5c81105]123      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
[d9c44c3]124#else
125      #error Only __i386__ and __x86_64__ is supported for threads in cfa
126#endif
[e4745d7a]127}
Note: See TracBrowser for help on using the repository browser.