source: src/libcfa/concurrency/coroutine @ 653f2c7

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 653f2c7 was 4aa2fb2, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Added verify macro for asserts only present in debug

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