source: libcfa/src/concurrency/thread.hfa @ 466787a

ADTast-experimental
Last change on this file since 466787a was 20cf96d, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

changes to PRNG types to eliminate casts

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