source: libcfa/src/concurrency/thread.cfa @ abcae55

Last change on this file since abcae55 was abcae55, checked in by Thierry Delisle <tdelisle@…>, 2 months ago

Removed last_cpu (and small spacing changes)

  • Property mode set to 100644
File size: 4.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 : Wed Dec  4 09:17:49 2019
13// Update Count     : 9
14//
15
16#define __cforall_thread__
17#define _GNU_SOURCE
18
19#include "thread.hfa"
20
21#include "kernel_private.hfa"
22#include "exception.hfa"
23
24#define __CFA_INVOKE_PRIVATE__
25#include "invoke.h"
26
27//-----------------------------------------------------------------------------
28// Thread ctors and dtors
29void ?{}(thread$ & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) {
30        context{ 0p, 0p };
31        self_cor{ name, storage, storageSize };
32        ticket = TICKET_RUNNING;
33        state = Start;
34        preempted = __NO_PREEMPTION;
35        corctx_flag = false;
36        curr_cor = &self_cor;
37        self_mon.owner = &this;
38        self_mon.recursion = 1;
39        self_mon_p = &self_mon;
40        curr_cluster = &cl;
41        link.next = 0p;
42        link.ts   = -1llu;
43        preferred = -1u;
44        last_proc = 0p;
45        #if defined( __CFA_WITH_VERIFY__ )
46                canary = 0x0D15EA5E0D15EA5Ep;
47        #endif
48
49        seqable.next = 0p;
50        seqable.back = 0p;
51
52        node.next = 0p;
53        node.prev = 0p;
54        doregister(curr_cluster, this);
55
56        monitors{ &self_mon_p, 1, (fptr_t)0 };
57}
58
59void ^?{}(thread$& this) with( this ) {
60        #if defined( __CFA_WITH_VERIFY__ )
61                canary = 0xDEADDEADDEADDEADp;
62        #endif
63        unregister(curr_cluster, this);
64        ^self_cor{};
65}
66
67forall(T &)
68void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src) {
69        dst->virtual_table = src->virtual_table;
70        dst->the_thread = src->the_thread;
71        dst->the_exception = src->the_exception;
72}
73
74forall(T &)
75const char * msg(ThreadCancelled(T) *) {
76        return "ThreadCancelled(...)";
77}
78
79forall(T &)
80static void default_thread_cancel_handler(ThreadCancelled(T) & ) {
81        // Improve this error message, can I do formatting?
82        abort( "Unhandled thread cancellation.\n" );
83}
84
85forall(T & | is_thread(T) | IS_EXCEPTION(ThreadCancelled, (T))
86    | { EHM_DEFAULT_VTABLE(ThreadCancelled, (T)); })
87void ?{}( thread_dtor_guard_t & this,
88                T & thrd, void(*cancelHandler)(ThreadCancelled(T) &)) {
89        monitor$ * m = get_monitor(thrd);
90        thread$ * desc = get_thread(thrd);
91
92        // Setup the monitor guard
93        void (*dtor)(T& mutex this) = ^?{};
94        bool join = cancelHandler != (void(*)(ThreadCancelled(T)&))0;
95        (this.mg){&m, (void(*)())dtor, join};
96
97
98        /* paranoid */ verifyf( Halted == desc->state || Cancelled == desc->state, "Expected thread to be Halted or Cancelled, was %d\n", (int)desc->state );
99
100        // After the guard set-up and any wait, check for cancellation.
101        struct _Unwind_Exception * cancellation = desc->self_cor.cancellation;
102        if ( likely( 0p == cancellation ) ) {
103                return;
104        } else if ( Cancelled == desc->state ) {
105                return;
106        }
107        desc->state = Cancelled;
108        void(*defaultResumptionHandler)(ThreadCancelled(T) &) =
109                join ? cancelHandler : default_thread_cancel_handler;
110
111        // TODO: Remove explitate vtable set once trac#186 is fixed.
112        ThreadCancelled(T) except;
113        except.virtual_table = &_default_vtable;
114        except.the_thread = &thrd;
115        except.the_exception = __cfaehm_cancellation_exception( cancellation );
116        // Why is this cast required?
117        throwResume (ThreadCancelled(T) &)except;
118
119        except.the_exception->virtual_table->free( except.the_exception );
120        free( cancellation );
121        desc->self_cor.cancellation = 0p;
122}
123
124void ^?{}( thread_dtor_guard_t & this ) {
125        ^(this.mg){};
126}
127
128//-----------------------------------------------------------------------------
129// Starting and stopping threads
130forall( T & | is_thread(T) )
131void __thrd_start( T & this, void (*main_p)(T &) ) {
132        thread$ * this_thrd = get_thread(this);
133
134        disable_interrupts();
135        __cfactx_start(main_p, get_coroutine(this), this, __cfactx_invoke_thread);
136
137        this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP];
138        /* paranoid */ verify( this_thrd->context.SP );
139
140        schedule_thread$( this_thrd );
141        enable_interrupts();
142}
143
144//-----------------------------------------------------------------------------
145// Support for threads that don't ues the thread keyword
146forall( T & | sized(T) | is_thread(T) | { void ?{}(T&); } )
147void ?{}( scoped(T)& this ) with( this ) {
148        handle{};
149        __thrd_start(handle, main);
150}
151
152forall( T &, P... | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
153void ?{}( scoped(T)& this, P params ) with( this ) {
154        handle{ params };
155        __thrd_start(handle, main);
156}
157
158forall( T & | sized(T) | is_thread(T) )
159void ^?{}( scoped(T)& this ) with( this ) {
160        ^handle{};
161}
162
163//-----------------------------------------------------------------------------
164forall(T & | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled, (T))
165    | { EHM_DEFAULT_VTABLE(ThreadCancelled, (T)); })
166T & join( T & this ) {
167        thread_dtor_guard_t guard = { this, defaultResumptionHandler };
168        return this;
169}
170
171uint64_t thread_rand() {
172        disable_interrupts();
173        uint64_t ret = __tls_rand();
174        enable_interrupts();
175        return ret;
176}
177
178// Local Variables: //
179// mode: c //
180// tab-width: 4 //
181// End: //
Note: See TracBrowser for help on using the repository browser.