source: libcfa/src/concurrency/thread.cfa @ b798713

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

Working ready queue

  • Property mode set to 100644
File size: 3.0 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
[b10affd]12// Last Modified On : Fri Mar 30 17:19:52 2018
13// Update Count     : 8
[78b3f52]14//
15
[2026bb6]16#define __cforall_thread__
17
[58b6d1b]18#include "thread.hfa"
[78b3f52]19
[73abe95]20#include "kernel_private.hfa"
[8118303]21
22#define __CFA_INVOKE_PRIVATE__
23#include "invoke.h"
24
[bd98b58]25extern "C" {
[8fcbb4c]26        #include <fenv.h>
[bd98b58]27        #include <stddef.h>
28}
29
[b10affd]30//extern volatile thread_local processor * this_processor;
[8118303]31
32//-----------------------------------------------------------------------------
33// Thread ctors and dtors
[de6319f]34void ?{}(thread_desc & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) {
[e8e457e]35        context{ NULL, NULL };
[de6319f]36        self_cor{ name, storage, storageSize };
[e8e457e]37        state = Start;
[82c948c]38        curr_cor = &self_cor;
[65deb18]39        self_mon.owner = &this;
40        self_mon.recursion = 1;
41        self_mon_p = &self_mon;
[de6319f]42        curr_cluster = &cl;
[b798713]43        link.next = 0p;
44        link.prev = 0p;
[de94a60]45
46        node.next = NULL;
47        node.prev = NULL;
[a1a17a7]48        doregister(curr_cluster, this);
[5ea06d6]49
[65deb18]50        monitors{ &self_mon_p, 1, (fptr_t)0 };
[8118303]51}
52
[65deb18]53void ^?{}(thread_desc& this) with( this ) {
[a1a17a7]54        unregister(curr_cluster, this);
[65deb18]55        ^self_cor{};
[8118303]56}
57
[242a902]58forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } )
[65deb18]59void ?{}( scoped(T)& this ) with( this ) {
60        handle{};
61        __thrd_start(handle);
[8118303]62}
63
[242a902]64forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
[65deb18]65void ?{}( scoped(T)& this, P params ) with( this ) {
66        handle{ params };
67        __thrd_start(handle);
[8118303]68}
69
[9f1695b]70forall( dtype T | sized(T) | is_thread(T) )
[65deb18]71void ^?{}( scoped(T)& this ) with( this ) {
72        ^handle{};
[8118303]73}
74
75//-----------------------------------------------------------------------------
76// Starting and stopping threads
[0c92c9f]77forall( dtype T | is_thread(T) )
[83a071f9]78void __thrd_start( T& this ) {
[e8e457e]79        thread_desc * this_thrd = get_thread(this);
80        thread_desc * curr_thrd = TL_GET( this_thread );
[8118303]81
[1c273d0]82        disable_interrupts();
[83a071f9]83        CtxStart(&this, CtxInvokeThread);
[e8e457e]84        this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP];
85        verify( this_thrd->context.SP );
86        CtxSwitch( &curr_thrd->context, &this_thrd->context );
[8118303]87
[e8e457e]88        ScheduleThread(this_thrd);
[36982fc]89        enable_interrupts( __cfaabi_dbg_ctx );
[8118303]90}
91
[b69ea6b]92extern "C" {
[14a61b5]93        // KERNEL ONLY
[e8e457e]94        void __finish_creation(thread_desc * this) {
95                // set new coroutine that the processor is executing
96                // and context switch to it
97                verify( kernelTLS.this_thread != this );
98                verify( kernelTLS.this_thread->context.SP );
99                CtxSwitch( &this->context, &kernelTLS.this_thread->context );
[b69ea6b]100        }
101}
102
[bd98b58]103void yield( void ) {
[14a61b5]104        // Safety note : This could cause some false positives due to preemption
[afd550c]105      verify( TL_GET( preemption_state.enabled ) );
[b10affd]106        BlockInternal( TL_GET( this_thread ) );
[14a61b5]107        // Safety note : This could cause some false positives due to preemption
[afd550c]108      verify( TL_GET( preemption_state.enabled ) );
[bd98b58]109}
110
[44264c5]111void yield( unsigned times ) {
112        for( unsigned i = 0; i < times; i++ ) {
113                yield();
114        }
115}
116
[78b3f52]117// Local Variables: //
118// mode: c //
119// tab-width: 4 //
[6a3d2e7]120// End: //
Note: See TracBrowser for help on using the repository browser.