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