source: src/libcfa/concurrency/coroutine @ 986dd36

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 986dd36 was b462670, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Coroutines should now properly use the first resumner as their starter

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[6a3d2e7]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//
[75a17f1]7// coroutine --
[6a3d2e7]8//
9// Author           : Thierry Delisle
10// Created On       : Mon Nov 28 12:27:26 2016
[91c389a]11// Last Modified By : Peter A. Buhr
[e5fccf4]12// Last Modified On : Wed Aug 30 07:58:29 2017
13// Update Count     : 3
[6a3d2e7]14//
15
[6b0b624]16#pragma once
[6a3d2e7]17
[91c389a]18#include <assert.h>
[6a3d2e7]19#include "invoke.h"
20
21//-----------------------------------------------------------------------------
22// Coroutine trait
23// Anything that implements this trait can be resumed.
24// Anything that is resumed is a coroutine.
25trait is_coroutine(dtype T) {
[83a071f9]26      void main(T & this);
27      coroutine_desc * get_coroutine(T & this);
[6a3d2e7]28};
29
[83a071f9]30#define DECL_COROUTINE(X) static inline coroutine_desc* get_coroutine(X& this) { return &this.__cor; } void main(X& this)
[c84e80a]31
[6a3d2e7]32//-----------------------------------------------------------------------------
33// Ctors and dtors
[242a902]34void ?{}(coStack_t & this);
35void ?{}(coroutine_desc & this);
36void ?{}(coroutine_desc & this, const char * name);
37void ^?{}(coStack_t & this);
38void ^?{}(coroutine_desc & this);
[6a3d2e7]39
40//-----------------------------------------------------------------------------
41// Public coroutine API
42static inline void suspend();
43
44forall(dtype T | is_coroutine(T))
[83a071f9]45static inline void resume(T & cor);
[6a3d2e7]46
47forall(dtype T | is_coroutine(T))
[83a071f9]48void prime(T & cor);
[6a3d2e7]49
50//-----------------------------------------------------------------------------
51// PRIVATE exposed because of inline
52
53// Start coroutine routines
54extern "C" {
55      forall(dtype T | is_coroutine(T))
[8118303]56      void CtxInvokeCoroutine(T * this);
[6a3d2e7]57
58      forall(dtype T | is_coroutine(T))
[8118303]59      void CtxStart(T * this, void ( *invoke)(T *));
[6a3d2e7]60}
61
62// Get current coroutine
[9cc0472]63extern thread_local coroutine_desc * volatile this_coroutine;
[6a3d2e7]64
65// Private wrappers for context switch and stack creation
[c3acb841]66extern void CoroutineCtxSwitch(coroutine_desc * src, coroutine_desc * dst);
[8118303]67extern void create_stack( coStack_t * this, unsigned int storageSize );
[6a3d2e7]68
69// Suspend implementation inlined for performance
70static inline void suspend() {
[1c273d0]71        coroutine_desc * src = this_coroutine;          // optimization
[6a3d2e7]72
73        assertf( src->last != 0,
[8def349]74                "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
[6a3d2e7]75                "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
76                src->name, src );
[ee897e4b]77        assertf( src->last->state != Halted,
[8def349]78                "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
[6a3d2e7]79                "Possible cause is terminated coroutine's main routine has already returned.",
80                src->name, src, src->last->name, src->last );
81
[0c92c9f]82        CoroutineCtxSwitch( src, src->last );
[6a3d2e7]83}
84
85// Resume implementation inlined for performance
86forall(dtype T | is_coroutine(T))
[83a071f9]87static inline void resume(T & cor) {
[1c273d0]88        coroutine_desc * src = this_coroutine;          // optimization
[c3acb841]89        coroutine_desc * dst = get_coroutine(cor);
[6a3d2e7]90
[4aa2fb2]91        if( unlikely(!dst->stack.base) ) {
[6a3d2e7]92                create_stack(&dst->stack, dst->stack.size);
[83a071f9]93                CtxStart(&cor, CtxInvokeCoroutine);
[6a3d2e7]94        }
95
[4aa2fb2]96        // not resuming self ?
[6a3d2e7]97        if ( src != dst ) {
[ee897e4b]98                assertf( dst->state != Halted ,
[6a3d2e7]99                        "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
100                        "Possible cause is terminated coroutine's main routine has already returned.",
101                        src->name, src, dst->name, dst );
102
[4aa2fb2]103                // set last resumer
[6a3d2e7]104                dst->last = src;
[b462670]105                dst->starter = dst->starter ? dst->starter : src;
[6a3d2e7]106        } // if
107
[4aa2fb2]108        // always done for performance testing
[0c92c9f]109        CoroutineCtxSwitch( src, dst );
[6a3d2e7]110}
111
[c3acb841]112static inline void resume(coroutine_desc * dst) {
[1c273d0]113        coroutine_desc * src = this_coroutine;          // optimization
[77e6fcb]114
[4aa2fb2]115        // not resuming self ?
[77e6fcb]116        if ( src != dst ) {
[ee897e4b]117                assertf( dst->state != Halted ,
[77e6fcb]118                        "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
119                        "Possible cause is terminated coroutine's main routine has already returned.",
120                        src->name, src, dst->name, dst );
121
[4aa2fb2]122                // set last resumer
[77e6fcb]123                dst->last = src;
124        } // if
125
[4aa2fb2]126        // always done for performance testing
[77e6fcb]127        CoroutineCtxSwitch( src, dst );
128}
129
[6a3d2e7]130// Local Variables: //
131// mode: c //
132// tab-width: 4 //
133// End: //
Note: See TracBrowser for help on using the repository browser.