source: src/libcfa/concurrency/invoke.c @ 82ff5845

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

First implementation of preemption, does not appear to work with scheduling

  • Property mode set to 100644
File size: 5.3 KB
Line 
1//                              -*- Mode: C -*-
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//
16
17#include <stdbool.h>
18#include <stdlib.h>
19#include <stdio.h>
20
21#include "libhdr.h"
22#include "invoke.h"
23
24#define __CFA_INVOKE_PRIVATE__
25#include "invoke.h"
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.
29
30extern void __suspend_internal(void);
31extern void __leave_monitor_desc( struct monitor_desc * this );
32extern void disable_interrupts();
33extern void enable_interrupts();
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      __suspend_internal();
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      __suspend_internal();
66
67      struct thread_desc* thrd = get_thread( this );
68      struct coroutine_desc* cor = &thrd->cor;
69      struct monitor_desc* mon = &thrd->mon;
70      cor->state = Active;
71      enable_interrupts();
72
73      // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
74      main( this );
75
76      disable_interrupts();
77      __leave_monitor_desc( mon );
78
79      //Final suspend, should never return
80      __suspend_internal();
81      abortf("Resumed dead thread");
82}
83
84
85void CtxStart(
86      void (*main)(void *), 
87      struct coroutine_desc *(*get_coroutine)(void *), 
88      void *this, 
89      void (*invoke)(void *)
90) {
91      // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
92
93      struct coStack_t* stack = &get_coroutine( this )->stack;
94
95#if defined( __i386__ )
96
97        struct FakeStack {
98            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
99            uint32_t mxcr;                        // SSE Status and Control bits (control bits are preserved across function calls)
100          uint16_t fcw;                         // X97 FPU control word (preserved across function calls)
101            void *rturn;                          // where to go on return from uSwitch
102            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
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
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;
111        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
112        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
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
115
116#elif defined( __x86_64__ )
117
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
135#else
136      #error Only __i386__ and __x86_64__ is supported for threads in cfa
137#endif
138}
Note: See TracBrowser for help on using the repository browser.