source: libcfa/src/concurrency/thread.cfa @ 3ff64cb

ADTast-experimental
Last change on this file since 3ff64cb was 261e107, checked in by Peter A. Buhr <pabuhr@…>, 20 months ago

change all PRNG to prime the PRNG state in set_seed with the given seed

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[78b3f52]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//
[75a17f1]7// thread.c --
[78b3f52]8//
9// Author           : Thierry Delisle
[f07e037]10// Created On       : Tue Jan 17 12:27:26 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
[261e107]12// Last Modified On : Fri Dec  9 15:13:04 2022
13// Update Count     : 98
[78b3f52]14//
15
[2026bb6]16#define __cforall_thread__
[43784ac]17#define _GNU_SOURCE
[2026bb6]18
[58b6d1b]19#include "thread.hfa"
[78b3f52]20
[ab8c6a6]21#include "exception.hfa"
[b035046]22#include "kernel/private.hfa"
23#include "limits.hfa"
[8118303]24
25#define __CFA_INVOKE_PRIVATE__
26#include "invoke.h"
27
[dd46fd3]28extern size_t __global_random_seed;
29extern size_t __global_random_prime;
[d2ad151]30extern bool __global_random_mask;
[2210cfc]31
[c18bf9e]32#pragma GCC visibility push(default)
33
[8118303]34//-----------------------------------------------------------------------------
35// Thread ctors and dtors
[2210cfc]36void ?{}( thread$ & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) {
[121be3e]37        context{ 0p, 0p };
[de6319f]38        self_cor{ name, storage, storageSize };
[6a77224]39        ticket = TICKET_RUNNING;
[e8e457e]40        state = Start;
[3381ed7]41        preempted = __NO_PREEMPTION;
[ab5baab]42        corctx_flag = false;
[82c948c]43        curr_cor = &self_cor;
[65deb18]44        self_mon.owner = &this;
45        self_mon.recursion = 1;
46        self_mon_p = &self_mon;
[de6319f]47        curr_cluster = &cl;
[15c93d8]48        rdy_link.next = 0p;
49        rdy_link.ts   = MAX;
[1553a55]50        user_link.next = 0p;
51        user_link.prev = 0p;
52        cltr_link.next = 0p;
53        cltr_link.prev = 0p;
[24e321c]54        preferred = ready_queue_new_preferred();
[89eff25]55        last_proc = 0p;
[dd46fd3]56        PRNG_SET_SEED( random_state, __global_random_mask ? __global_random_prime : __global_random_prime ^ rdtscl() );
[b4b63e8]57        #if defined( __CFA_WITH_VERIFY__ )
[878cfcc]58                executing = 0p;
[ac12f1f]59                canary = 0x0D15EA5E0D15EA5Ep;
[b4b63e8]60        #endif
[de94a60]61
[015925a]62        clh_node = malloc( );
63        *clh_node = false;
[f835806]64
65        doregister(curr_cluster, this);
[65deb18]66        monitors{ &self_mon_p, 1, (fptr_t)0 };
[8118303]67}
68
[e84ab3d]69void ^?{}(thread$& this) with( this ) {
[b4b63e8]70        #if defined( __CFA_WITH_VERIFY__ )
[ac12f1f]71                canary = 0xDEADDEADDEADDEADp;
[b4b63e8]72        #endif
[015925a]73        free(clh_node);
[a1a17a74]74        unregister(curr_cluster, this);
[65deb18]75        ^self_cor{};
[8118303]76}
77
[fd54fef]78forall(T &)
[ab8c6a6]79void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src) {
80        dst->virtual_table = src->virtual_table;
81        dst->the_thread = src->the_thread;
82        dst->the_exception = src->the_exception;
83}
84
[fd54fef]85forall(T &)
[ab8c6a6]86const char * msg(ThreadCancelled(T) *) {
[ecfd758]87        return "ThreadCancelled(...)";
[ab8c6a6]88}
89
[fd54fef]90forall(T &)
[ab8c6a6]91static void default_thread_cancel_handler(ThreadCancelled(T) & ) {
[ecfd758]92        // Improve this error message, can I do formatting?
[ab8c6a6]93        abort( "Unhandled thread cancellation.\n" );
94}
95
[c3b9d639]96forall(T & | is_thread(T) | IS_EXCEPTION(ThreadCancelled(T))
97    | { EHM_DEFAULT_VTABLE(ThreadCancelled(T)); })
[ab8c6a6]98void ?{}( thread_dtor_guard_t & this,
[8edbe40]99                T & thrd, void(*cancelHandler)(ThreadCancelled(T) &)) {
[e84ab3d]100        monitor$ * m = get_monitor(thrd);
101        thread$ * desc = get_thread(thrd);
[3ea8ad1]102
103        // Setup the monitor guard
[ab8c6a6]104        void (*dtor)(T& mutex this) = ^?{};
[8edbe40]105        bool join = cancelHandler != (void(*)(ThreadCancelled(T)&))0;
[ab8c6a6]106        (this.mg){&m, (void(*)())dtor, join};
[342be43]107
[3ea8ad1]108
109        /* paranoid */ verifyf( Halted == desc->state || Cancelled == desc->state, "Expected thread to be Halted or Cancelled, was %d\n", (int)desc->state );
110
[342be43]111        // After the guard set-up and any wait, check for cancellation.
112        struct _Unwind_Exception * cancellation = desc->self_cor.cancellation;
113        if ( likely( 0p == cancellation ) ) {
114                return;
115        } else if ( Cancelled == desc->state ) {
116                return;
117        }
118        desc->state = Cancelled;
[8edbe40]119        void(*defaultResumptionHandler)(ThreadCancelled(T) &) =
[fcd0b9d7]120                join ? cancelHandler : default_thread_cancel_handler;
[342be43]121
122        // TODO: Remove explitate vtable set once trac#186 is fixed.
[8edbe40]123        ThreadCancelled(T) except;
124        except.virtual_table = &_default_vtable;
[342be43]125        except.the_thread = &thrd;
126        except.the_exception = __cfaehm_cancellation_exception( cancellation );
[ecfd758]127        // Why is this cast required?
[8edbe40]128        throwResume (ThreadCancelled(T) &)except;
[342be43]129
130        except.the_exception->virtual_table->free( except.the_exception );
131        free( cancellation );
132        desc->self_cor.cancellation = 0p;
[ab8c6a6]133}
134
135void ^?{}( thread_dtor_guard_t & this ) {
136        ^(this.mg){};
137}
138
[8118303]139//-----------------------------------------------------------------------------
140// Starting and stopping threads
[fd54fef]141forall( T & | is_thread(T) )
[09f357ec]142void __thrd_start( T & this, void (*main_p)(T &) ) {
[e84ab3d]143        thread$ * this_thrd = get_thread(this);
[8118303]144
[1c273d0]145        disable_interrupts();
[c7a900a]146        __cfactx_start(main_p, get_coroutine(this), this, __cfactx_invoke_thread);
[09f357ec]147
[e8e457e]148        this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP];
[ac5816d]149        /* paranoid */ verify( this_thrd->context.SP );
[8118303]150
[24e321c]151        schedule_thread$( this_thrd, UNPARK_LOCAL );
[a3821fa]152        enable_interrupts();
[8118303]153}
154
[8c50aed]155//-----------------------------------------------------------------------------
156// Support for threads that don't ues the thread keyword
[fd54fef]157forall( T & | sized(T) | is_thread(T) | { void ?{}(T&); } )
[65deb18]158void ?{}( scoped(T)& this ) with( this ) {
159        handle{};
[09f357ec]160        __thrd_start(handle, main);
[bd98b58]161}
162
[fd54fef]163forall( T &, P... | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
[65deb18]164void ?{}( scoped(T)& this, P params ) with( this ) {
165        handle{ params };
[09f357ec]166        __thrd_start(handle, main);
[8118303]167}
168
[fd54fef]169forall( T & | sized(T) | is_thread(T) )
[65deb18]170void ^?{}( scoped(T)& this ) with( this ) {
171        ^handle{};
[44264c5]172}
173
[ab8c6a6]174//-----------------------------------------------------------------------------
[c3b9d639]175forall(T & | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled(T))
176        | { EHM_DEFAULT_VTABLE(ThreadCancelled(T)); })
[ab8c6a6]177T & join( T & this ) {
178        thread_dtor_guard_t guard = { this, defaultResumptionHandler };
179        return this;
180}
181
[a167c70c]182//-----------------------------------------------------------------------------
183bool migrate( thread$ * thrd, struct cluster & cl ) {
184
185        monitor$ * tmon = get_monitor(thrd);
186        monitor$ * __monitors[] = { tmon };
187        monitor_guard_t __guard = { __monitors, 1 };
188
189
190        {
191                // if nothing needs to be done, return false
192                if( thrd->curr_cluster == &cl ) return false;
193
194                // are we migrating ourself?
195                const bool local = thrd == active_thread();
196
197                /* paranoid */ verify( !local || &cl != active_cluster() );
198                /* paranoid */ verify( !local || thrd->curr_cluster == active_cluster() );
199                /* paranoid */ verify( !local || thrd->curr_cluster == active_processor()->cltr );
200                /* paranoid */ verify( local || tmon->signal_stack.top->owner->waiting_thread == thrd );
201                /* paranoid */ verify( local || tmon->signal_stack.top );
202
203                // make sure we aren't interrupted while doing this
204                // not as important if we aren't local
205                disable_interrupts();
206
207                // actually move the thread
208                unregister( thrd->curr_cluster, *thrd );
209                thrd->curr_cluster = &cl;
210                doregister( thrd->curr_cluster, *thrd );
211
212                // restore interrupts
213                enable_interrupts();
214
215                // if this is the local thread, we are still running on the old cluster
216                if(local) yield();
217
218                /* paranoid */ verify( !local || &cl == active_cluster() );
219                /* paranoid */ verify( !local || thrd->curr_cluster == active_cluster() );
220                /* paranoid */ verify( !local || thrd->curr_cluster == active_processor()->cltr );
221                /* paranoid */ verify(  local || tmon->signal_stack.top );
222                /* paranoid */ verify(  local || tmon->signal_stack.top->owner->waiting_thread == thrd );
223
224                return true;
225        }
226}
227
[5d1ebb9]228//-----------------------------------------------------------------------------
[2210cfc]229
[20cf96d]230void set_seed( size_t seed ) {
[dd46fd3]231        PRNG_STATE_T & state = active_thread()->random_state;
232        PRNG_SET_SEED( state, seed );
[261e107]233        __global_random_seed = seed;
[dd46fd3]234        __global_random_prime = seed;
[c655650]235        __global_random_mask = true;
[00f5fde]236} // set_seed
[12b5e94a]237
[20cf96d]238size_t prng( void ) {                                                                   // [0,UINT_MAX]
[dd46fd3]239        PRNG_STATE_T( & state ) = active_thread()->random_state;
[d2ad151]240        return PRNG_NAME( state );
[12b5e94a]241} // prng
[2210cfc]242
[78b3f52]243// Local Variables: //
244// mode: c //
245// tab-width: 4 //
[6a3d2e7]246// End: //
Note: See TracBrowser for help on using the repository browser.