source: src/libcfa/concurrency/threads.c@ a073d46

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since a073d46 was 8fcbb4c, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

removed pthread_spinlock_t and fixed race condition in yield

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[6a3d2e7]1// -*- Mode: CFA -*-
[78b3f52]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//
[8118303]8// threads.c --
[78b3f52]9//
10// Author : Thierry Delisle
[6a3d2e7]11// Created On : Tue Jan 17 12:27:26 2016
[78b3f52]12// Last Modified By : Thierry Delisle
[6a3d2e7]13// Last Modified On : --
[78b3f52]14// Update Count : 0
15//
16
[8118303]17#include "threads"
[78b3f52]18
[8118303]19#include "kernel"
20#include "libhdr.h"
21
22#define __CFA_INVOKE_PRIVATE__
23#include "invoke.h"
24
[bd98b58]25extern "C" {
[8fcbb4c]26 #include <fenv.h>
[bd98b58]27 #include <stddef.h>
28}
29
[8def349]30extern processor * get_this_processor();
[8118303]31
32//-----------------------------------------------------------------------------
33// Forward declarations
[0c92c9f]34forall( dtype T | is_thread(T) )
[bd98b58]35void start( T* this );
[8118303]36
[0c92c9f]37forall( dtype T | is_thread(T) )
[bd98b58]38void stop( T* this );
[8118303]39
40//-----------------------------------------------------------------------------
41// Thread ctors and dtors
42
[e15df4c]43void ?{}(thread* this) {
[8118303]44 (&this->c){};
[8def349]45 this->c.name = "Anonymous Coroutine";
[bd98b58]46 (&this->lock){};
47 this->next = NULL;
[8118303]48}
49
[e15df4c]50void ^?{}(thread* this) {
[8118303]51 ^(&this->c){};
52}
53
[8def349]54forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
[e15df4c]55void ?{}( scoped(T)* this ) {
[8118303]56 (&this->handle){};
[bd98b58]57 start(&this->handle);
[8118303]58}
59
[8def349]60forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
[e15df4c]61void ?{}( scoped(T)* this, P params ) {
[8118303]62 (&this->handle){ params };
[bd98b58]63 start(&this->handle);
[8118303]64}
65
[8def349]66forall( dtype T | sized(T) | is_thread(T) | { void ^?{}(T*); } )
[e15df4c]67void ^?{}( scoped(T)* this ) {
[bd98b58]68 stop(&this->handle);
[8118303]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
[e15df4c]79extern void thread_schedule( thread * );
[bd98b58]80
[0c92c9f]81forall( dtype T | is_thread(T) )
[bd98b58]82void start( T* this ) {
83 coroutine* thrd_c = get_coroutine(this);
[e15df4c]84 thread* thrd_h = get_thread (this);
[8118303]85 thrd_c->last = this_coroutine();
[8def349]86 get_this_processor()->current_coroutine = thrd_c;
[8118303]87
[0c92c9f]88 LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
[8118303]89
90 create_stack(&thrd_c->stack, thrd_c->stack.size);
[bd98b58]91 CtxStart(this, CtxInvokeThread);
[8118303]92 CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
93
[8fcbb4c]94 fenv_t envp;
95 fegetenv( &envp );
96 LIB_DEBUG_PRINTF("Thread : mxcsr %x\n", envp.__mxcsr);
[0c92c9f]97 LIB_DEBUG_PRINTF("Thread started : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
98
[bd98b58]99 thread_schedule(thrd_h);
[8118303]100}
101
[0c92c9f]102forall( dtype T | is_thread(T) )
[bd98b58]103void stop( T* this ) {
[e15df4c]104 thread* thrd = get_thread(this);
[bd98b58]105 if( thrd->c.notHalted ) {
106 lock( &thrd->lock );
[8f49a54]107 }
[8118303]108}
[9129a84]109
[bd98b58]110void yield( void ) {
[8fcbb4c]111 get_this_processor()->thread_action = Reschedule;
[bd98b58]112 suspend();
113}
114
[0c92c9f]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
[78b3f52]142// Local Variables: //
143// mode: c //
144// tab-width: 4 //
[6a3d2e7]145// End: //
Note: See TracBrowser for help on using the repository browser.