[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 | printf("thread() ctor\n");
|
---|
| 49 | (&this->handle){};
|
---|
| 50 | start(this);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | forall(otype T, ttype P | is_thread(T) | { void ?{}(T*, P); } )
|
---|
| 54 | void ?{}( thread(T)* this, P params ) {
|
---|
| 55 | (&this->handle){ params };
|
---|
| 56 | start(this);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | forall(otype T | is_thread(T) )
|
---|
| 60 | void ^?{}( thread(T)* this ) {
|
---|
| 61 | stop(this);
|
---|
| 62 | ^(&this->handle){};
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | //-----------------------------------------------------------------------------
|
---|
| 66 | // Starting and stopping threads
|
---|
| 67 | extern "C" {
|
---|
| 68 | forall(dtype T | is_thread(T))
|
---|
| 69 | void CtxInvokeThread(T * this);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | forall(otype T | is_thread(T))
|
---|
| 73 | void start( thread(T)* this ) {
|
---|
| 74 | T* handle = &this->handle;
|
---|
| 75 | coroutine* thrd_c = get_coroutine(handle);
|
---|
| 76 | thread_h* thrd_h = get_thread (handle);
|
---|
| 77 | thrd_c->last = this_coroutine();
|
---|
| 78 | current_coroutine = thrd_c;
|
---|
| 79 |
|
---|
[c84e80a] | 80 | // LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h);
|
---|
[8118303] | 81 |
|
---|
| 82 | create_stack(&thrd_c->stack, thrd_c->stack.size);
|
---|
| 83 | CtxStart(handle, CtxInvokeThread);
|
---|
| 84 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
|
---|
| 85 |
|
---|
[c84e80a] | 86 | scheduler_add(thrd_h);
|
---|
[8118303] | 87 | }
|
---|
| 88 |
|
---|
| 89 | forall(otype T | is_thread(T) )
|
---|
| 90 | void stop( thread(T)* this ) {
|
---|
| 91 |
|
---|
| 92 | }
|
---|
[9129a84] | 93 |
|
---|
[78b3f52] | 94 | // Local Variables: //
|
---|
| 95 | // mode: c //
|
---|
| 96 | // tab-width: 4 //
|
---|
[6a3d2e7] | 97 | // End: //
|
---|