| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // thread.c --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Thierry Delisle
 | 
|---|
| 10 | // Created On       : Tue Jan 17 12:27:26 2017
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Fri Jul 21 22:34:46 2017
 | 
|---|
| 13 | // Update Count     : 1
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "thread"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "kernel_private.h"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #define __CFA_INVOKE_PRIVATE__
 | 
|---|
| 21 | #include "invoke.h"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | extern "C" {
 | 
|---|
| 24 |         #include <fenv.h>
 | 
|---|
| 25 |         #include <stddef.h>
 | 
|---|
| 26 | }
 | 
|---|
| 27 | 
 | 
|---|
| 28 | extern volatile thread_local processor * this_processor;
 | 
|---|
| 29 | 
 | 
|---|
| 30 | //-----------------------------------------------------------------------------
 | 
|---|
| 31 | // Thread ctors and dtors
 | 
|---|
| 32 | 
 | 
|---|
| 33 | void ?{}(thread_desc& this) {
 | 
|---|
| 34 |         (this.self_cor){};
 | 
|---|
| 35 |         this.self_cor.name = "Anonymous Coroutine";
 | 
|---|
| 36 |         this.self_mon.owner = &this;
 | 
|---|
| 37 |         this.self_mon.recursion = 1;
 | 
|---|
| 38 |         this.self_mon_p = &this.self_mon;
 | 
|---|
| 39 |         this.next = NULL;
 | 
|---|
| 40 | 
 | 
|---|
| 41 |         (this.monitors){ &this.self_mon_p, 1, (fptr_t)0 };
 | 
|---|
| 42 | }
 | 
|---|
| 43 | 
 | 
|---|
| 44 | void ^?{}(thread_desc& this) {
 | 
|---|
| 45 |         ^(this.self_cor){};
 | 
|---|
| 46 | }
 | 
|---|
| 47 | 
 | 
|---|
| 48 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } )
 | 
|---|
| 49 | void ?{}( scoped(T)& this ) {
 | 
|---|
| 50 |         (this.handle){};
 | 
|---|
| 51 |         __thrd_start(this.handle);
 | 
|---|
| 52 | }
 | 
|---|
| 53 | 
 | 
|---|
| 54 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
 | 
|---|
| 55 | void ?{}( scoped(T)& this, P params ) {
 | 
|---|
| 56 |         (this.handle){ params };
 | 
|---|
| 57 |         __thrd_start(this.handle);
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | forall( dtype T | sized(T) | is_thread(T) )
 | 
|---|
| 61 | void ^?{}( scoped(T)& this ) {
 | 
|---|
| 62 |         ^(this.handle){};
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | //-----------------------------------------------------------------------------
 | 
|---|
| 66 | // Starting and stopping threads
 | 
|---|
| 67 | forall( dtype T | is_thread(T) )
 | 
|---|
| 68 | void __thrd_start( T& this ) {
 | 
|---|
| 69 |         coroutine_desc* thrd_c = get_coroutine(this);
 | 
|---|
| 70 |         thread_desc*  thrd_h = get_thread   (this);
 | 
|---|
| 71 |         thrd_c->last = this_coroutine;
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         // __cfaabi_dbg_print_safe("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
 | 
|---|
| 74 | 
 | 
|---|
| 75 |         disable_interrupts();
 | 
|---|
| 76 |         create_stack(&thrd_c->stack, thrd_c->stack.size);
 | 
|---|
| 77 |         this_coroutine = thrd_c;
 | 
|---|
| 78 |         CtxStart(&this, CtxInvokeThread);
 | 
|---|
| 79 |         assert( thrd_c->last->stack.context );
 | 
|---|
| 80 |         CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
 | 
|---|
| 81 | 
 | 
|---|
| 82 |         ScheduleThread(thrd_h);
 | 
|---|
| 83 |         enable_interrupts( __cfaabi_dbg_ctx );
 | 
|---|
| 84 | }
 | 
|---|
| 85 | 
 | 
|---|
| 86 | void yield( void ) {
 | 
|---|
| 87 |         BlockInternal( this_thread );
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | void yield( unsigned times ) {
 | 
|---|
| 91 |         for( unsigned i = 0; i < times; i++ ) {
 | 
|---|
| 92 |                 yield();
 | 
|---|
| 93 |         }
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
 | 
|---|
| 97 |         // set state of current coroutine to inactive
 | 
|---|
| 98 |         src->state = src->state == Halted ? Halted : Inactive;
 | 
|---|
| 99 |         dst->state = Active;
 | 
|---|
| 100 | 
 | 
|---|
| 101 |         //update the last resumer
 | 
|---|
| 102 |         dst->last = src;
 | 
|---|
| 103 | 
 | 
|---|
| 104 |         // set new coroutine that the processor is executing
 | 
|---|
| 105 |         // and context switch to it
 | 
|---|
| 106 |         this_coroutine = dst;
 | 
|---|
| 107 |         assert( src->stack.context );
 | 
|---|
| 108 |         CtxSwitch( src->stack.context, dst->stack.context );
 | 
|---|
| 109 |         this_coroutine = src;
 | 
|---|
| 110 | 
 | 
|---|
| 111 |         // set state of new coroutine to active
 | 
|---|
| 112 |         dst->state = dst->state == Halted ? Halted : Inactive;
 | 
|---|
| 113 |         src->state = Active;
 | 
|---|
| 114 | }
 | 
|---|
| 115 | 
 | 
|---|
| 116 | // Local Variables: //
 | 
|---|
| 117 | // mode: c //
 | 
|---|
| 118 | // tab-width: 4 //
 | 
|---|
| 119 | // End: //
 | 
|---|