| 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 | // exception.cfa -- Exceptions in a concurrent environment. | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Andrew Beach | 
|---|
| 10 | // Created On       : Mon Aug 17 10:41:00 2020 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Wed Sep 25 17:25:37 2024 | 
|---|
| 13 | // Update Count     : 15 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #define __cforall_thread__ | 
|---|
| 17 |  | 
|---|
| 18 | #include "exception.hfa" | 
|---|
| 19 |  | 
|---|
| 20 | #include "coroutine.hfa" | 
|---|
| 21 |  | 
|---|
| 22 | extern struct thread$ * mainThread; | 
|---|
| 23 | extern "C" { | 
|---|
| 24 | extern void __cfactx_thrd_leave(); | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | // Common pattern for all the stop functions, wait until the end then act. | 
|---|
| 28 | #define STOP_AT_END_FUNCTION(NAME, ...) \ | 
|---|
| 29 | static _Unwind_Reason_Code NAME( \ | 
|---|
| 30 | int version, \ | 
|---|
| 31 | _Unwind_Action actions, \ | 
|---|
| 32 | _Unwind_Exception_Class exception_class, \ | 
|---|
| 33 | struct _Unwind_Exception * unwind_exception, \ | 
|---|
| 34 | struct _Unwind_Context * unwind_context, \ | 
|---|
| 35 | void * stop_param) { \ | 
|---|
| 36 | verify(actions & _UA_CLEANUP_PHASE); \ | 
|---|
| 37 | verify(actions & _UA_FORCE_UNWIND); \ | 
|---|
| 38 | verify(!(actions & _UA_SEARCH_PHASE)); \ | 
|---|
| 39 | verify(!(actions & _UA_HANDLER_FRAME)); \ | 
|---|
| 40 | if ( actions & _UA_END_OF_STACK ) { \ | 
|---|
| 41 | __VA_ARGS__ \ | 
|---|
| 42 | } else { \ | 
|---|
| 43 | return _URC_NO_REASON; \ | 
|---|
| 44 | } \ | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | STOP_AT_END_FUNCTION(main_cancelstop, | 
|---|
| 48 | abort( | 
|---|
| 49 | "Propagation failed to find a matching handler.\n" | 
|---|
| 50 | "Possible cause is a missing try block with appropriate catch clause for the specified or derived exception type.\n" | 
|---|
| 51 | "Last exception name or message: %%s.\n" | 
|---|
| 52 | //              NODE_TO_EXCEPT( UNWIND_TO_NODE( unwind_exception ) )-> | 
|---|
| 53 | //                      virtual_table->msg( NODE_TO_EXCEPT( UNWIND_TO_NODE( unwind_exception ) ) ) | 
|---|
| 54 | ); | 
|---|
| 55 | ) | 
|---|
| 56 |  | 
|---|
| 57 | STOP_AT_END_FUNCTION(thread_cancelstop, | 
|---|
| 58 | __cfactx_thrd_leave(); | 
|---|
| 59 | __cabi_abort( "Resumed cancelled thread" ); | 
|---|
| 60 | ) | 
|---|
| 61 |  | 
|---|
| 62 | STOP_AT_END_FUNCTION(coroutine_cancelstop, | 
|---|
| 63 | struct coroutine$ * src = (coroutine$ *)stop_param; | 
|---|
| 64 | struct coroutine$ * dst = src->last; | 
|---|
| 65 | dst->cancellation = 1p; | 
|---|
| 66 | $ctx_switch( src, dst ); | 
|---|
| 67 | abort( "coroutine_cancelstop" ); | 
|---|
| 68 | ) | 
|---|
| 69 |  | 
|---|
| 70 | extern "C" { | 
|---|
| 71 |  | 
|---|
| 72 | struct exception_context_t * this_exception_context(void) libcfa_public { | 
|---|
| 73 | return &__get_stack( active_coroutine() )->exception_context; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | _Unwind_Reason_Code __cfaehm_cancellation_unwind( struct _Unwind_Exception * unwind_exception ) libcfa_public { | 
|---|
| 77 | _Unwind_Stop_Fn stop_func; | 
|---|
| 78 | void * stop_param; | 
|---|
| 79 |  | 
|---|
| 80 | struct thread$ * this_thread = active_thread(); | 
|---|
| 81 | if ( &this_thread->self_cor != this_thread->curr_cor ) { | 
|---|
| 82 | struct coroutine$ * cor = this_thread->curr_cor; | 
|---|
| 83 | cor->cancellation = unwind_exception; | 
|---|
| 84 |  | 
|---|
| 85 | stop_func = coroutine_cancelstop; | 
|---|
| 86 | stop_param = cor; | 
|---|
| 87 | } else if ( mainThread == this_thread ) { | 
|---|
| 88 | stop_func = main_cancelstop; | 
|---|
| 89 | stop_param = (void *)0x22; | 
|---|
| 90 | } else { | 
|---|
| 91 | this_thread->self_cor.cancellation = unwind_exception; | 
|---|
| 92 |  | 
|---|
| 93 | stop_func = thread_cancelstop; | 
|---|
| 94 | stop_param = this_thread; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | return _Unwind_ForcedUnwind( unwind_exception, stop_func, stop_param ); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | } | 
|---|