| [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 | 
 | 
|---|
| [bd4d011] | 58 | forall( dtype T | is_thread(T) )
 | 
|---|
 | 59 | void __thrd_start( T* this );
 | 
|---|
 | 60 | 
 | 
|---|
| [8118303] | 61 | //-----------------------------------------------------------------------------
 | 
|---|
 | 62 | // Ctors and dtors
 | 
|---|
| [348006f] | 63 | void ?{}(thread_desc* this);
 | 
|---|
 | 64 | void ^?{}(thread_desc* this);
 | 
|---|
| [8118303] | 65 | 
 | 
|---|
 | 66 | //-----------------------------------------------------------------------------
 | 
|---|
 | 67 | // thread runner
 | 
|---|
 | 68 | // Structure that actually start and stop threads
 | 
|---|
| [8def349] | 69 | forall( dtype T | sized(T) | is_thread(T) )
 | 
|---|
| [e15df4c] | 70 | struct scoped {
 | 
|---|
| [8118303] | 71 |         T handle;
 | 
|---|
 | 72 | };
 | 
|---|
 | 73 | 
 | 
|---|
| [8def349] | 74 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
 | 
|---|
| [e15df4c] | 75 | void ?{}( scoped(T)* this );
 | 
|---|
| [8118303] | 76 | 
 | 
|---|
| [8def349] | 77 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
 | 
|---|
| [e15df4c] | 78 | void ?{}( scoped(T)* this, P params );
 | 
|---|
| [8118303] | 79 | 
 | 
|---|
| [9f1695b] | 80 | forall( dtype T | sized(T) | is_thread(T) )
 | 
|---|
| [e15df4c] | 81 | void ^?{}( scoped(T)* this );
 | 
|---|
| [8118303] | 82 | 
 | 
|---|
| [bd98b58] | 83 | void yield();
 | 
|---|
| [44264c5] | 84 | void yield( unsigned times );
 | 
|---|
| [596f987b] | 85 | 
 | 
|---|
| [17e5e2b] | 86 | #endif //THREADS_H
 | 
|---|
 | 87 | 
 | 
|---|
| [78b3f52] | 88 | // Local Variables: //
 | 
|---|
 | 89 | // mode: c //
 | 
|---|
 | 90 | // tab-width: 4 //
 | 
|---|
 | 91 | // End: //
 | 
|---|