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