source: src/libcfa/concurrency/invoke.c @ 8cb529e

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

Preemption is now stable enough to push, some clean-up needed

  • Property mode set to 100644
File size: 5.6 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
[0c92c9f]30extern void __suspend_internal(void);
[1c273d0]31extern void __leave_thread_monitor( struct thread_desc * this );
[82ff5845]32extern void disable_interrupts();
[2ac095d]33extern void enable_interrupts( DEBUG_CTX_PARAM );
[78b3f52]34
[b58a5772]35void CtxInvokeCoroutine(
[1c273d0]36      void (*main)(void *),
37      struct coroutine_desc *(*get_coroutine)(void *),
[80d9e49]38      void *this
39) {
[c84e80a]40      // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
[80d9e49]41
[c3acb841]42      struct coroutine_desc* cor = get_coroutine( this );
[80d9e49]43
44      if(cor->state == Primed) {
[0c92c9f]45            __suspend_internal();
[80d9e49]46      }
[78b3f52]47
48      cor->state = Active;
49
[80d9e49]50      main( this );
[8118303]51
[ee897e4b]52      cor->state = Halted;
[8f49a54]53
[8118303]54      //Final suspend, should never return
[0c92c9f]55      __suspend_internal();
56      abortf("Resumed dead coroutine");
[8118303]57}
58
59void CtxInvokeThread(
[1c273d0]60      void (*dtor)(void *),
61      void (*main)(void *),
62      struct thread_desc *(*get_thread)(void *),
[8118303]63      void *this
64) {
[1c273d0]65      // First suspend, once the thread arrives here,
66      // the function pointer to main can be invalidated without risk
[0c92c9f]67      __suspend_internal();
[8118303]68
[1c273d0]69      // Fetch the thread handle from the user defined thread structure
[348006f]70      struct thread_desc* thrd = get_thread( this );
[1c273d0]71
72      // Officially start the thread by enabling preemption
[2ac095d]73      enable_interrupts( DEBUG_CTX );
[8118303]74
[1c273d0]75      // Call the main of the thread
[8118303]76      main( this );
77
[1c273d0]78      // To exit a thread we must :
79      // 1 - Mark it as halted
80      // 2 - Leave its monitor
81      // 3 - Disable the interupts
[f2b12406]82      // 4 - Final suspend
83      // The order of these 4 operations is very important
[8118303]84      //Final suspend, should never return
[f2b12406]85      __leave_thread_monitor( thrd );
[0c92c9f]86      abortf("Resumed dead thread");
[78b3f52]87}
88
[5c81105]89
[b58a5772]90void CtxStart(
[1c273d0]91      void (*main)(void *),
92      struct coroutine_desc *(*get_coroutine)(void *),
93      void *this,
[80d9e49]94      void (*invoke)(void *)
95) {
[c84e80a]96      // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
[5c81105]97
[80d9e49]98      struct coStack_t* stack = &get_coroutine( this )->stack;
[78b3f52]99
[d9c44c3]100#if defined( __i386__ )
101
102        struct FakeStack {
103            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
[17af7d1]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
[d9c44c3]107            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
[80d9e49]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
[d9c44c3]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;
[80d9e49]116        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
[d9c44c3]117        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
[17af7d1]118      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
[1c273d0]119      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
[d9c44c3]120
121#elif defined( __x86_64__ )
122
[78b3f52]123      struct FakeStack {
[17af7d1]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
[d9c44c3]129      };
[78b3f52]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;
[b58a5772]135      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
[80d9e49]136      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
[5c81105]137      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
[8761006c]138      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
[1c273d0]139      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
[d9c44c3]140#else
141      #error Only __i386__ and __x86_64__ is supported for threads in cfa
142#endif
[e4745d7a]143}
Note: See TracBrowser for help on using the repository browser.