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

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 d104b02 was 6b0b624, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change #ifndef to #pragma once

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