source: src/libcfa/concurrency/threads.c @ 75f3522

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

Moved several declarations to a new private header and made some clean-up

  • Property mode set to 100644
File size: 3.1 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// threads.c --
9//
10// Author           : Thierry Delisle
11// Created On       : Tue Jan 17 12:27:26 2016
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count     : 0
15//
16
17#include "threads"
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 processor * get_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* this) {
44        (&this->c){};
45        this->c.name = "Anonymous Coroutine";
46        (&this->lock){};
47        this->next = NULL;
48}
49
50void ^?{}(thread* this) {
51        ^(&this->c){};
52}
53
54forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
55void ?{}( scoped(T)* this ) {
56        (&this->handle){};
57        start(&this->handle);
58}
59
60forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
61void ?{}( scoped(T)* this, P params ) {
62        (&this->handle){ params };
63        start(&this->handle);
64}
65
66forall( dtype T | sized(T) | is_thread(T) | { void ^?{}(T*); } )
67void ^?{}( scoped(T)* this ) {
68        stop(&this->handle);
69        ^(&this->handle){};
70}
71
72//-----------------------------------------------------------------------------
73// Starting and stopping threads
74forall( dtype T | is_thread(T) )
75void start( T* this ) {
76        coroutine* thrd_c = get_coroutine(this);
77        thread*  thrd_h = get_thread   (this);
78        thrd_c->last = this_coroutine();
79        get_this_processor()->current_coroutine = thrd_c;
80
81        LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
82
83        create_stack(&thrd_c->stack, thrd_c->stack.size);
84        CtxStart(this, CtxInvokeThread);
85        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
86
87        ScheduleThread(thrd_h);
88}
89
90forall( dtype T | is_thread(T) )
91void stop( T* this ) {
92        thread*  thrd = get_thread(this);
93        if( thrd->c.notHalted ) {
94                lock( &thrd->lock );
95        }
96}
97
98void yield( void ) {
99        ScheduleInternal();
100}
101
102void ThreadCtxSwitch(coroutine* src, coroutine* dst) {
103        // set state of current coroutine to inactive
104        src->state = Inactive;
105        dst->state = Active;
106
107        //update the last resumer
108        dst->last = src;
109
110        // set new coroutine that the processor is executing
111        // and context switch to it
112        get_this_processor()->current_coroutine = dst; 
113        CtxSwitch( src->stack.context, dst->stack.context );
114        get_this_processor()->current_coroutine = src; 
115
116        // set state of new coroutine to active
117        dst->state = Inactive;
118        src->state = Active;
119}
120
121// C Helper to signal the termination of a thread
122// Used in invoke.c
123extern "C" {
124        void __thread_signal_termination( thread * this ) {
125                this->c.state = Halt;
126                this->c.notHalted = false;
127                unlock( &this->lock );
128        }
129}
130
131// Local Variables: //
132// mode: c //
133// tab-width: 4 //
134// End: //
Note: See TracBrowser for help on using the repository browser.