source: src/libcfa/concurrency/thread.c @ 7416d46a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 7416d46a was 7416d46a, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • 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
12// Last Modified On : Fri Jul 21 22:34:46 2017
13// Update Count     : 1
[78b3f52]14//
15
[75a17f1]16#include "thread"
[78b3f52]17
[75f3522]18#include "kernel_private.h"
[8118303]19
20#define __CFA_INVOKE_PRIVATE__
21#include "invoke.h"
22
[bd98b58]23extern "C" {
[8fcbb4c]24        #include <fenv.h>
[bd98b58]25        #include <stddef.h>
26}
27
[4e6fb8e]28extern volatile thread_local processor * this_processor;
[8118303]29
30//-----------------------------------------------------------------------------
31// Thread ctors and dtors
32
[65deb18]33void ?{}(thread_desc& this) with( this ) {
34        self_cor{};
35        self_cor.name = "Anonymous Coroutine";
36        self_mon.owner = &this;
37        self_mon.recursion = 1;
38        self_mon_p = &self_mon;
39        next = NULL;
[f7d6bb0]40        __cfaabi_dbg_debug_do(
[7416d46a]41                dbg_next = NULL;
42                dbg_prev = NULL;
[f7d6bb0]43                __cfaabi_dbg_thread_register(&this);
44        )
[5ea06d6]45
[65deb18]46        monitors{ &self_mon_p, 1, (fptr_t)0 };
[8118303]47}
48
[65deb18]49void ^?{}(thread_desc& this) with( this ) {
50        ^self_cor{};
[8118303]51}
52
[242a902]53forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } )
[65deb18]54void ?{}( scoped(T)& this ) with( this ) {
55        handle{};
56        __thrd_start(handle);
[8118303]57}
58
[242a902]59forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
[65deb18]60void ?{}( scoped(T)& this, P params ) with( this ) {
61        handle{ params };
62        __thrd_start(handle);
[8118303]63}
64
[9f1695b]65forall( dtype T | sized(T) | is_thread(T) )
[65deb18]66void ^?{}( scoped(T)& this ) with( this ) {
67        ^handle{};
[8118303]68}
69
70//-----------------------------------------------------------------------------
71// Starting and stopping threads
[0c92c9f]72forall( dtype T | is_thread(T) )
[83a071f9]73void __thrd_start( T& this ) {
[c3acb841]74        coroutine_desc* thrd_c = get_coroutine(this);
[65deb18]75        thread_desc   * thrd_h = get_thread   (this);
[1c273d0]76        thrd_c->last = this_coroutine;
[8118303]77
[36982fc]78        // __cfaabi_dbg_print_safe("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
[8118303]79
[1c273d0]80        disable_interrupts();
[8118303]81        create_stack(&thrd_c->stack, thrd_c->stack.size);
[1c273d0]82        this_coroutine = thrd_c;
[83a071f9]83        CtxStart(&this, CtxInvokeThread);
[1c273d0]84        assert( thrd_c->last->stack.context );
[8118303]85        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
86
[75f3522]87        ScheduleThread(thrd_h);
[36982fc]88        enable_interrupts( __cfaabi_dbg_ctx );
[8118303]89}
90
[bd98b58]91void yield( void ) {
[3175147]92        BlockInternal( this_thread );
[bd98b58]93}
94
[44264c5]95void yield( unsigned times ) {
96        for( unsigned i = 0; i < times; i++ ) {
97                yield();
98        }
99}
100
[c3acb841]101void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
[0c92c9f]102        // set state of current coroutine to inactive
[1c273d0]103        src->state = src->state == Halted ? Halted : Inactive;
[75f3522]104        dst->state = Active;
[0c92c9f]105
[75f3522]106        //update the last resumer
107        dst->last = src;
[0c92c9f]108
[75f3522]109        // set new coroutine that the processor is executing
110        // and context switch to it
[1c273d0]111        this_coroutine = dst;
112        assert( src->stack.context );
[0c92c9f]113        CtxSwitch( src->stack.context, dst->stack.context );
[1c273d0]114        this_coroutine = src;
[0c92c9f]115
116        // set state of new coroutine to active
[1c273d0]117        dst->state = dst->state == Halted ? Halted : Inactive;
[0c92c9f]118        src->state = Active;
119}
120
[78b3f52]121// Local Variables: //
122// mode: c //
123// tab-width: 4 //
[6a3d2e7]124// End: //
Note: See TracBrowser for help on using the repository browser.