source: libcfa/src/concurrency/thread.hfa @ 5d3d281

Last change on this file since 5d3d281 was 5d3d281, checked in by Michael Brooks <mlbrooks@…>, 8 days ago

Remove autogen forward declarations, which are never needed, and cause warnings about static declarations without definitions.

Intended to fix the failing test from previous commit.

Autogen forward declarations are never needed because they do not depend on each other, much less with mutual recursion.

Consequences:

  • tests/.expect/(5 tests).(3 archs).txt: Accept generated code that lacks autogen forward declarations
  • libcfa/src/concurrency/thread.*: Remove unused dependency on destructor from constructor (via thrd_start), by splitting trait is_thread with is_basic_thread
  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[0e76cf4f]1//
[78b3f52]2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
[0e76cf4f]3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[75a17f1]7// thread --
[0e76cf4f]8//
[78b3f52]9// Author           : Thierry Delisle
[f07e037]10// Created On       : Tue Jan 17 12:27:26 2017
[91c389a]11// Last Modified By : Peter A. Buhr
[3ef5905]12// Last Modified On : Sun Sep  3 07:34:43 2023
13// Update Count     : 47
[0e76cf4f]14//
15
[6b0b624]16#pragma once
[0e76cf4f]17
[91c389a]18#include <assert.h>
[8118303]19#include "invoke.h"
[78b3f52]20
[58b6d1b]21#include "coroutine.hfa"
22#include "kernel.hfa"
23#include "monitor.hfa"
[ab8c6a6]24#include "exception.hfa"
[d2ad151]25#include "bits/random.hfa"
[8118303]26
27//-----------------------------------------------------------------------------
[de6319f]28// thread trait
[8a97248]29forall( T & )
[5d3d281]30trait is_basic_thread {
[ab8c6a6]31        void main(T& this);
[e84ab3d]32        thread$ * get_thread(T& this);
[8118303]33};
[5d3d281]34forall( T & | is_basic_thread(T) )
35trait is_thread {
36        void ^?{}(T& mutex this);
37};
[8118303]38
[c715e5f]39forall(thread_t &)
40exception ThreadCancelled {
[ab8c6a6]41        thread_t * the_thread;
42        exception_t * the_exception;
[c715e5f]43};
[ab8c6a6]44
[fd54fef]45forall(T &)
[ab8c6a6]46void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src);
47
[fd54fef]48forall(T &)
[ab8c6a6]49const char * msg(ThreadCancelled(T) *);
50
[8c50aed]51// Inline getters for threads/coroutines/monitors
[5d3d281]52forall( T & | is_basic_thread(T) )
[e84ab3d]53static inline coroutine$ * get_coroutine(T & this) __attribute__((const)) { return &get_thread(this)->self_cor; }
[8118303]54
[5d3d281]55forall( T & | is_basic_thread(T) )
[e84ab3d]56static inline monitor$   * get_monitor  (T & this) __attribute__((const)) { return &get_thread(this)->self_mon; }
[cb0e6de]57
[e84ab3d]58static inline coroutine$ * get_coroutine(thread$ * this) __attribute__((const)) { return &this->self_cor; }
59static inline monitor$   * get_monitor  (thread$ * this) __attribute__((const)) { return &this->self_mon; }
[cb0e6de]60
[8c50aed]61//-----------------------------------------------------------------------------
62// forward declarations needed for threads
[de6319f]63extern struct cluster * mainCluster;
[bd98b58]64
[5d3d281]65forall( T & | is_basic_thread(T) )
[09f357ec]66void __thrd_start( T & this, void (*)(T &) );
[bd4d011]67
[8118303]68//-----------------------------------------------------------------------------
69// Ctors and dtors
[e84ab3d]70void ?{}(thread$ & this, const char * const name, struct cluster & cl, void * storage, size_t storageSize );
71void ^?{}(thread$ & this);
72
[eaf269d]73static inline void ?{}(thread$ & this)                                                                  { this{ "Anonymous Thread", *mainCluster, 0p, DEFAULT_STACK_SIZE }; }
[e84ab3d]74static inline void ?{}(thread$ & this, size_t stackSize )                                               { this{ "Anonymous Thread", *mainCluster, 0p, stackSize }; }
75static inline void ?{}(thread$ & this, void * storage, size_t storageSize )                             { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; }
[eaf269d]76static inline void ?{}(thread$ & this, struct cluster & cl )                                            { this{ "Anonymous Thread", cl, 0p, DEFAULT_STACK_SIZE }; }
[e84ab3d]77static inline void ?{}(thread$ & this, struct cluster & cl, size_t stackSize )                          { this{ "Anonymous Thread", cl, 0p, stackSize }; }
78static inline void ?{}(thread$ & this, struct cluster & cl, void * storage, size_t storageSize )        { this{ "Anonymous Thread", cl, storage, storageSize }; }
[eaf269d]79static inline void ?{}(thread$ & this, const char * const name)                                         { this{ name, *mainCluster, 0p, DEFAULT_STACK_SIZE }; }
80static inline void ?{}(thread$ & this, const char * const name, struct cluster & cl )                   { this{ name, cl, 0p, DEFAULT_STACK_SIZE }; }
[e84ab3d]81static inline void ?{}(thread$ & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, 0p, stackSize }; }
[8118303]82
[ab8c6a6]83struct thread_dtor_guard_t {
84        monitor_dtor_guard_t mg;
85};
86
[c3b9d639]87forall( T & | is_thread(T) | IS_EXCEPTION(ThreadCancelled(T))
88        | { EHM_DEFAULT_VTABLE(ThreadCancelled(T)); })
[8edbe40]89void ?{}( thread_dtor_guard_t & this, T & thrd, void(*)(ThreadCancelled(T) &) );
[ab8c6a6]90void ^?{}( thread_dtor_guard_t & this );
91
[8118303]92//-----------------------------------------------------------------------------
93// thread runner
94// Structure that actually start and stop threads
[fd54fef]95forall( T & | sized(T) | is_thread(T) )
[e15df4c]96struct scoped {
[8118303]97        T handle;
98};
99
[fd54fef]100forall( T & | sized(T) | is_thread(T) | { void ?{}(T&); } )
[242a902]101void ?{}( scoped(T)& this );
[8118303]102
[fd54fef]103forall( T &, P... | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
[242a902]104void ?{}( scoped(T)& this, P params );
[8118303]105
[fd54fef]106forall( T & | sized(T) | is_thread(T) )
[242a902]107void ^?{}( scoped(T)& this );
[8118303]108
[3381ed7]109//-----------------------------------------------------------------------------
110// Scheduler API
111
112//----------
113// Park thread: block until corresponding call to unpark, won't block if unpark is already called
[e235429]114void park( void );
[3381ed7]115
116//----------
117// Unpark a thread, if the thread is already blocked, schedule it
[b0c7419]118//                  if the thread is not yet block, signal that it should rerun immediately
[e84ab3d]119void unpark( thread$ * this );
[3381ed7]120
[fd54fef]121forall( T & | is_thread(T) )
[e235429]122static inline void unpark( T & this ) { if(!&this) return; unpark( get_thread( this ) );}
[3381ed7]123
124//----------
125// Yield: force thread to block and be rescheduled
[b0c7419]126bool force_yield( enum __Preemption_Reason );
127
[2d8f7b0]128//----------
129// sleep: force thread to block and be rescheduled after Duration duration
130void sleep( Duration duration );
131
[77a2994]132//----------
133// join
[c3b9d639]134forall( T & | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled(T))
135        | { EHM_DEFAULT_VTABLE(ThreadCancelled(T)); })
[77a2994]136T & join( T & this );
137
[a167c70c]138//----------
139// misc
140bool migrate( thread$ * thrd, struct cluster & cl );
141
142forall( T & | is_thread(T) )
143static inline bool migrate( T & mutex thrd, struct cluster & cl ) { return migrate( &(thread&)thrd, cl ); }
144
145
[12b5e94a]146//----------
[c655650]147// prng
[20cf96d]148void set_seed( size_t seed );
[3ef5905]149size_t get_seed( void ) __attribute__(( warn_unused_result ));
[12b5e94a]150static inline {
[20cf96d]151        size_t prng( thread$ & th ) __attribute__(( warn_unused_result )) { return PRNG_NAME( th.random_state ); } // [0,UINT_MAX]
152        size_t prng( thread$ & th, size_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u)
153        size_t prng( thread$ & th, size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u]
[c655650]154        forall( T & | is_thread(T) ) {
[20cf96d]155                size_t prng( T & th ) __attribute__(( warn_unused_result )) { return prng( (thread &)th ); } // [0,UINT_MAX]
156                size_t prng( T & th, size_t u ) __attribute__(( warn_unused_result )) { return prng( th ) % u; } // [0,u)
157                size_t prng( T & th, size_t l, size_t u ) __attribute__(( warn_unused_result )) { return prng( th, u - l + 1 ) + l; } // [l,u]
[c655650]158        } // distribution
159} // distribution
[12b5e94a]160
[78b3f52]161// Local Variables: //
162// mode: c //
163// tab-width: 4 //
164// End: //
Note: See TracBrowser for help on using the repository browser.