source: libcfa/src/concurrency/thread.hfa @ 04b4a71

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 04b4a71 was 2d8f7b0, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Implemented basic non-blocking io

  • Property mode set to 100644
File size: 5.1 KB
Line 
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 : Wed Dec  4 09:18:14 2019
13// Update Count     : 6
14//
15
16#pragma once
17
18#include <assert.h>
19#include "invoke.h"
20
21#include "coroutine.hfa"
22#include "kernel.hfa"
23#include "monitor.hfa"
24
25//-----------------------------------------------------------------------------
26// thread trait
27trait is_thread(dtype T) {
28      void ^?{}(T& mutex this);
29      void main(T& this);
30      $thread* get_thread(T& this);
31};
32
33// define that satisfies the trait without using the thread keyword
34#define DECL_THREAD(X) $thread* get_thread(X& this) __attribute__((const)) { return &this.__thrd; } void main(X& this)
35
36// Inline getters for threads/coroutines/monitors
37forall( dtype T | is_thread(T) )
38static inline $coroutine* get_coroutine(T & this) __attribute__((const)) { return &get_thread(this)->self_cor; }
39
40forall( dtype T | is_thread(T) )
41static inline $monitor  * get_monitor  (T & this) __attribute__((const)) { return &get_thread(this)->self_mon; }
42
43static inline $coroutine* get_coroutine($thread * this) __attribute__((const)) { return &this->self_cor; }
44static inline $monitor  * get_monitor  ($thread * this) __attribute__((const)) { return &this->self_mon; }
45
46//-----------------------------------------------------------------------------
47// forward declarations needed for threads
48extern struct cluster * mainCluster;
49
50forall( dtype T | is_thread(T) )
51void __thrd_start( T & this, void (*)(T &) );
52
53//-----------------------------------------------------------------------------
54// Ctors and dtors
55void ?{}($thread & this, const char * const name, struct cluster & cl, void * storage, size_t storageSize );
56void ^?{}($thread & this);
57
58static inline void ?{}($thread & this)                                                                  { this{ "Anonymous Thread", *mainCluster, 0p, 65000 }; }
59static inline void ?{}($thread & this, size_t stackSize )                                               { this{ "Anonymous Thread", *mainCluster, 0p, stackSize }; }
60static inline void ?{}($thread & this, void * storage, size_t storageSize )                             { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; }
61static inline void ?{}($thread & this, struct cluster & cl )                                            { this{ "Anonymous Thread", cl, 0p, 65000 }; }
62static inline void ?{}($thread & this, struct cluster & cl, size_t stackSize )                          { this{ "Anonymous Thread", cl, 0p, stackSize }; }
63static inline void ?{}($thread & this, struct cluster & cl, void * storage, size_t storageSize )        { this{ "Anonymous Thread", cl, storage, storageSize }; }
64static inline void ?{}($thread & this, const char * const name)                                         { this{ name, *mainCluster, 0p, 65000 }; }
65static inline void ?{}($thread & this, const char * const name, struct cluster & cl )                   { this{ name, cl, 0p, 65000 }; }
66static inline void ?{}($thread & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, 0p, stackSize }; }
67
68//-----------------------------------------------------------------------------
69// thread runner
70// Structure that actually start and stop threads
71forall( dtype T | sized(T) | is_thread(T) )
72struct scoped {
73        T handle;
74};
75
76forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } )
77void ?{}( scoped(T)& this );
78
79forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
80void ?{}( scoped(T)& this, P params );
81
82forall( dtype T | sized(T) | is_thread(T) )
83void ^?{}( scoped(T)& this );
84
85//-----------------------------------------------------------------------------
86// Thread getters
87static inline struct $thread * active_thread () { return TL_GET( this_thread ); }
88
89//-----------------------------------------------------------------------------
90// Scheduler API
91
92//----------
93// Park thread: block until corresponding call to unpark, won't block if unpark is already called
94void park( __cfaabi_dbg_ctx_param );
95
96//----------
97// Unpark a thread, if the thread is already blocked, schedule it
98//                  if the thread is not yet block, signal that it should rerun immediately
99void unpark( $thread * this __cfaabi_dbg_ctx_param2 );
100
101forall( dtype T | is_thread(T) )
102static inline void unpark( T & this __cfaabi_dbg_ctx_param2 ) { if(!&this) return; unpark( get_thread( this ) __cfaabi_dbg_ctx_fwd2 );}
103
104//----------
105// Yield: force thread to block and be rescheduled
106bool force_yield( enum __Preemption_Reason );
107
108static inline void yield() {
109        force_yield(__MANUAL_PREEMPTION);
110}
111
112// Yield: yield N times
113static inline void yield( unsigned times ) {
114        for( times ) {
115                yield();
116        }
117}
118
119//----------
120// sleep: force thread to block and be rescheduled after Duration duration
121void sleep( Duration duration );
122
123// Local Variables: //
124// mode: c //
125// tab-width: 4 //
126// End: //
Note: See TracBrowser for help on using the repository browser.