| 1 | // -*- Mode: CFA -*-
|
|---|
| 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 | // thread.c --
|
|---|
| 9 | //
|
|---|
| 10 | // Author : Thierry Delisle
|
|---|
| 11 | // Created On : Tue Jan 17 12:27:26 2017
|
|---|
| 12 | // Last Modified By : Thierry Delisle
|
|---|
| 13 | // Last Modified On : --
|
|---|
| 14 | // Update Count : 0
|
|---|
| 15 | //
|
|---|
| 16 |
|
|---|
| 17 | #include "thread"
|
|---|
| 18 |
|
|---|
| 19 | #include "kernel_private.h"
|
|---|
| 20 | #include "libhdr.h"
|
|---|
| 21 |
|
|---|
| 22 | #define __CFA_INVOKE_PRIVATE__
|
|---|
| 23 | #include "invoke.h"
|
|---|
| 24 |
|
|---|
| 25 | extern "C" {
|
|---|
| 26 | #include <fenv.h>
|
|---|
| 27 | #include <stddef.h>
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | extern thread_local processor * this_processor;
|
|---|
| 31 |
|
|---|
| 32 | //-----------------------------------------------------------------------------
|
|---|
| 33 | // Thread ctors and dtors
|
|---|
| 34 |
|
|---|
| 35 | void ?{}(thread_desc* this) {
|
|---|
| 36 | (&this->cor){};
|
|---|
| 37 | this->cor.name = "Anonymous Coroutine";
|
|---|
| 38 | this->mon.owner = this;
|
|---|
| 39 | this->mon.recursion = 1;
|
|---|
| 40 | this->next = NULL;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | void ^?{}(thread_desc* this) {
|
|---|
| 44 | ^(&this->cor){};
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
|
|---|
| 48 | void ?{}( scoped(T)* this ) {
|
|---|
| 49 | (&this->handle){};
|
|---|
| 50 | __thrd_start(&this->handle);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
|
|---|
| 54 | void ?{}( scoped(T)* this, P params ) {
|
|---|
| 55 | (&this->handle){ params };
|
|---|
| 56 | __thrd_start(&this->handle);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | forall( dtype T | sized(T) | is_thread(T) )
|
|---|
| 60 | void ^?{}( scoped(T)* this ) {
|
|---|
| 61 | ^(&this->handle){};
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | //-----------------------------------------------------------------------------
|
|---|
| 65 | // Starting and stopping threads
|
|---|
| 66 | forall( dtype T | is_thread(T) )
|
|---|
| 67 | void __thrd_start( T* this ) {
|
|---|
| 68 | coroutine_desc* thrd_c = get_coroutine(this);
|
|---|
| 69 | thread_desc* thrd_h = get_thread (this);
|
|---|
| 70 | thrd_c->last = this_coroutine();
|
|---|
| 71 | this_processor->current_coroutine = thrd_c;
|
|---|
| 72 |
|
|---|
| 73 | LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
|
|---|
| 74 |
|
|---|
| 75 | create_stack(&thrd_c->stack, thrd_c->stack.size);
|
|---|
| 76 | CtxStart(this, CtxInvokeThread);
|
|---|
| 77 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
|
|---|
| 78 |
|
|---|
| 79 | ScheduleThread(thrd_h);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void yield( void ) {
|
|---|
| 83 | ScheduleInternal( this_processor->current_thread );
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
|
|---|
| 87 | // set state of current coroutine to inactive
|
|---|
| 88 | src->state = Inactive;
|
|---|
| 89 | dst->state = Active;
|
|---|
| 90 |
|
|---|
| 91 | //update the last resumer
|
|---|
| 92 | dst->last = src;
|
|---|
| 93 |
|
|---|
| 94 | // set new coroutine that the processor is executing
|
|---|
| 95 | // and context switch to it
|
|---|
| 96 | this_processor->current_coroutine = dst;
|
|---|
| 97 | CtxSwitch( src->stack.context, dst->stack.context );
|
|---|
| 98 | this_processor->current_coroutine = src;
|
|---|
| 99 |
|
|---|
| 100 | // set state of new coroutine to active
|
|---|
| 101 | dst->state = Inactive;
|
|---|
| 102 | src->state = Active;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | // Local Variables: //
|
|---|
| 106 | // mode: c //
|
|---|
| 107 | // tab-width: 4 //
|
|---|
| 108 | // End: //
|
|---|