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

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

Working implementation of internal scheduling, TODO some cleanup

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