[78b3f52] | 1 | // -*- Mode: CFA -*-
|
---|
[0e76cf4f] | 2 | //
|
---|
[78b3f52] | 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
---|
[0e76cf4f] | 4 | //
|
---|
| 5 | // The contents of this file are covered under the licence agreement in the
|
---|
| 6 | // file "LICENCE" distributed with Cforall.
|
---|
| 7 | //
|
---|
[75a17f1] | 8 | // thread --
|
---|
[0e76cf4f] | 9 | //
|
---|
[78b3f52] | 10 | // Author : Thierry Delisle
|
---|
[f07e037] | 11 | // Created On : Tue Jan 17 12:27:26 2017
|
---|
[78b3f52] | 12 | // Last Modified By : Thierry Delisle
|
---|
[6a3d2e7] | 13 | // Last Modified On : --
|
---|
[78b3f52] | 14 | // Update Count : 0
|
---|
[0e76cf4f] | 15 | //
|
---|
| 16 |
|
---|
[17e5e2b] | 17 | #ifndef THREADS_H
|
---|
| 18 | #define THREADS_H
|
---|
[0e76cf4f] | 19 |
|
---|
[8118303] | 20 | #include "assert"
|
---|
| 21 | #include "invoke.h"
|
---|
[78b3f52] | 22 |
|
---|
[75a17f1] | 23 | #include "coroutine"
|
---|
[cb0e6de] | 24 | #include "monitor"
|
---|
[8118303] | 25 |
|
---|
| 26 | //-----------------------------------------------------------------------------
|
---|
| 27 | // Coroutine trait
|
---|
| 28 | // Anything that implements this trait can be resumed.
|
---|
| 29 | // Anything that is resumed is a coroutine.
|
---|
[0c92c9f] | 30 | trait is_thread(dtype T) {
|
---|
[cb0e6de] | 31 | void ^?{}(T* mutex this);
|
---|
[7fbe450] | 32 | void main(T* this);
|
---|
[348006f] | 33 | thread_desc* get_thread(T* this);
|
---|
[8118303] | 34 | };
|
---|
| 35 |
|
---|
[17af7d1] | 36 | #define DECL_THREAD(X) thread_desc* get_thread(X* this) { return &this->__thrd; } void main(X* this)
|
---|
[8f49a54] | 37 |
|
---|
[0c92c9f] | 38 | forall( dtype T | is_thread(T) )
|
---|
[c3acb841] | 39 | static inline coroutine_desc* get_coroutine(T* this) {
|
---|
[17af7d1] | 40 | return &get_thread(this)->cor;
|
---|
[8118303] | 41 | }
|
---|
| 42 |
|
---|
[cb0e6de] | 43 | forall( dtype T | is_thread(T) )
|
---|
| 44 | static inline monitor_desc* get_monitor(T * this) {
|
---|
| 45 | return &get_thread(this)->mon;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | static inline coroutine_desc* get_coroutine(thread_desc * this) {
|
---|
[17af7d1] | 49 | return &this->cor;
|
---|
[c84e80a] | 50 | }
|
---|
| 51 |
|
---|
[cb0e6de] | 52 | static inline monitor_desc* get_monitor(thread_desc * this) {
|
---|
| 53 | return &this->mon;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[348006f] | 56 | thread_desc * this_thread(void);
|
---|
[bd98b58] | 57 |
|
---|
[8118303] | 58 | //-----------------------------------------------------------------------------
|
---|
| 59 | // Ctors and dtors
|
---|
[348006f] | 60 | void ?{}(thread_desc* this);
|
---|
| 61 | void ^?{}(thread_desc* this);
|
---|
[8118303] | 62 |
|
---|
| 63 | //-----------------------------------------------------------------------------
|
---|
| 64 | // thread runner
|
---|
| 65 | // Structure that actually start and stop threads
|
---|
[8def349] | 66 | forall( dtype T | sized(T) | is_thread(T) )
|
---|
[e15df4c] | 67 | struct scoped {
|
---|
[8118303] | 68 | T handle;
|
---|
| 69 | };
|
---|
| 70 |
|
---|
[8def349] | 71 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
|
---|
[e15df4c] | 72 | void ?{}( scoped(T)* this );
|
---|
[8118303] | 73 |
|
---|
[8def349] | 74 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
|
---|
[e15df4c] | 75 | void ?{}( scoped(T)* this, P params );
|
---|
[8118303] | 76 |
|
---|
[9f1695b] | 77 | forall( dtype T | sized(T) | is_thread(T) )
|
---|
[e15df4c] | 78 | void ^?{}( scoped(T)* this );
|
---|
[8118303] | 79 |
|
---|
[bd98b58] | 80 | void yield();
|
---|
[596f987b] | 81 |
|
---|
[17e5e2b] | 82 | #endif //THREADS_H
|
---|
| 83 |
|
---|
[78b3f52] | 84 | // Local Variables: //
|
---|
| 85 | // mode: c //
|
---|
| 86 | // tab-width: 4 //
|
---|
| 87 | // End: //
|
---|