Ignore:
Timestamp:
Jan 11, 2019, 10:54:41 AM (3 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer
Children:
456834b
Parents:
f23b685
Message:

Implemented stack unwinding on coroutine termination

File:
1 edited

Legend:

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

    rf23b685 r76e069f  
    2222#include <string.h>
    2323#include <unistd.h>
     24// use this define to make unwind.h play nice, definetely a hack
     25#define HIDE_EXPORTS
     26#include <unwind.h>
     27#undef HIDE_EXPORTS
    2428#include <sys/mman.h>
    2529}
     
    2933#define __CFA_INVOKE_PRIVATE__
    3034#include "invoke.h"
     35
     36extern "C" {
     37      void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage) __attribute__ ((__noreturn__));
     38      static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) __attribute__ ((__noreturn__));
     39      static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) {
     40            abort();
     41      }
     42}
    3143
    3244//-----------------------------------------------------------------------------
     
    6779      starter = NULL;
    6880      last = NULL;
    69 }
    70 
    71 void ^?{}(coroutine_desc& this) {}
     81      cancellation = NULL;
     82}
     83
     84void ^?{}(coroutine_desc& this) {
     85      if(this.state != Halted) {
     86            coroutine_desc * src = TL_GET( this_coroutine );
     87            coroutine_desc * dst = &this;
     88
     89            struct _Unwind_Exception storage;
     90            storage.exception_class = -1;
     91            storage.exception_cleanup = _CtxCoroutine_UnwindCleanup;
     92            this.cancellation = &storage;
     93            this.last = src;
     94
     95              // not resuming self ?
     96              if ( src == dst ) {
     97                      abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src );
     98            }
     99
     100              CoroutineCtxSwitch( src, dst );
     101      }
     102}
    72103
    73104// Part of the Public API
     
    105136      // Safety note : This could cause some false positives due to preemption
    106137      verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate );
     138
     139      if( unlikely(src->cancellation != NULL) ) {
     140            _CtxCoroutine_Unwind(src->cancellation);
     141      }
    107142} //ctxSwitchDirect
    108143
     
    162197      }
    163198
    164       void __leave_coroutine(void) {
     199      void __leave_coroutine() {
    165200            coroutine_desc * src = TL_GET( this_coroutine ); // optimization
     201            coroutine_desc * starter = src->cancellation != 0 ? src->last : src->starter;
    166202
    167203            src->state = Halted;
    168204
    169             assertf( src->starter != 0,
     205            assertf( starter != 0,
    170206                  "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n"
    171207                  "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
    172208                  src->name, src );
    173             assertf( src->starter->state != Halted,
     209            assertf( starter->state != Halted,
    174210                  "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n"
    175211                  "Possible cause is terminated coroutine's main routine has already returned.",
    176                   src->name, src, src->starter->name, src->starter );
    177 
    178             CoroutineCtxSwitch( src, src->starter );
     212                  src->name, src, starter->name, starter );
     213
     214            CoroutineCtxSwitch( src, starter );
    179215      }
    180216}
Note: See TracChangeset for help on using the changeset viewer.