source: src/libcfa/concurrency/thread.c @ cb0e6de

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 cb0e6de was cb0e6de, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Threads now use monitor semantics to wait until completion

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[6a3d2e7]1//                              -*- Mode: CFA -*-
[78b3f52]2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
[75a17f1]8// thread.c --
[78b3f52]9//
10// Author           : Thierry Delisle
[f07e037]11// Created On       : Tue Jan 17 12:27:26 2017
[78b3f52]12// Last Modified By : Thierry Delisle
[6a3d2e7]13// Last Modified On : --
[78b3f52]14// Update Count     : 0
15//
16
[75a17f1]17#include "thread"
[78b3f52]18
[75f3522]19#include "kernel_private.h"
[8118303]20#include "libhdr.h"
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
[89a3df5]30extern thread_local processor * this_processor;
[8118303]31
32//-----------------------------------------------------------------------------
33// Forward declarations
[0c92c9f]34forall( dtype T | is_thread(T) )
[bd98b58]35void start( T* this );
[8118303]36
[0c92c9f]37forall( dtype T | is_thread(T) )
[bd98b58]38void stop( T* this );
[8118303]39
40//-----------------------------------------------------------------------------
41// Thread ctors and dtors
42
[348006f]43void ?{}(thread_desc* this) {
[17af7d1]44        (&this->cor){};
45        this->cor.name = "Anonymous Coroutine";
[cb0e6de]46        this->mon.owner = this;
47        this->mon.recursion = 1;
[db6f06a]48        (&this->terminated){};
[bd98b58]49        this->next = NULL;
[8118303]50}
51
[348006f]52void ^?{}(thread_desc* this) {
[17af7d1]53        ^(&this->cor){};
[8118303]54}
55
[8def349]56forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
[e15df4c]57void ?{}( scoped(T)* this ) {
[8118303]58        (&this->handle){};
[bd98b58]59        start(&this->handle);
[8118303]60}
61
[8def349]62forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
[e15df4c]63void ?{}( scoped(T)* this, P params ) {
[8118303]64        (&this->handle){ params };
[bd98b58]65        start(&this->handle);
[8118303]66}
67
[9f1695b]68forall( dtype T | sized(T) | is_thread(T) )
[e15df4c]69void ^?{}( scoped(T)* this ) {
[bd98b58]70        stop(&this->handle);
[8118303]71        ^(&this->handle){};
72}
73
74//-----------------------------------------------------------------------------
75// Starting and stopping threads
[0c92c9f]76forall( dtype T | is_thread(T) )
[bd98b58]77void start( T* this ) {
[c3acb841]78        coroutine_desc* thrd_c = get_coroutine(this);
[348006f]79        thread_desc*  thrd_h = get_thread   (this);
[8118303]80        thrd_c->last = this_coroutine();
[89a3df5]81        this_processor->current_coroutine = thrd_c;
[8118303]82
[0c92c9f]83        LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
[8118303]84
85        create_stack(&thrd_c->stack, thrd_c->stack.size);
[bd98b58]86        CtxStart(this, CtxInvokeThread);
[8118303]87        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
88
[75f3522]89        ScheduleThread(thrd_h);
[8118303]90}
91
[0c92c9f]92forall( dtype T | is_thread(T) )
[bd98b58]93void stop( T* this ) {
[cb0e6de]94        // wait( & get_thread(this)->terminated );     
[8118303]95}
[9129a84]96
[bd98b58]97void yield( void ) {
[89a3df5]98        ScheduleInternal( this_processor->current_thread );
[bd98b58]99}
100
[c3acb841]101void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
[0c92c9f]102        // set state of current coroutine to inactive
103        src->state = 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
[89a3df5]111        this_processor->current_coroutine = dst;
[0c92c9f]112        CtxSwitch( src->stack.context, dst->stack.context );
[89a3df5]113        this_processor->current_coroutine = src;
[0c92c9f]114
115        // set state of new coroutine to active
[75f3522]116        dst->state = Inactive;
[0c92c9f]117        src->state = Active;
118}
119
[348006f]120// C Helper to signal the termination of a thread_desc
[0c92c9f]121// Used in invoke.c
122extern "C" {
[348006f]123        void __thread_signal_termination( thread_desc * this ) {
[17af7d1]124                this->cor.state = Halted;
[db6f06a]125                LIB_DEBUG_PRINTF("Thread end : %p\n", this);
126                signal( &this->terminated );   
[0c92c9f]127        }
128}
129
[78b3f52]130// Local Variables: //
131// mode: c //
132// tab-width: 4 //
[6a3d2e7]133// End: //
Note: See TracBrowser for help on using the repository browser.