[6a3d2e7] | 1 | // -*- Mode: CFA -*-
|
---|
[78b3f52] | 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 | //
|
---|
[8118303] | 8 | // threads.c --
|
---|
[78b3f52] | 9 | //
|
---|
| 10 | // Author : Thierry Delisle
|
---|
[6a3d2e7] | 11 | // Created On : Tue Jan 17 12:27:26 2016
|
---|
[78b3f52] | 12 | // Last Modified By : Thierry Delisle
|
---|
[6a3d2e7] | 13 | // Last Modified On : --
|
---|
[78b3f52] | 14 | // Update Count : 0
|
---|
| 15 | //
|
---|
| 16 |
|
---|
[8118303] | 17 | #include "threads"
|
---|
[78b3f52] | 18 |
|
---|
[8118303] | 19 | #include "kernel"
|
---|
| 20 | #include "libhdr.h"
|
---|
| 21 |
|
---|
| 22 | #define __CFA_INVOKE_PRIVATE__
|
---|
| 23 | #include "invoke.h"
|
---|
| 24 |
|
---|
[bd98b58] | 25 | extern "C" {
|
---|
[8fcbb4c] | 26 | #include <fenv.h>
|
---|
[bd98b58] | 27 | #include <stddef.h>
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[8def349] | 30 | extern processor * get_this_processor();
|
---|
[8118303] | 31 |
|
---|
| 32 | //-----------------------------------------------------------------------------
|
---|
| 33 | // Forward declarations
|
---|
[0c92c9f] | 34 | forall( dtype T | is_thread(T) )
|
---|
[bd98b58] | 35 | void start( T* this );
|
---|
[8118303] | 36 |
|
---|
[0c92c9f] | 37 | forall( dtype T | is_thread(T) )
|
---|
[bd98b58] | 38 | void stop( T* this );
|
---|
[8118303] | 39 |
|
---|
| 40 | //-----------------------------------------------------------------------------
|
---|
| 41 | // Thread ctors and dtors
|
---|
| 42 |
|
---|
[e15df4c] | 43 | void ?{}(thread* this) {
|
---|
[8118303] | 44 | (&this->c){};
|
---|
[8def349] | 45 | this->c.name = "Anonymous Coroutine";
|
---|
[bd98b58] | 46 | (&this->lock){};
|
---|
| 47 | this->next = NULL;
|
---|
[8118303] | 48 | }
|
---|
| 49 |
|
---|
[e15df4c] | 50 | void ^?{}(thread* this) {
|
---|
[8118303] | 51 | ^(&this->c){};
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[8def349] | 54 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
|
---|
[e15df4c] | 55 | void ?{}( scoped(T)* this ) {
|
---|
[8118303] | 56 | (&this->handle){};
|
---|
[bd98b58] | 57 | start(&this->handle);
|
---|
[8118303] | 58 | }
|
---|
| 59 |
|
---|
[8def349] | 60 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
|
---|
[e15df4c] | 61 | void ?{}( scoped(T)* this, P params ) {
|
---|
[8118303] | 62 | (&this->handle){ params };
|
---|
[bd98b58] | 63 | start(&this->handle);
|
---|
[8118303] | 64 | }
|
---|
| 65 |
|
---|
[8def349] | 66 | forall( dtype T | sized(T) | is_thread(T) | { void ^?{}(T*); } )
|
---|
[e15df4c] | 67 | void ^?{}( scoped(T)* this ) {
|
---|
[bd98b58] | 68 | stop(&this->handle);
|
---|
[8118303] | 69 | ^(&this->handle){};
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | //-----------------------------------------------------------------------------
|
---|
| 73 | // Starting and stopping threads
|
---|
| 74 | extern "C" {
|
---|
| 75 | forall(dtype T | is_thread(T))
|
---|
| 76 | void CtxInvokeThread(T * this);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[e15df4c] | 79 | extern void thread_schedule( thread * );
|
---|
[bd98b58] | 80 |
|
---|
[0c92c9f] | 81 | forall( dtype T | is_thread(T) )
|
---|
[bd98b58] | 82 | void start( T* this ) {
|
---|
| 83 | coroutine* thrd_c = get_coroutine(this);
|
---|
[e15df4c] | 84 | thread* thrd_h = get_thread (this);
|
---|
[8118303] | 85 | thrd_c->last = this_coroutine();
|
---|
[8def349] | 86 | get_this_processor()->current_coroutine = thrd_c;
|
---|
[8118303] | 87 |
|
---|
[0c92c9f] | 88 | LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
|
---|
[8118303] | 89 |
|
---|
| 90 | create_stack(&thrd_c->stack, thrd_c->stack.size);
|
---|
[bd98b58] | 91 | CtxStart(this, CtxInvokeThread);
|
---|
[8118303] | 92 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
|
---|
| 93 |
|
---|
[8fcbb4c] | 94 | fenv_t envp;
|
---|
| 95 | fegetenv( &envp );
|
---|
| 96 | LIB_DEBUG_PRINTF("Thread : mxcsr %x\n", envp.__mxcsr);
|
---|
[0c92c9f] | 97 | LIB_DEBUG_PRINTF("Thread started : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
|
---|
| 98 |
|
---|
[bd98b58] | 99 | thread_schedule(thrd_h);
|
---|
[8118303] | 100 | }
|
---|
| 101 |
|
---|
[0c92c9f] | 102 | forall( dtype T | is_thread(T) )
|
---|
[bd98b58] | 103 | void stop( T* this ) {
|
---|
[e15df4c] | 104 | thread* thrd = get_thread(this);
|
---|
[bd98b58] | 105 | if( thrd->c.notHalted ) {
|
---|
| 106 | lock( &thrd->lock );
|
---|
[8f49a54] | 107 | }
|
---|
[8118303] | 108 | }
|
---|
[9129a84] | 109 |
|
---|
[bd98b58] | 110 | void yield( void ) {
|
---|
[8fcbb4c] | 111 | get_this_processor()->thread_action = Reschedule;
|
---|
[bd98b58] | 112 | suspend();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[0c92c9f] | 115 | void ThreadCtxSwitch(coroutine* src, coroutine* dst) {
|
---|
| 116 | dst->last = src;
|
---|
| 117 |
|
---|
| 118 | // set state of current coroutine to inactive
|
---|
| 119 | src->state = Inactive;
|
---|
| 120 |
|
---|
| 121 | // set new coroutine that task is executing
|
---|
| 122 | get_this_processor()->current_coroutine = dst;
|
---|
| 123 |
|
---|
| 124 | // context switch to specified coroutine
|
---|
| 125 | CtxSwitch( src->stack.context, dst->stack.context );
|
---|
| 126 | // when CtxSwitch returns we are back in the src coroutine
|
---|
| 127 |
|
---|
| 128 | // set state of new coroutine to active
|
---|
| 129 | src->state = Active;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | // C Helper to signal the termination of a thread
|
---|
| 133 | // Used in invoke.c
|
---|
| 134 | extern "C" {
|
---|
| 135 | void __thread_signal_termination( thread * this ) {
|
---|
| 136 | this->c.state = Halt;
|
---|
| 137 | this->c.notHalted = false;
|
---|
| 138 | unlock( &this->lock );
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[78b3f52] | 142 | // Local Variables: //
|
---|
| 143 | // mode: c //
|
---|
| 144 | // tab-width: 4 //
|
---|
[6a3d2e7] | 145 | // End: //
|
---|