Ignore:
Timestamp:
Nov 4, 2020, 2:56:30 PM (3 years ago)
Author:
Colby Alexander Parsons <caparsons@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
eeb5023
Parents:
4b30e8cc (diff), a3f5208a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/thread.cfa

    r4b30e8cc rc28ea4e  
    1919
    2020#include "kernel_private.hfa"
     21#include "exception.hfa"
    2122
    2223#define __CFA_INVOKE_PRIVATE__
     
    2829        context{ 0p, 0p };
    2930        self_cor{ name, storage, storageSize };
    30         ticket = 1;
     31        ticket = TICKET_RUNNING;
    3132        state = Start;
    3233        preempted = __NO_PREEMPTION;
     
    5859}
    5960
     61FORALL_DATA_INSTANCE(ThreadCancelled, (dtype thread_t), (thread_t))
     62
     63forall(dtype T)
     64void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src) {
     65        dst->virtual_table = src->virtual_table;
     66        dst->the_thread = src->the_thread;
     67        dst->the_exception = src->the_exception;
     68}
     69
     70forall(dtype T)
     71const char * msg(ThreadCancelled(T) *) {
     72        return "ThreadCancelled";
     73}
     74
     75forall(dtype T)
     76static void default_thread_cancel_handler(ThreadCancelled(T) & ) {
     77        abort( "Unhandled thread cancellation.\n" );
     78}
     79
     80forall(dtype T | is_thread(T) | IS_EXCEPTION(ThreadCancelled, (T)))
     81void ?{}( thread_dtor_guard_t & this,
     82                T & thrd, void(*defaultResumptionHandler)(ThreadCancelled(T) &)) {
     83        $monitor * m = get_monitor(thrd);
     84        void (*dtor)(T& mutex this) = ^?{};
     85        bool join = defaultResumptionHandler != (void(*)(ThreadCancelled(T)&))0;
     86        (this.mg){&m, (void(*)())dtor, join};
     87
     88        // After the guard set-up and any wait, check for cancellation.
     89        $thread * desc = get_thread(thrd);
     90        struct _Unwind_Exception * cancellation = desc->self_cor.cancellation;
     91        if ( likely( 0p == cancellation ) ) {
     92                return;
     93        } else if ( Cancelled == desc->state ) {
     94                return;
     95        }
     96        desc->state = Cancelled;
     97        if (!join) {
     98                defaultResumptionHandler = default_thread_cancel_handler;
     99        }
     100
     101        ThreadCancelled(T) except;
     102        // TODO: Remove explitate vtable set once trac#186 is fixed.
     103        except.virtual_table = &get_exception_vtable(&except);
     104        except.the_thread = &thrd;
     105        except.the_exception = __cfaehm_cancellation_exception( cancellation );
     106        throwResume except;
     107
     108        except.the_exception->virtual_table->free( except.the_exception );
     109        free( cancellation );
     110        desc->self_cor.cancellation = 0p;
     111}
     112
     113void ^?{}( thread_dtor_guard_t & this ) {
     114        ^(this.mg){};
     115}
     116
    60117//-----------------------------------------------------------------------------
    61118// Starting and stopping threads
     
    70127        verify( this_thrd->context.SP );
    71128
    72         __schedule_thread( (__processor_id_t *)kernelTLS.this_processor, this_thrd);
     129        __schedule_thread( this_thrd );
    73130        enable_interrupts( __cfaabi_dbg_ctx );
    74131}
     
    93150}
    94151
     152//-----------------------------------------------------------------------------
     153forall(dtype T | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled, (T)))
     154T & join( T & this ) {
     155        thread_dtor_guard_t guard = { this, defaultResumptionHandler };
     156        return this;
     157}
     158
    95159// Local Variables: //
    96160// mode: c //
Note: See TracChangeset for help on using the changeset viewer.