| 1 | //                              -*- Mode: CFA -*- | 
|---|
| 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 | // | 
|---|
| 8 | // thread -- | 
|---|
| 9 | // | 
|---|
| 10 | // Author           : Thierry Delisle | 
|---|
| 11 | // Created On       : Tue Jan 17 12:27:26 2017 | 
|---|
| 12 | // Last Modified By : Thierry Delisle | 
|---|
| 13 | // Last Modified On : -- | 
|---|
| 14 | // Update Count     : 0 | 
|---|
| 15 | // | 
|---|
| 16 |  | 
|---|
| 17 | #ifndef THREADS_H | 
|---|
| 18 | #define THREADS_H | 
|---|
| 19 |  | 
|---|
| 20 | #include "assert" | 
|---|
| 21 | #include "invoke.h" | 
|---|
| 22 |  | 
|---|
| 23 | #include "coroutine" | 
|---|
| 24 | #include "monitor" | 
|---|
| 25 |  | 
|---|
| 26 | //----------------------------------------------------------------------------- | 
|---|
| 27 | // Coroutine trait | 
|---|
| 28 | // Anything that implements this trait can be resumed. | 
|---|
| 29 | // Anything that is resumed is a coroutine. | 
|---|
| 30 | trait is_thread(dtype T) { | 
|---|
| 31 | void ^?{}(T* mutex this); | 
|---|
| 32 | void main(T* this); | 
|---|
| 33 | thread_desc* get_thread(T* this); | 
|---|
| 34 | }; | 
|---|
| 35 |  | 
|---|
| 36 | #define DECL_THREAD(X) thread_desc* get_thread(X* this) { return &this->__thrd; } void main(X* this) | 
|---|
| 37 |  | 
|---|
| 38 | forall( dtype T | is_thread(T) ) | 
|---|
| 39 | static inline coroutine_desc* get_coroutine(T* this) { | 
|---|
| 40 | return &get_thread(this)->cor; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 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) { | 
|---|
| 49 | return &this->cor; | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | static inline monitor_desc* get_monitor(thread_desc * this) { | 
|---|
| 53 | return &this->mon; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | thread_desc * this_thread(void); | 
|---|
| 57 |  | 
|---|
| 58 | forall( dtype T | is_thread(T) ) | 
|---|
| 59 | void __thrd_start( T* this ); | 
|---|
| 60 |  | 
|---|
| 61 | //----------------------------------------------------------------------------- | 
|---|
| 62 | // Ctors and dtors | 
|---|
| 63 | void ?{}(thread_desc* this); | 
|---|
| 64 | void ^?{}(thread_desc* this); | 
|---|
| 65 |  | 
|---|
| 66 | //----------------------------------------------------------------------------- | 
|---|
| 67 | // thread runner | 
|---|
| 68 | // Structure that actually start and stop threads | 
|---|
| 69 | forall( dtype T | sized(T) | is_thread(T) ) | 
|---|
| 70 | struct scoped { | 
|---|
| 71 | T handle; | 
|---|
| 72 | }; | 
|---|
| 73 |  | 
|---|
| 74 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } ) | 
|---|
| 75 | void ?{}( scoped(T)* this ); | 
|---|
| 76 |  | 
|---|
| 77 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } ) | 
|---|
| 78 | void ?{}( scoped(T)* this, P params ); | 
|---|
| 79 |  | 
|---|
| 80 | forall( dtype T | sized(T) | is_thread(T) ) | 
|---|
| 81 | void ^?{}( scoped(T)* this ); | 
|---|
| 82 |  | 
|---|
| 83 | void yield(); | 
|---|
| 84 | void yield( unsigned times ); | 
|---|
| 85 |  | 
|---|
| 86 | #endif //THREADS_H | 
|---|
| 87 |  | 
|---|
| 88 | // Local Variables: // | 
|---|
| 89 | // mode: c // | 
|---|
| 90 | // tab-width: 4 // | 
|---|
| 91 | // End: // | 
|---|