| 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 volatile 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 | this->current_monitors = &this->mon;
|
|---|
| 43 | this->current_monitor_count = 1;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | void ^?{}(thread_desc* this) {
|
|---|
| 47 | ^(&this->cor){};
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
|
|---|
| 51 | void ?{}( scoped(T)* this ) {
|
|---|
| 52 | (&this->handle){};
|
|---|
| 53 | __thrd_start(&this->handle);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
|
|---|
| 57 | void ?{}( scoped(T)* this, P params ) {
|
|---|
| 58 | (&this->handle){ params };
|
|---|
| 59 | __thrd_start(&this->handle);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | forall( dtype T | sized(T) | is_thread(T) )
|
|---|
| 63 | void ^?{}( scoped(T)* this ) {
|
|---|
| 64 | ^(&this->handle){};
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | //-----------------------------------------------------------------------------
|
|---|
| 68 | // Starting and stopping threads
|
|---|
| 69 | forall( dtype T | is_thread(T) )
|
|---|
| 70 | void __thrd_start( T* this ) {
|
|---|
| 71 | coroutine_desc* thrd_c = get_coroutine(this);
|
|---|
| 72 | thread_desc* thrd_h = get_thread (this);
|
|---|
| 73 | thrd_c->last = this_coroutine;
|
|---|
| 74 |
|
|---|
| 75 | // LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
|
|---|
| 76 |
|
|---|
| 77 | disable_interrupts();
|
|---|
| 78 | create_stack(&thrd_c->stack, thrd_c->stack.size);
|
|---|
| 79 | this_coroutine = thrd_c;
|
|---|
| 80 | CtxStart(this, CtxInvokeThread);
|
|---|
| 81 | assert( thrd_c->last->stack.context );
|
|---|
| 82 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
|
|---|
| 83 |
|
|---|
| 84 | ScheduleThread(thrd_h);
|
|---|
| 85 | enable_interrupts( DEBUG_CTX );
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void yield( void ) {
|
|---|
| 89 | BlockInternal( (thread_desc *)this_thread );
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | void yield( unsigned times ) {
|
|---|
| 93 | for( unsigned i = 0; i < times; i++ ) {
|
|---|
| 94 | yield();
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
|
|---|
| 99 | // set state of current coroutine to inactive
|
|---|
| 100 | src->state = src->state == Halted ? Halted : Inactive;
|
|---|
| 101 | dst->state = Active;
|
|---|
| 102 |
|
|---|
| 103 | //update the last resumer
|
|---|
| 104 | dst->last = src;
|
|---|
| 105 |
|
|---|
| 106 | // set new coroutine that the processor is executing
|
|---|
| 107 | // and context switch to it
|
|---|
| 108 | this_coroutine = dst;
|
|---|
| 109 | assert( src->stack.context );
|
|---|
| 110 | CtxSwitch( src->stack.context, dst->stack.context );
|
|---|
| 111 | this_coroutine = src;
|
|---|
| 112 |
|
|---|
| 113 | // set state of new coroutine to active
|
|---|
| 114 | dst->state = dst->state == Halted ? Halted : Inactive;
|
|---|
| 115 | src->state = Active;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | // Local Variables: //
|
|---|
| 119 | // mode: c //
|
|---|
| 120 | // tab-width: 4 //
|
|---|
| 121 | // End: //
|
|---|