source: src/libcfa/concurrency/coroutine @ 75a17f1

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 75a17f1 was 75a17f1, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Fixed compilation after renaming

  • Property mode set to 100644
File size: 4.1 KB
Line 
1//                              - *- Mode: CFA - *-
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//
8// coroutine --
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
20#include "assert"
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) {
28      void main(T * this);
29      coroutine * get_coroutine(T * this);
30};
31
32#define DECL_COROUTINE(X) static inline coroutine* get_coroutine(X* this) { return &this->c; } void main(X* this)
33
34//-----------------------------------------------------------------------------
35// Ctors and dtors
36void ?{}(coStack_t * this);
37void ?{}(coroutine * this);
38void ?{}(coroutine * this, const char * name);
39void ^?{}(coStack_t * this);
40void ^?{}(coroutine * this);
41
42//-----------------------------------------------------------------------------
43// Public coroutine API
44static inline void suspend();
45
46forall(dtype T | is_coroutine(T))
47static inline void resume(T * cor);
48
49forall(dtype T | is_coroutine(T))
50void prime(T * cor);
51
52//-----------------------------------------------------------------------------
53// PRIVATE exposed because of inline
54
55// Start coroutine routines
56extern "C" {
57      forall(dtype T | is_coroutine(T))
58      void CtxInvokeCoroutine(T * this);
59
60      forall(dtype T | is_coroutine(T))
61      void CtxStart(T * this, void ( *invoke)(T *));
62}
63
64// Get current coroutine
65coroutine * this_coroutine(void);
66
67// Private wrappers for context switch and stack creation
68extern void CoroutineCtxSwitch(coroutine * src, coroutine * dst);
69extern void create_stack( coStack_t * this, unsigned int storageSize );
70
71// Suspend implementation inlined for performance
72static inline void suspend() {
73      coroutine * src = this_coroutine();               // optimization
74
75        assertf( src->last != 0,
76                "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
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 );
79        assertf( src->last->state != Halted,
80                "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
81                "Possible cause is terminated coroutine's main routine has already returned.",
82                src->name, src, src->last->name, src->last );
83
84        CoroutineCtxSwitch( src, src->last );
85}
86
87// Resume implementation inlined for performance
88forall(dtype T | is_coroutine(T))
89static inline void resume(T * cor) {
90        coroutine * src = this_coroutine();             // optimization
91        coroutine * dst = get_coroutine(cor);
92
93      if( unlikely(!dst->stack.base) ) {
94                create_stack(&dst->stack, dst->stack.size);
95                CtxStart(cor, CtxInvokeCoroutine);
96        }
97
98      // not resuming self ?
99        if ( src != dst ) {
100                assertf( dst->state != Halted ,
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
105            // set last resumer
106                dst->last = src;
107        } // if
108
109      // always done for performance testing
110        CoroutineCtxSwitch( src, dst );
111}
112
113static inline void resume(coroutine * dst) {
114        coroutine * src = this_coroutine();             // optimization
115
116      // not resuming self ?
117        if ( src != dst ) {
118                assertf( dst->state != Halted ,
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
123            // set last resumer
124                dst->last = src;
125        } // if
126
127      // always done for performance testing
128        CoroutineCtxSwitch( src, dst );
129}
130
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.