source: src/libcfa/concurrency/coroutine @ de6319f

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerwith_gc
Last change on this file since de6319f was de6319f, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Implemented clusters and added many constructors for threads/coroutines/processors

  • Property mode set to 100644
File size: 4.6 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
34// void ?{}( coStack_t & this );
35// void ^?{}( coStack_t & this );
36
37void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize );
38void ^?{}( coroutine_desc & this );
39
40static inline void ?{}( coroutine_desc & this)                                       { this{ "Anonymous Coroutine", NULL, 0 }; }
41static inline void ?{}( coroutine_desc & this, size_t stackSize)                     { this{ "Anonymous Coroutine", NULL, stackSize }; }
42static inline void ?{}( coroutine_desc & this, void * storage, size_t storageSize )  { this{ "Anonymous Coroutine", storage, storageSize }; }
43static inline void ?{}( coroutine_desc & this, const char * name)                    { this{ name, NULL, 0 }; }
44static inline void ?{}( coroutine_desc & this, const char * name, size_t stackSize ) { this{ name, NULL, stackSize }; }
45
46//-----------------------------------------------------------------------------
47// Public coroutine API
48static inline void suspend();
49
50forall(dtype T | is_coroutine(T))
51static inline void resume(T & cor);
52
53forall(dtype T | is_coroutine(T))
54void prime(T & cor);
55
56//-----------------------------------------------------------------------------
57// PRIVATE exposed because of inline
58
59// Start coroutine routines
60extern "C" {
61      forall(dtype T | is_coroutine(T))
62      void CtxInvokeCoroutine(T * this);
63
64      forall(dtype T | is_coroutine(T))
65      void CtxStart(T * this, void ( *invoke)(T *));
66}
67
68// Private wrappers for context switch and stack creation
69extern void CoroutineCtxSwitch(coroutine_desc * src, coroutine_desc * dst);
70extern void create_stack( coStack_t * this, unsigned int storageSize );
71
72// Suspend implementation inlined for performance
73static inline void suspend() {
74        coroutine_desc * src = TL_GET( this_coroutine );                        // optimization
75
76        assertf( src->last != 0,
77                "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
78                "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
79                src->name, src );
80        assertf( src->last->state != Halted,
81                "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
82                "Possible cause is terminated coroutine's main routine has already returned.",
83                src->name, src, src->last->name, src->last );
84
85        CoroutineCtxSwitch( src, src->last );
86}
87
88// Resume implementation inlined for performance
89forall(dtype T | is_coroutine(T))
90static inline void resume(T & cor) {
91        coroutine_desc * src = TL_GET( this_coroutine );                        // optimization
92        coroutine_desc * dst = get_coroutine(cor);
93
94        if( unlikely(!dst->stack.base) ) {
95                create_stack(&dst->stack, dst->stack.size);
96                CtxStart(&cor, CtxInvokeCoroutine);
97        }
98
99        // not resuming self ?
100        if ( src != dst ) {
101                assertf( dst->state != Halted ,
102                        "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
103                        "Possible cause is terminated coroutine's main routine has already returned.",
104                        src->name, src, dst->name, dst );
105
106                // set last resumer
107                dst->last = src;
108                dst->starter = dst->starter ? dst->starter : src;
109        } // if
110
111        // always done for performance testing
112        CoroutineCtxSwitch( src, dst );
113}
114
115static inline void resume(coroutine_desc * dst) {
116        coroutine_desc * src = TL_GET( this_coroutine );                        // optimization
117
118        // not resuming self ?
119        if ( src != dst ) {
120                assertf( dst->state != Halted ,
121                        "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
122                        "Possible cause is terminated coroutine's main routine has already returned.",
123                        src->name, src, dst->name, dst );
124
125                // set last resumer
126                dst->last = src;
127        } // if
128
129        // always done for performance testing
130        CoroutineCtxSwitch( src, dst );
131}
132
133// Local Variables: //
134// mode: c //
135// tab-width: 4 //
136// End: //
Note: See TracBrowser for help on using the repository browser.