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

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

Added doubly-linked list of existing threads

  • Property mode set to 100644
File size: 3.1 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) {
34        (this.self_cor){};
35        this.self_cor.name = "Anonymous Thread";
36        this.self_mon.owner = &this;
37        this.self_mon.recursion = 1;
38        this.self_mon_p = &this.self_mon;
39        this.next = NULL;
40        __cfaabi_dbg_debug_do(
41                this.dbg_next = NULL;
42                this.dbg_prev = NULL;
43                __cfaabi_dbg_thread_register(&this);
44        )
45
46        (this.monitors){ &this.self_mon_p, 1, (fptr_t)0 };
47}
48
49void ^?{}(thread_desc& this) {
50        __cfaabi_dbg_debug_do(
51                __cfaabi_dbg_thread_unregister(&this);
52        )
53        ^(this.self_cor){};
54}
55
56forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } )
57void ?{}( scoped(T)& this ) {
58        (this.handle){};
59        __thrd_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        __thrd_start(this.handle);
66}
67
68forall( dtype T | sized(T) | is_thread(T) )
69void ^?{}( scoped(T)& this ) {
70        ^(this.handle){};
71}
72
73//-----------------------------------------------------------------------------
74// Starting and stopping threads
75forall( dtype T | is_thread(T) )
76void __thrd_start( T& this ) {
77        coroutine_desc* thrd_c = get_coroutine(this);
78        thread_desc*  thrd_h = get_thread   (this);
79        thrd_c->last = this_coroutine;
80
81        // __cfaabi_dbg_print_safe("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
82
83        disable_interrupts();
84        create_stack(&thrd_c->stack, thrd_c->stack.size);
85        this_coroutine = thrd_c;
86        CtxStart(&this, CtxInvokeThread);
87        assert( thrd_c->last->stack.context );
88        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
89
90        ScheduleThread(thrd_h);
91        enable_interrupts( __cfaabi_dbg_ctx );
92}
93
94void yield( void ) {
95        BlockInternal( this_thread );
96}
97
98void yield( unsigned times ) {
99        for( unsigned i = 0; i < times; i++ ) {
100                yield();
101        }
102}
103
104void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
105        // set state of current coroutine to inactive
106        src->state = src->state == Halted ? Halted : Inactive;
107        dst->state = Active;
108
109        //update the last resumer
110        dst->last = src;
111
112        // set new coroutine that the processor is executing
113        // and context switch to it
114        this_coroutine = dst;
115        assert( src->stack.context );
116        CtxSwitch( src->stack.context, dst->stack.context );
117        this_coroutine = src;
118
119        // set state of new coroutine to active
120        dst->state = dst->state == Halted ? Halted : Inactive;
121        src->state = Active;
122}
123
124// Local Variables: //
125// mode: c //
126// tab-width: 4 //
127// End: //
Note: See TracBrowser for help on using the repository browser.