| 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 | //-----------------------------------------------------------------------------
|
|---|
| 59 | // Ctors and dtors
|
|---|
| 60 | void ?{}(thread_desc* this);
|
|---|
| 61 | void ^?{}(thread_desc* this);
|
|---|
| 62 |
|
|---|
| 63 | //-----------------------------------------------------------------------------
|
|---|
| 64 | // thread runner
|
|---|
| 65 | // Structure that actually start and stop threads
|
|---|
| 66 | forall( dtype T | sized(T) | is_thread(T) )
|
|---|
| 67 | struct scoped {
|
|---|
| 68 | T handle;
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
|
|---|
| 72 | void ?{}( scoped(T)* this );
|
|---|
| 73 |
|
|---|
| 74 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
|
|---|
| 75 | void ?{}( scoped(T)* this, P params );
|
|---|
| 76 |
|
|---|
| 77 | forall( dtype T | sized(T) | is_thread(T) )
|
|---|
| 78 | void ^?{}( scoped(T)* this );
|
|---|
| 79 |
|
|---|
| 80 | void yield();
|
|---|
| 81 |
|
|---|
| 82 | #endif //THREADS_H
|
|---|
| 83 |
|
|---|
| 84 | // Local Variables: //
|
|---|
| 85 | // mode: c //
|
|---|
| 86 | // tab-width: 4 //
|
|---|
| 87 | // End: //
|
|---|