source: src/libcfa/concurrency/coroutines@ fe26fbf

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since fe26fbf was 77e6fcb, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Removed trailing semi-colon on thread/coroutine declaration macro

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