source: libcfa/src/concurrency/thread.hfa @ 7039ab9

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 7039ab9 was b1d83ba, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Removed unused code for manual threads.

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