[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 | |
---|
| 25 | #include <stdlib> |
---|
| 26 | |
---|
| 27 | //----------------------------------------------------------------------------- |
---|
| 28 | // Forward declarations |
---|
| 29 | forall(otype T | is_thread(T) ) |
---|
| 30 | void start( thread(T)* this ); |
---|
| 31 | |
---|
| 32 | forall(otype T | is_thread(T) ) |
---|
| 33 | void stop( thread(T)* this ); |
---|
| 34 | |
---|
| 35 | //----------------------------------------------------------------------------- |
---|
| 36 | // Thread ctors and dtors |
---|
| 37 | |
---|
| 38 | void ?{}(thread_h* this) { |
---|
| 39 | (&this->c){}; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | void ^?{}(thread_h* this) { |
---|
| 43 | ^(&this->c){}; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | forall(otype T | is_thread(T) ) |
---|
| 47 | void ?{}( thread(T)* this ) { |
---|
| 48 | (&this->handle){}; |
---|
| 49 | start(this); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | forall(otype T, ttype P | is_thread(T) | { void ?{}(T*, P); } ) |
---|
| 53 | void ?{}( thread(T)* this, P params ) { |
---|
| 54 | (&this->handle){ params }; |
---|
| 55 | start(this); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | forall(otype T | is_thread(T) ) |
---|
| 59 | void ^?{}( thread(T)* this ) { |
---|
| 60 | stop(this); |
---|
| 61 | ^(&this->handle){}; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | //----------------------------------------------------------------------------- |
---|
| 65 | // Starting and stopping threads |
---|
| 66 | extern "C" { |
---|
| 67 | forall(dtype T | is_thread(T)) |
---|
| 68 | void CtxInvokeThread(T * this); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | forall(otype T | is_thread(T)) |
---|
| 72 | void start( thread(T)* this ) { |
---|
| 73 | T* handle = &this->handle; |
---|
| 74 | coroutine* thrd_c = get_coroutine(handle); |
---|
| 75 | thread_h* thrd_h = get_thread (handle); |
---|
| 76 | thrd_c->last = this_coroutine(); |
---|
| 77 | current_coroutine = thrd_c; |
---|
| 78 | |
---|
[c84e80a] | 79 | // LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h); |
---|
[8118303] | 80 | |
---|
| 81 | create_stack(&thrd_c->stack, thrd_c->stack.size); |
---|
| 82 | CtxStart(handle, CtxInvokeThread); |
---|
| 83 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); |
---|
| 84 | |
---|
[c84e80a] | 85 | scheduler_add(thrd_h); |
---|
[8118303] | 86 | } |
---|
| 87 | |
---|
| 88 | forall(otype T | is_thread(T) ) |
---|
| 89 | void stop( thread(T)* this ) { |
---|
[8f49a54] | 90 | T* handle = &this->handle; |
---|
| 91 | thread_h* thrd_h = get_thread (handle); |
---|
| 92 | while( thrd_h->c.notHalted ) { |
---|
| 93 | suspend(); |
---|
| 94 | } |
---|
[8118303] | 95 | } |
---|
[9129a84] | 96 | |
---|
[78b3f52] | 97 | // Local Variables: // |
---|
| 98 | // mode: c // |
---|
| 99 | // tab-width: 4 // |
---|
[6a3d2e7] | 100 | // End: // |
---|