source: src/libcfa/concurrency/threads.c @ 132fad4

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

removed pthread_spinlock_t and fixed race condition in yield

  • Property mode set to 100644
File size: 3.4 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"
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
74extern "C" {
75      forall(dtype T | is_thread(T))
76      void CtxInvokeThread(T * this);
77}
78
79extern void thread_schedule( thread * );
80
81forall( dtype T | is_thread(T) )
82void start( T* this ) {
83        coroutine* thrd_c = get_coroutine(this);
84        thread*  thrd_h = get_thread   (this);
85        thrd_c->last = this_coroutine();
86        get_this_processor()->current_coroutine = thrd_c;
87
88        LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
89
90        create_stack(&thrd_c->stack, thrd_c->stack.size);
91        CtxStart(this, CtxInvokeThread);
92        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
93
94        fenv_t envp;
95        fegetenv( &envp );
96        LIB_DEBUG_PRINTF("Thread : mxcsr %x\n", envp.__mxcsr);
97        LIB_DEBUG_PRINTF("Thread started : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
98
99        thread_schedule(thrd_h);
100}
101
102forall( dtype T | is_thread(T) )
103void stop( T* this ) {
104        thread*  thrd = get_thread(this);
105        if( thrd->c.notHalted ) {
106                lock( &thrd->lock );
107        }
108}
109
110void yield( void ) {
111        get_this_processor()->thread_action = Reschedule;
112        suspend();
113}
114
115void ThreadCtxSwitch(coroutine* src, coroutine* dst) {
116        dst->last = src;
117
118        // set state of current coroutine to inactive
119        src->state = Inactive;
120
121        // set new coroutine that task is executing
122        get_this_processor()->current_coroutine = dst; 
123
124        // context switch to specified coroutine
125        CtxSwitch( src->stack.context, dst->stack.context );
126        // when CtxSwitch returns we are back in the src coroutine
127
128        // set state of new coroutine to active
129        src->state = Active;
130}
131
132// C Helper to signal the termination of a thread
133// Used in invoke.c
134extern "C" {
135        void __thread_signal_termination( thread * this ) {
136                this->c.state = Halt;
137                this->c.notHalted = false;
138                unlock( &this->lock );
139        }
140}
141
142// Local Variables: //
143// mode: c //
144// tab-width: 4 //
145// End: //
Note: See TracBrowser for help on using the repository browser.