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
Line 
1//                              -*- Mode: CFA -*-
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//
8// thread.c --
9//
10// Author           : Thierry Delisle
11// Created On       : Tue Jan 17 12:27:26 2017
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count     : 0
15//
16
17#include "thread"
18
19#include "kernel_private.h"
20#include "libhdr.h"
21
22#define __CFA_INVOKE_PRIVATE__
23#include "invoke.h"
24
25extern "C" {
26        #include <fenv.h>
27        #include <stddef.h>
28}
29
30extern thread_local processor * this_processor;
31
32//-----------------------------------------------------------------------------
33// Forward declarations
34forall( dtype T | is_thread(T) )
35void start( T* this );
36
37forall( dtype T | is_thread(T) )
38void stop( T* this );
39
40//-----------------------------------------------------------------------------
41// Thread ctors and dtors
42
43void ?{}(thread_desc* this) {
44        (&this->cor){};
45        this->cor.name = "Anonymous Coroutine";
46        this->mon.owner = this;
47        this->mon.recursion = 1;
48        (&this->terminated){};
49        this->next = NULL;
50}
51
52void ^?{}(thread_desc* this) {
53        ^(&this->cor){};
54}
55
56forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
57void ?{}( scoped(T)* this ) {
58        (&this->handle){};
59        start(&this->handle);
60}
61
62forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
63void ?{}( scoped(T)* this, P params ) {
64        (&this->handle){ params };
65        start(&this->handle);
66}
67
68forall( dtype T | sized(T) | is_thread(T) )
69void ^?{}( scoped(T)* this ) {
70        stop(&this->handle);
71        ^(&this->handle){};
72}
73
74//-----------------------------------------------------------------------------
75// Starting and stopping threads
76forall( dtype T | is_thread(T) )
77void start( T* this ) {
78        coroutine_desc* thrd_c = get_coroutine(this);
79        thread_desc*  thrd_h = get_thread   (this);
80        thrd_c->last = this_coroutine();
81        this_processor->current_coroutine = thrd_c;
82
83        LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
84
85        create_stack(&thrd_c->stack, thrd_c->stack.size);
86        CtxStart(this, CtxInvokeThread);
87        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
88
89        ScheduleThread(thrd_h);
90}
91
92forall( dtype T | is_thread(T) )
93void stop( T* this ) {
94        // wait( & get_thread(this)->terminated );     
95}
96
97void yield( void ) {
98        ScheduleInternal( this_processor->current_thread );
99}
100
101void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
102        // set state of current coroutine to inactive
103        src->state = Inactive;
104        dst->state = Active;
105
106        //update the last resumer
107        dst->last = src;
108
109        // set new coroutine that the processor is executing
110        // and context switch to it
111        this_processor->current_coroutine = dst;
112        CtxSwitch( src->stack.context, dst->stack.context );
113        this_processor->current_coroutine = src;
114
115        // set state of new coroutine to active
116        dst->state = Inactive;
117        src->state = Active;
118}
119
120// C Helper to signal the termination of a thread_desc
121// Used in invoke.c
122extern "C" {
123        void __thread_signal_termination( thread_desc * this ) {
124                this->cor.state = Halted;
125                LIB_DEBUG_PRINTF("Thread end : %p\n", this);
126                signal( &this->terminated );   
127        }
128}
129
130// Local Variables: //
131// mode: c //
132// tab-width: 4 //
133// End: //
Note: See TracBrowser for help on using the repository browser.