| [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 | //
 | 
|---|
| [75a17f1] | 8 | // thread.c --
 | 
|---|
| [78b3f52] | 9 | //
 | 
|---|
 | 10 | // Author           : Thierry Delisle
 | 
|---|
| [f07e037] | 11 | // Created On       : Tue Jan 17 12:27:26 2017
 | 
|---|
| [78b3f52] | 12 | // Last Modified By : Thierry Delisle
 | 
|---|
| [6a3d2e7] | 13 | // Last Modified On : --
 | 
|---|
| [78b3f52] | 14 | // Update Count     : 0
 | 
|---|
 | 15 | //
 | 
|---|
 | 16 | 
 | 
|---|
| [75a17f1] | 17 | #include "thread"
 | 
|---|
| [78b3f52] | 18 | 
 | 
|---|
| [75f3522] | 19 | #include "kernel_private.h"
 | 
|---|
| [8118303] | 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 | 
 | 
|---|
| [4e6fb8e] | 30 | extern volatile thread_local processor * this_processor;
 | 
|---|
| [8118303] | 31 | 
 | 
|---|
 | 32 | //-----------------------------------------------------------------------------
 | 
|---|
 | 33 | // Thread ctors and dtors
 | 
|---|
 | 34 | 
 | 
|---|
| [348006f] | 35 | void ?{}(thread_desc* this) {
 | 
|---|
| [17af7d1] | 36 |         (&this->cor){};
 | 
|---|
 | 37 |         this->cor.name = "Anonymous Coroutine";
 | 
|---|
| [cb0e6de] | 38 |         this->mon.owner = this;
 | 
|---|
 | 39 |         this->mon.recursion = 1;
 | 
|---|
| [bd98b58] | 40 |         this->next = NULL;
 | 
|---|
| [5ea06d6] | 41 | 
 | 
|---|
| [9643b31] | 42 |         this->current_monitors      = &this->mon;
 | 
|---|
 | 43 |         this->current_monitor_count = 1;
 | 
|---|
| [8118303] | 44 | }
 | 
|---|
 | 45 | 
 | 
|---|
| [348006f] | 46 | void ^?{}(thread_desc* this) {
 | 
|---|
| [17af7d1] | 47 |         ^(&this->cor){};
 | 
|---|
| [8118303] | 48 | }
 | 
|---|
 | 49 | 
 | 
|---|
| [8def349] | 50 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
 | 
|---|
| [e15df4c] | 51 | void ?{}( scoped(T)* this ) {
 | 
|---|
| [8118303] | 52 |         (&this->handle){};
 | 
|---|
| [bd4d011] | 53 |         __thrd_start(&this->handle);
 | 
|---|
| [8118303] | 54 | }
 | 
|---|
 | 55 | 
 | 
|---|
| [8def349] | 56 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
 | 
|---|
| [e15df4c] | 57 | void ?{}( scoped(T)* this, P params ) {
 | 
|---|
| [8118303] | 58 |         (&this->handle){ params };
 | 
|---|
| [bd4d011] | 59 |         __thrd_start(&this->handle);
 | 
|---|
| [8118303] | 60 | }
 | 
|---|
 | 61 | 
 | 
|---|
| [9f1695b] | 62 | forall( dtype T | sized(T) | is_thread(T) )
 | 
|---|
| [e15df4c] | 63 | void ^?{}( scoped(T)* this ) {
 | 
|---|
| [8118303] | 64 |         ^(&this->handle){};
 | 
|---|
 | 65 | }
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 | //-----------------------------------------------------------------------------
 | 
|---|
 | 68 | // Starting and stopping threads
 | 
|---|
| [0c92c9f] | 69 | forall( dtype T | is_thread(T) )
 | 
|---|
| [bd4d011] | 70 | void __thrd_start( T* this ) {
 | 
|---|
| [c3acb841] | 71 |         coroutine_desc* thrd_c = get_coroutine(this);
 | 
|---|
| [348006f] | 72 |         thread_desc*  thrd_h = get_thread   (this);
 | 
|---|
| [1c273d0] | 73 |         thrd_c->last = this_coroutine;
 | 
|---|
| [8118303] | 74 | 
 | 
|---|
| [1c273d0] | 75 |         // LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
 | 
|---|
| [8118303] | 76 | 
 | 
|---|
| [1c273d0] | 77 |         disable_interrupts();
 | 
|---|
| [8118303] | 78 |         create_stack(&thrd_c->stack, thrd_c->stack.size);
 | 
|---|
| [1c273d0] | 79 |         this_coroutine = thrd_c;
 | 
|---|
| [bd98b58] | 80 |         CtxStart(this, CtxInvokeThread);
 | 
|---|
| [1c273d0] | 81 |         assert( thrd_c->last->stack.context );
 | 
|---|
| [8118303] | 82 |         CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
 | 
|---|
 | 83 | 
 | 
|---|
| [75f3522] | 84 |         ScheduleThread(thrd_h);
 | 
|---|
| [2ac095d] | 85 |         enable_interrupts( DEBUG_CTX );
 | 
|---|
| [8118303] | 86 | }
 | 
|---|
 | 87 | 
 | 
|---|
| [bd98b58] | 88 | void yield( void ) {
 | 
|---|
| [1c273d0] | 89 |         BlockInternal( (thread_desc *)this_thread );
 | 
|---|
| [bd98b58] | 90 | }
 | 
|---|
 | 91 | 
 | 
|---|
| [44264c5] | 92 | void yield( unsigned times ) {
 | 
|---|
 | 93 |         for( unsigned i = 0; i < times; i++ ) {
 | 
|---|
 | 94 |                 yield();
 | 
|---|
 | 95 |         }
 | 
|---|
 | 96 | }
 | 
|---|
 | 97 | 
 | 
|---|
| [c3acb841] | 98 | void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
 | 
|---|
| [0c92c9f] | 99 |         // set state of current coroutine to inactive
 | 
|---|
| [1c273d0] | 100 |         src->state = src->state == Halted ? Halted : Inactive;
 | 
|---|
| [75f3522] | 101 |         dst->state = Active;
 | 
|---|
| [0c92c9f] | 102 | 
 | 
|---|
| [75f3522] | 103 |         //update the last resumer
 | 
|---|
 | 104 |         dst->last = src;
 | 
|---|
| [0c92c9f] | 105 | 
 | 
|---|
| [75f3522] | 106 |         // set new coroutine that the processor is executing
 | 
|---|
 | 107 |         // and context switch to it
 | 
|---|
| [1c273d0] | 108 |         this_coroutine = dst;
 | 
|---|
 | 109 |         assert( src->stack.context );
 | 
|---|
| [0c92c9f] | 110 |         CtxSwitch( src->stack.context, dst->stack.context );
 | 
|---|
| [1c273d0] | 111 |         this_coroutine = src;
 | 
|---|
| [0c92c9f] | 112 | 
 | 
|---|
 | 113 |         // set state of new coroutine to active
 | 
|---|
| [1c273d0] | 114 |         dst->state = dst->state == Halted ? Halted : Inactive;
 | 
|---|
| [0c92c9f] | 115 |         src->state = Active;
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
| [78b3f52] | 118 | // Local Variables: //
 | 
|---|
 | 119 | // mode: c //
 | 
|---|
 | 120 | // tab-width: 4 //
 | 
|---|
| [6a3d2e7] | 121 | // End: //
 | 
|---|