source: src/libcfa/concurrency/thread.c @ 119bb6a

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

Thread context switch no longer break coroutines.
Added corresponding test

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