source: src/libcfa/concurrency/invoke.c @ 0a4c59b

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

Threads now use monitor semantics to wait until completion

  • Property mode set to 100644
File size: 5.2 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 );
32
33void CtxInvokeCoroutine(
34      void (*main)(void *), 
35      struct coroutine_desc *(*get_coroutine)(void *), 
36      void *this
37) {
38      // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
39
40      struct coroutine_desc* cor = get_coroutine( this );
41
42      if(cor->state == Primed) {
43            __suspend_internal();
44      }
45
46      cor->state = Active;
47
48      main( this );
49
50      cor->state = Halted;
51
52      //Final suspend, should never return
53      __suspend_internal();
54      abortf("Resumed dead coroutine");
55}
56
57void CtxInvokeThread(
58      void (*dtor)(void *), 
59      void (*main)(void *), 
60      struct thread_desc *(*get_thread)(void *), 
61      void *this
62) {
63      __suspend_internal();
64
65      struct thread_desc* thrd = get_thread( this );
66      struct coroutine_desc* cor = &thrd->cor;
67      struct monitor_desc* mon = &thrd->mon;
68      cor->state = Active;
69
70      // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
71      main( this );
72
73      __leave_monitor_desc( mon );
74
75      //Final suspend, should never return
76      __suspend_internal();
77      abortf("Resumed dead thread");
78}
79
80
81void CtxStart(
82      void (*main)(void *), 
83      struct coroutine_desc *(*get_coroutine)(void *), 
84      void *this, 
85      void (*invoke)(void *)
86) {
87      // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
88
89      struct coStack_t* stack = &get_coroutine( this )->stack;
90
91#if defined( __i386__ )
92
93        struct FakeStack {
94            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
95            uint32_t mxcr;                        // SSE Status and Control bits (control bits are preserved across function calls)
96          uint16_t fcw;                         // X97 FPU control word (preserved across function calls)
97            void *rturn;                          // where to go on return from uSwitch
98            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
99            void *argument[3];                          // for 16-byte ABI, 16-byte alignment starts here
100            void *padding;                              // padding to force 16-byte alignment, as "base" is 16-byte aligned
101        };
102
103        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
104        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
105
106        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
107        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
108        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
109      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
110      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
111
112#elif defined( __x86_64__ )
113
114      struct FakeStack {
115            void *fixedRegisters[5];            // fixed registers rbx, r12, r13, r14, r15
116            uint32_t mxcr;                      // SSE Status and Control bits (control bits are preserved across function calls)
117            uint16_t fcw;                       // X97 FPU control word (preserved across function calls)
118            void *rturn;                        // where to go on return from uSwitch
119            void *dummyReturn;                  // NULL return address to provide proper alignment
120      };
121
122      ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
123      ((struct machine_context_t *)stack->context)->FP = NULL;          // terminate stack with NULL fp
124
125      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
126      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
127      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
128      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
129      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
130      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
131#else
132      #error Only __i386__ and __x86_64__ is supported for threads in cfa
133#endif
134}
Note: See TracBrowser for help on using the repository browser.