source: libcfa/src/concurrency/thread.hfa @ 3d937e2

ADTast-experimental
Last change on this file since 3d937e2 was 8a97248, checked in by Peter A. Buhr <pabuhr@…>, 22 months ago

switch from old trait syntax to new trait syntax using forall clause

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