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

Last change on this file since db19e1d was 3ef5905, checked in by Peter A. Buhr <pabuhr@…>, 15 months ago

add get_seed for thread random numbers

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