source: src/libcfa/concurrency/coroutine @ 90cedbdd

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 90cedbdd was 14a61b5, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Ground work for quiescing processors and update/remove TL_GET/TL_SETs. Still work to be done in both cases

  • Property mode set to 100644
File size: 5.2 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        // optimization : read TLS once and reuse it
75        // Safety note: this is preemption safe since if
76        // preemption occurs after this line, the pointer
77        // will also migrate which means this value will
78        // stay in syn with the TLS
79        coroutine_desc * src = TL_GET( this_coroutine );
80
81        assertf( src->last != 0,
82                "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
83                "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
84                src->name, src );
85        assertf( src->last->state != Halted,
86                "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
87                "Possible cause is terminated coroutine's main routine has already returned.",
88                src->name, src, src->last->name, src->last );
89
90        CoroutineCtxSwitch( src, src->last );
91}
92
93// Resume implementation inlined for performance
94forall(dtype T | is_coroutine(T))
95static inline void resume(T & cor) {
96        // optimization : read TLS once and reuse it
97        // Safety note: this is preemption safe since if
98        // preemption occurs after this line, the pointer
99        // will also migrate which means this value will
100        // stay in syn with the TLS
101        coroutine_desc * src = TL_GET( this_coroutine );
102        coroutine_desc * dst = get_coroutine(cor);
103
104        if( unlikely(!dst->stack.base) ) {
105                create_stack(&dst->stack, dst->stack.size);
106                CtxStart(&cor, CtxInvokeCoroutine);
107        }
108
109        // not resuming self ?
110        if ( src != dst ) {
111                assertf( dst->state != Halted ,
112                        "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
113                        "Possible cause is terminated coroutine's main routine has already returned.",
114                        src->name, src, dst->name, dst );
115
116                // set last resumer
117                dst->last = src;
118                dst->starter = dst->starter ? dst->starter : src;
119        }
120
121        // always done for performance testing
122        CoroutineCtxSwitch( src, dst );
123}
124
125static inline void resume(coroutine_desc * dst) {
126        // optimization : read TLS once and reuse it
127        // Safety note: this is preemption safe since if
128        // preemption occurs after this line, the pointer
129        // will also migrate which means this value will
130        // stay in syn with the TLS
131        coroutine_desc * src = TL_GET( this_coroutine );
132
133        // not resuming self ?
134        if ( src != dst ) {
135                assertf( dst->state != Halted ,
136                        "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
137                        "Possible cause is terminated coroutine's main routine has already returned.",
138                        src->name, src, dst->name, dst );
139
140                // set last resumer
141                dst->last = src;
142        }
143
144        // always done for performance testing
145        CoroutineCtxSwitch( src, dst );
146}
147
148// Local Variables: //
149// mode: c //
150// tab-width: 4 //
151// End: //
Note: See TracBrowser for help on using the repository browser.