[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" { |
---|
| 26 | #include <stddef.h> |
---|
| 27 | } |
---|
| 28 | |
---|
[8def349] | 29 | extern processor * get_this_processor(); |
---|
[8118303] | 30 | |
---|
| 31 | //----------------------------------------------------------------------------- |
---|
| 32 | // Forward declarations |
---|
[8def349] | 33 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
[bd98b58] | 34 | void start( T* this ); |
---|
[8118303] | 35 | |
---|
[8def349] | 36 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
[bd98b58] | 37 | void stop( T* this ); |
---|
[8118303] | 38 | |
---|
| 39 | //----------------------------------------------------------------------------- |
---|
| 40 | // Thread ctors and dtors |
---|
| 41 | |
---|
| 42 | void ?{}(thread_h* this) { |
---|
| 43 | (&this->c){}; |
---|
[8def349] | 44 | this->c.name = "Anonymous Coroutine"; |
---|
[bd98b58] | 45 | (&this->lock){}; |
---|
| 46 | this->next = NULL; |
---|
[8118303] | 47 | } |
---|
| 48 | |
---|
| 49 | void ^?{}(thread_h* this) { |
---|
| 50 | ^(&this->c){}; |
---|
| 51 | } |
---|
| 52 | |
---|
[8def349] | 53 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } ) |
---|
[8118303] | 54 | void ?{}( thread(T)* this ) { |
---|
| 55 | (&this->handle){}; |
---|
[bd98b58] | 56 | start(&this->handle); |
---|
[8118303] | 57 | } |
---|
| 58 | |
---|
[8def349] | 59 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } ) |
---|
[8118303] | 60 | void ?{}( thread(T)* this, P params ) { |
---|
| 61 | (&this->handle){ params }; |
---|
[bd98b58] | 62 | start(&this->handle); |
---|
[8118303] | 63 | } |
---|
| 64 | |
---|
[8def349] | 65 | forall( dtype T | sized(T) | is_thread(T) | { void ^?{}(T*); } ) |
---|
[8118303] | 66 | void ^?{}( thread(T)* this ) { |
---|
[bd98b58] | 67 | stop(&this->handle); |
---|
[8118303] | 68 | ^(&this->handle){}; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | //----------------------------------------------------------------------------- |
---|
| 72 | // Starting and stopping threads |
---|
| 73 | extern "C" { |
---|
| 74 | forall(dtype T | is_thread(T)) |
---|
| 75 | void CtxInvokeThread(T * this); |
---|
| 76 | } |
---|
| 77 | |
---|
[bd98b58] | 78 | extern void thread_schedule( thread_h * ); |
---|
| 79 | |
---|
[8def349] | 80 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
[bd98b58] | 81 | void start( T* this ) { |
---|
| 82 | coroutine* thrd_c = get_coroutine(this); |
---|
| 83 | thread_h* thrd_h = get_thread (this); |
---|
[8118303] | 84 | thrd_c->last = this_coroutine(); |
---|
[8def349] | 85 | get_this_processor()->current_coroutine = thrd_c; |
---|
[8118303] | 86 | |
---|
[c84e80a] | 87 | // LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h); |
---|
[8118303] | 88 | |
---|
| 89 | create_stack(&thrd_c->stack, thrd_c->stack.size); |
---|
[bd98b58] | 90 | CtxStart(this, CtxInvokeThread); |
---|
[8118303] | 91 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); |
---|
| 92 | |
---|
[bd98b58] | 93 | thread_schedule(thrd_h); |
---|
[8118303] | 94 | } |
---|
| 95 | |
---|
[8def349] | 96 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
[bd98b58] | 97 | void stop( T* this ) { |
---|
| 98 | thread_h* thrd = get_thread(this); |
---|
| 99 | if( thrd->c.notHalted ) { |
---|
| 100 | lock( &thrd->lock ); |
---|
[8f49a54] | 101 | } |
---|
[8118303] | 102 | } |
---|
[9129a84] | 103 | |
---|
[bd98b58] | 104 | void signal_termination( thread_h * this ) { |
---|
| 105 | this->c.state = Halt; |
---|
| 106 | this->c.notHalted = false; |
---|
| 107 | unlock( &this->lock ); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void yield( void ) { |
---|
| 111 | thread_schedule( this_thread() ); |
---|
| 112 | suspend(); |
---|
| 113 | } |
---|
| 114 | |
---|
[78b3f52] | 115 | // Local Variables: // |
---|
| 116 | // mode: c // |
---|
| 117 | // tab-width: 4 // |
---|
[6a3d2e7] | 118 | // End: // |
---|