source: src/libcfa/concurrency/coroutine @ 9181f1d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 9181f1d was b10affd, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

thread-local storage converted to structure and thread-local macros for access

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