source: libcfa/src/concurrency/thread.hfa @ ecfd758

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since ecfd758 was ecfd758, checked in by Andrew Beach <ajbeach@…>, 6 months ago

Major exception update, seperating type-ids from virtual tables. The major interface changes are done. There is a regression of ?Cancelled(T) to Some?Cancelled. There is some bits of code for the new verion of the ?Cancelled(T) interface already there. Not connected yet but I just reached the limit of what I wanted to do in one commit and then spent over a day cleaning up, so it will replace Some?Cancelled in a future commit.

  • Property mode set to 100644
File size: 5.2 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#include "exception.hfa"
25
26//-----------------------------------------------------------------------------
27// thread trait
28trait is_thread(T &) {
29        void ^?{}(T& mutex this);
30        void main(T& this);
31        $thread* get_thread(T& this);
32};
33
34EHM_EXCEPTION(SomeThreadCancelled) (
35        void * the_thread;
36        exception_t * the_exception;
37);
38
39EHM_EXTERN_VTABLE(SomeThreadCancelled, std_thread_cancelled);
40
41EHM_FORALL_EXCEPTION(ThreadCancelled, (thread_t &), (thread_t)) (
42        thread_t * the_thread;
43        exception_t * the_exception;
44);
45
46forall(T &)
47void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src);
48
49forall(T &)
50const char * msg(ThreadCancelled(T) *);
51
52// Inline getters for threads/coroutines/monitors
53forall( T & | is_thread(T) )
54static inline $coroutine* get_coroutine(T & this) __attribute__((const)) { return &get_thread(this)->self_cor; }
55
56forall( T & | is_thread(T) )
57static inline $monitor  * get_monitor  (T & this) __attribute__((const)) { return &get_thread(this)->self_mon; }
58
59static inline $coroutine* get_coroutine($thread * this) __attribute__((const)) { return &this->self_cor; }
60static inline $monitor  * get_monitor  ($thread * this) __attribute__((const)) { return &this->self_mon; }
61
62//-----------------------------------------------------------------------------
63// forward declarations needed for threads
64extern struct cluster * mainCluster;
65
66forall( T & | is_thread(T) )
67void __thrd_start( T & this, void (*)(T &) );
68
69//-----------------------------------------------------------------------------
70// Ctors and dtors
71void ?{}($thread & this, const char * const name, struct cluster & cl, void * storage, size_t storageSize );
72void ^?{}($thread & this);
73
74static inline void ?{}($thread & this)                                                                  { this{ "Anonymous Thread", *mainCluster, 0p, 65000 }; }
75static inline void ?{}($thread & this, size_t stackSize )                                               { this{ "Anonymous Thread", *mainCluster, 0p, stackSize }; }
76static inline void ?{}($thread & this, void * storage, size_t storageSize )                             { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; }
77static inline void ?{}($thread & this, struct cluster & cl )                                            { this{ "Anonymous Thread", cl, 0p, 65000 }; }
78static inline void ?{}($thread & this, struct cluster & cl, size_t stackSize )                          { this{ "Anonymous Thread", cl, 0p, stackSize }; }
79static inline void ?{}($thread & this, struct cluster & cl, void * storage, size_t storageSize )        { this{ "Anonymous Thread", cl, storage, storageSize }; }
80static inline void ?{}($thread & this, const char * const name)                                         { this{ name, *mainCluster, 0p, 65000 }; }
81static inline void ?{}($thread & this, const char * const name, struct cluster & cl )                   { this{ name, cl, 0p, 65000 }; }
82static inline void ?{}($thread & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, 0p, stackSize }; }
83
84struct thread_dtor_guard_t {
85        monitor_dtor_guard_t mg;
86};
87
88forall( T & | is_thread(T) | IS_EXCEPTION(SomeThreadCancelled) )
89void ?{}( thread_dtor_guard_t & this, T & thrd, void(*)(SomeThreadCancelled &) );
90void ^?{}( thread_dtor_guard_t & this );
91
92//-----------------------------------------------------------------------------
93// thread runner
94// Structure that actually start and stop threads
95forall( T & | sized(T) | is_thread(T) )
96struct scoped {
97        T handle;
98};
99
100forall( T & | sized(T) | is_thread(T) | { void ?{}(T&); } )
101void ?{}( scoped(T)& this );
102
103forall( T &, P... | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
104void ?{}( scoped(T)& this, P params );
105
106forall( T & | sized(T) | is_thread(T) )
107void ^?{}( scoped(T)& this );
108
109//-----------------------------------------------------------------------------
110// Scheduler API
111
112//----------
113// Park thread: block until corresponding call to unpark, won't block if unpark is already called
114void park( void );
115
116//----------
117// Unpark a thread, if the thread is already blocked, schedule it
118//                  if the thread is not yet block, signal that it should rerun immediately
119void unpark( $thread * this );
120
121forall( T & | is_thread(T) )
122static inline void unpark( T & this ) { if(!&this) return; unpark( get_thread( this ) );}
123
124//----------
125// Yield: force thread to block and be rescheduled
126bool force_yield( enum __Preemption_Reason );
127
128//----------
129// sleep: force thread to block and be rescheduled after Duration duration
130void sleep( Duration duration );
131
132//----------
133// join
134forall( T & | is_thread(T) | IS_RESUMPTION_EXCEPTION(SomeThreadCancelled) )
135T & join( T & this );
136
137// Local Variables: //
138// mode: c //
139// tab-width: 4 //
140// End: //
Note: See TracBrowser for help on using the repository browser.