source: libcfa/src/concurrency/coroutine.hfa @ f1c1339

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since f1c1339 was 76d3ca6, checked in by tdelisle <tdelisle@…>, 5 years ago

Fixed error in libcfa where suspend() should have been declared suspend(void)

  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[6a3d2e7]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//
[75a17f1]7// coroutine --
[6a3d2e7]8//
9// Author           : Thierry Delisle
10// Created On       : Mon Nov 28 12:27:26 2016
[91c389a]11// Last Modified By : Peter A. Buhr
[b10affd]12// Last Modified On : Fri Mar 30 18:23:45 2018
13// Update Count     : 8
[6a3d2e7]14//
15
[6b0b624]16#pragma once
[6a3d2e7]17
[91c389a]18#include <assert.h>
[6a3d2e7]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) {
[83a071f9]26      void main(T & this);
27      coroutine_desc * get_coroutine(T & this);
[6a3d2e7]28};
29
[83a071f9]30#define DECL_COROUTINE(X) static inline coroutine_desc* get_coroutine(X& this) { return &this.__cor; } void main(X& this)
[c84e80a]31
[6a3d2e7]32//-----------------------------------------------------------------------------
33// Ctors and dtors
[de6319f]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 }; }
[6a3d2e7]45
46//-----------------------------------------------------------------------------
47// Public coroutine API
[76d3ca6]48static inline void suspend(void);
[6a3d2e7]49
50forall(dtype T | is_coroutine(T))
[83a071f9]51static inline void resume(T & cor);
[6a3d2e7]52
53forall(dtype T | is_coroutine(T))
[83a071f9]54void prime(T & cor);
[6a3d2e7]55
56//-----------------------------------------------------------------------------
57// PRIVATE exposed because of inline
58
59// Start coroutine routines
60extern "C" {
61      forall(dtype T | is_coroutine(T))
[8118303]62      void CtxInvokeCoroutine(T * this);
[6a3d2e7]63
64      forall(dtype T | is_coroutine(T))
[8118303]65      void CtxStart(T * this, void ( *invoke)(T *));
[6a3d2e7]66}
67
68// Private wrappers for context switch and stack creation
[c3acb841]69extern void CoroutineCtxSwitch(coroutine_desc * src, coroutine_desc * dst);
[8118303]70extern void create_stack( coStack_t * this, unsigned int storageSize );
[6a3d2e7]71
72// Suspend implementation inlined for performance
[76d3ca6]73static inline void suspend(void) {
[14a61b5]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 );
[6a3d2e7]80
81        assertf( src->last != 0,
[8def349]82                "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
[6a3d2e7]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 );
[ee897e4b]85        assertf( src->last->state != Halted,
[8def349]86                "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
[6a3d2e7]87                "Possible cause is terminated coroutine's main routine has already returned.",
88                src->name, src, src->last->name, src->last );
89
[0c92c9f]90        CoroutineCtxSwitch( src, src->last );
[6a3d2e7]91}
92
93// Resume implementation inlined for performance
94forall(dtype T | is_coroutine(T))
[83a071f9]95static inline void resume(T & cor) {
[14a61b5]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 );
[c3acb841]102        coroutine_desc * dst = get_coroutine(cor);
[6a3d2e7]103
[4aa2fb2]104        if( unlikely(!dst->stack.base) ) {
[6a3d2e7]105                create_stack(&dst->stack, dst->stack.size);
[83a071f9]106                CtxStart(&cor, CtxInvokeCoroutine);
[6a3d2e7]107        }
108
[4aa2fb2]109        // not resuming self ?
[6a3d2e7]110        if ( src != dst ) {
[ee897e4b]111                assertf( dst->state != Halted ,
[6a3d2e7]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
[4aa2fb2]116                // set last resumer
[6a3d2e7]117                dst->last = src;
[b462670]118                dst->starter = dst->starter ? dst->starter : src;
[14a61b5]119        }
[6a3d2e7]120
[4aa2fb2]121        // always done for performance testing
[0c92c9f]122        CoroutineCtxSwitch( src, dst );
[6a3d2e7]123}
124
[c3acb841]125static inline void resume(coroutine_desc * dst) {
[14a61b5]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 );
[77e6fcb]132
[4aa2fb2]133        // not resuming self ?
[77e6fcb]134        if ( src != dst ) {
[ee897e4b]135                assertf( dst->state != Halted ,
[77e6fcb]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
[4aa2fb2]140                // set last resumer
[77e6fcb]141                dst->last = src;
[14a61b5]142        }
[77e6fcb]143
[4aa2fb2]144        // always done for performance testing
[77e6fcb]145        CoroutineCtxSwitch( src, dst );
146}
147
[6a3d2e7]148// Local Variables: //
149// mode: c //
150// tab-width: 4 //
151// End: //
Note: See TracBrowser for help on using the repository browser.