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

ADTast-experimental
Last change on this file since a46f7b6 was c3b9d639, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Clean-up the exception interface. It should be slightly more like the final - non-macro - interface.

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