source: src/libcfa/concurrency/thread.c @ 0583064b

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 0583064b was 9c31349, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Removed unnecessary synchronisation routines in threads, now using monitor instead

  • Property mode set to 100644
File size: 2.8 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
37//-----------------------------------------------------------------------------
38// Thread ctors and dtors
39
40void ?{}(thread_desc* this) {
41        (&this->cor){};
42        this->cor.name = "Anonymous Coroutine";
43        this->mon.owner = this;
44        this->mon.recursion = 1;
45        this->next = NULL;
46}
47
48void ^?{}(thread_desc* this) {
49        ^(&this->cor){};
50}
51
52forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
53void ?{}( scoped(T)* this ) {
54        (&this->handle){};
55        start(&this->handle);
56}
57
58forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
59void ?{}( scoped(T)* this, P params ) {
60        (&this->handle){ params };
61        start(&this->handle);
62}
63
64forall( dtype T | sized(T) | is_thread(T) )
65void ^?{}( scoped(T)* this ) {
66        ^(&this->handle){};
67}
68
69//-----------------------------------------------------------------------------
70// Starting and stopping threads
71forall( dtype T | is_thread(T) )
72void start( T* this ) {
73        coroutine_desc* thrd_c = get_coroutine(this);
74        thread_desc*  thrd_h = get_thread   (this);
75        thrd_c->last = this_coroutine();
76        this_processor->current_coroutine = thrd_c;
77
78        LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
79
80        create_stack(&thrd_c->stack, thrd_c->stack.size);
81        CtxStart(this, CtxInvokeThread);
82        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
83
84        ScheduleThread(thrd_h);
85}
86
87void yield( void ) {
88        ScheduleInternal( this_processor->current_thread );
89}
90
91void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
92        // set state of current coroutine to inactive
93        src->state = Inactive;
94        dst->state = Active;
95
96        //update the last resumer
97        dst->last = src;
98
99        // set new coroutine that the processor is executing
100        // and context switch to it
101        this_processor->current_coroutine = dst;
102        CtxSwitch( src->stack.context, dst->stack.context );
103        this_processor->current_coroutine = src;
104
105        // set state of new coroutine to active
106        dst->state = Inactive;
107        src->state = Active;
108}
109
110// Local Variables: //
111// mode: c //
112// tab-width: 4 //
113// End: //
Note: See TracBrowser for help on using the repository browser.