source: src/libcfa/concurrency/thread.c@ 59a0bde

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

Refactoring monitor code in prevision for proper waitfor support

  • added monitor group struct
  • else and timeout now return negative results
  • Property mode set to 100644
File size: 2.9 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// thread.c --
8//
9// Author : Thierry Delisle
10// Created On : Tue Jan 17 12:27:26 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jul 21 22:34:46 2017
13// Update Count : 1
14//
15
16#include "thread"
17
18#include "kernel_private.h"
19#include "libhdr.h"
20
21#define __CFA_INVOKE_PRIVATE__
22#include "invoke.h"
23
24extern "C" {
25 #include <fenv.h>
26 #include <stddef.h>
27}
28
29extern volatile thread_local processor * this_processor;
30
31//-----------------------------------------------------------------------------
32// Thread ctors and dtors
33
34void ?{}(thread_desc& this) {
35 (this.self_cor){};
36 this.self_cor.name = "Anonymous Coroutine";
37 this.self_mon.owner = &this;
38 this.self_mon.recursion = 1;
39 this.self_mon_p = &this.self_mon;
40 this.next = NULL;
41
42 (this.monitors){ &this.self_mon_p, 1, (fptr_t)0 };
43}
44
45void ^?{}(thread_desc& this) {
46 ^(this.self_cor){};
47}
48
49forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } )
50void ?{}( scoped(T)& this ) {
51 (this.handle){};
52 __thrd_start(this.handle);
53}
54
55forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
56void ?{}( scoped(T)& this, P params ) {
57 (this.handle){ params };
58 __thrd_start(this.handle);
59}
60
61forall( dtype T | sized(T) | is_thread(T) )
62void ^?{}( scoped(T)& this ) {
63 ^(this.handle){};
64}
65
66//-----------------------------------------------------------------------------
67// Starting and stopping threads
68forall( dtype T | is_thread(T) )
69void __thrd_start( T& this ) {
70 coroutine_desc* thrd_c = get_coroutine(this);
71 thread_desc* thrd_h = get_thread (this);
72 thrd_c->last = this_coroutine;
73
74 // LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
75
76 disable_interrupts();
77 create_stack(&thrd_c->stack, thrd_c->stack.size);
78 this_coroutine = thrd_c;
79 CtxStart(&this, CtxInvokeThread);
80 assert( thrd_c->last->stack.context );
81 CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
82
83 ScheduleThread(thrd_h);
84 enable_interrupts( DEBUG_CTX );
85}
86
87void yield( void ) {
88 BlockInternal( this_thread );
89}
90
91void yield( unsigned times ) {
92 for( unsigned i = 0; i < times; i++ ) {
93 yield();
94 }
95}
96
97void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
98 // set state of current coroutine to inactive
99 src->state = src->state == Halted ? Halted : Inactive;
100 dst->state = Active;
101
102 //update the last resumer
103 dst->last = src;
104
105 // set new coroutine that the processor is executing
106 // and context switch to it
107 this_coroutine = dst;
108 assert( src->stack.context );
109 CtxSwitch( src->stack.context, dst->stack.context );
110 this_coroutine = src;
111
112 // set state of new coroutine to active
113 dst->state = dst->state == Halted ? Halted : Inactive;
114 src->state = Active;
115}
116
117// Local Variables: //
118// mode: c //
119// tab-width: 4 //
120// End: //
Note: See TracBrowser for help on using the repository browser.