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

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since f820f42 was c18bf9e, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Visibility concurrency

  • Property mode set to 100644
File size: 7.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
[eaf269d]12// Last Modified On : Thu Jan 6 16:33:16 2022
13// Update Count : 12
[6a3d2e7]14//
15
[6b0b624]16#pragma once
[6a3d2e7]17
[91c389a]18#include <assert.h>
[6a3d2e7]19#include "invoke.h"
[1c01c58]20#include "../exception.hfa"
21
22//-----------------------------------------------------------------------------
23// Exception thrown from resume when a coroutine stack is cancelled.
[ecfd758]24EHM_FORALL_EXCEPTION(CoroutineCancelled, (coroutine_t &), (coroutine_t)) (
[1c01c58]25 coroutine_t * the_coroutine;
26 exception_t * the_exception;
27);
28
[fd54fef]29forall(T &)
[1c01c58]30void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src);
31
[fd54fef]32forall(T &)
[1c01c58]33const char * msg(CoroutineCancelled(T) *);
[6a3d2e7]34
35//-----------------------------------------------------------------------------
36// Coroutine trait
37// Anything that implements this trait can be resumed.
38// Anything that is resumed is a coroutine.
[b583113]39trait is_coroutine(T & | IS_RESUMPTION_EXCEPTION(CoroutineCancelled, (T))) {
[1c01c58]40 void main(T & this);
[e84ab3d]41 coroutine$ * get_coroutine(T & this);
[6a3d2e7]42};
43
[e84ab3d]44#define DECL_COROUTINE(X) static inline coroutine$* get_coroutine(X& this) { return &this.__cor; } void main(X& this)
[c84e80a]45
[6a3d2e7]46//-----------------------------------------------------------------------------
47// Ctors and dtors
[de6319f]48// void ?{}( coStack_t & this );
49// void ^?{}( coStack_t & this );
50
[e84ab3d]51void ?{}( coroutine$ & this, const char name[], void * storage, size_t storageSize );
52void ^?{}( coroutine$ & this );
[de6319f]53
[e84ab3d]54static inline void ?{}( coroutine$ & this) { this{ "Anonymous Coroutine", 0p, 0 }; }
55static inline void ?{}( coroutine$ & this, size_t stackSize) { this{ "Anonymous Coroutine", 0p, stackSize }; }
56static inline void ?{}( coroutine$ & this, void * storage, size_t storageSize ) { this{ "Anonymous Coroutine", storage, storageSize }; }
57static inline void ?{}( coroutine$ & this, const char name[]) { this{ name, 0p, 0 }; }
58static inline void ?{}( coroutine$ & this, const char name[], size_t stackSize ) { this{ name, 0p, stackSize }; }
[6a3d2e7]59
60//-----------------------------------------------------------------------------
61// Public coroutine API
[5456537]62forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled, (T)); })
[83a071f9]63void prime(T & cor);
[6a3d2e7]64
[e84ab3d]65static inline struct coroutine$ * active_coroutine() { return active_thread()->curr_cor; }
[d4e68a6]66
[6a3d2e7]67//-----------------------------------------------------------------------------
68// PRIVATE exposed because of inline
69
70// Start coroutine routines
71extern "C" {
[c7a900a]72 void __cfactx_invoke_coroutine(void (*main)(void *), void * this);
[6a3d2e7]73
[fd54fef]74 forall(T &)
[e84ab3d]75 void __cfactx_start(void (*main)(T &), struct coroutine$ * cor, T & this, void (*invoke)(void (*main)(void *), void *));
[3c06bba]76
[e84ab3d]77 extern void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct coroutine$ *) __attribute__ ((__noreturn__));
[3c06bba]78
[c7a900a]79 extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch");
[6a3d2e7]80}
81
82// Private wrappers for context switch and stack creation
[3c06bba]83// Wrapper for co
[e84ab3d]84static inline void $ctx_switch( coroutine$ * src, coroutine$ * dst ) __attribute__((nonnull (1, 2))) {
[3c06bba]85 // set state of current coroutine to inactive
[ae7be7a]86 src->state = src->state == Halted ? Halted : Blocked;
[3c06bba]87
[ab5baab]88 // get the active thread once
[e84ab3d]89 thread$ * athrd = active_thread();
[ab5baab]90
91 // Mark the coroutine
92 /* paranoid */ verify( !athrd->corctx_flag );
93 athrd->corctx_flag = true;
94
[3c06bba]95 // set new coroutine that task is executing
[ab5baab]96 athrd->curr_cor = dst;
[3c06bba]97
98 // context switch to specified coroutine
[ab5baab]99 /* paranoid */ verify( dst->context.SP );
[c7a900a]100 __cfactx_switch( &src->context, &dst->context );
101 // when __cfactx_switch returns we are back in the src coroutine
[3c06bba]102
[ab5baab]103 /* paranoid */ verify( athrd->corctx_flag );
104 athrd->corctx_flag = false;
105
[3c06bba]106 // set state of new coroutine to active
107 src->state = Active;
108
[121be3e]109 if( unlikely(src->cancellation != 0p) ) {
[c7a900a]110 __cfactx_coroutine_unwind(src->cancellation, src);
[3c06bba]111 }
112}
113
[bfcf6b9]114extern void __stack_prepare( __stack_info_t * this, size_t size /* ignored if storage already allocated */);
[6a3d2e7]115
116// Suspend implementation inlined for performance
[427854b]117extern "C" {
118 static inline void __cfactx_suspend(void) {
119 // optimization : read TLS once and reuse it
120 // Safety note: this is preemption safe since if
121 // preemption occurs after this line, the pointer
122 // will also migrate which means this value will
123 // stay in syn with the TLS
[e84ab3d]124 coroutine$ * src = active_coroutine();
[427854b]125
126 assertf( src->last != 0,
127 "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
128 "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
129 src->name, src );
130 assertf( src->last->state != Halted,
131 "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
132 "Possible cause is terminated coroutine's main routine has already returned.",
133 src->name, src, src->last->name, src->last );
[6a3d2e7]134
[427854b]135 $ctx_switch( src, src->last );
136 }
[6a3d2e7]137}
138
[fd54fef]139forall(T & | is_coroutine(T))
[b583113]140void __cfaehm_cancelled_coroutine(
[e84ab3d]141 T & cor, coroutine$ * desc, EHM_DEFAULT_VTABLE(CoroutineCancelled, (T)) );
[1c01c58]142
[6a3d2e7]143// Resume implementation inlined for performance
[5456537]144forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled, (T)); })
[aa00626]145static inline T & resume(T & cor) {
[14a61b5]146 // optimization : read TLS once and reuse it
147 // Safety note: this is preemption safe since if
148 // preemption occurs after this line, the pointer
149 // will also migrate which means this value will
150 // stay in syn with the TLS
[e84ab3d]151 coroutine$ * src = active_coroutine();
152 coroutine$ * dst = get_coroutine(cor);
[6a3d2e7]153
[121be3e]154 if( unlikely(dst->context.SP == 0p) ) {
[eaf269d]155 __stack_prepare(&dst->stack, DEFAULT_STACK_SIZE);
[c7a900a]156 __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine);
[6a3d2e7]157 }
158
[4aa2fb2]159 // not resuming self ?
[6a3d2e7]160 if ( src != dst ) {
[ee897e4b]161 assertf( dst->state != Halted ,
[6a3d2e7]162 "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
163 "Possible cause is terminated coroutine's main routine has already returned.",
164 src->name, src, dst->name, dst );
165
[4aa2fb2]166 // set last resumer
[6a3d2e7]167 dst->last = src;
[b462670]168 dst->starter = dst->starter ? dst->starter : src;
[14a61b5]169 }
[6a3d2e7]170
[4aa2fb2]171 // always done for performance testing
[ac2b598]172 $ctx_switch( src, dst );
[1c01c58]173 if ( unlikely(dst->cancellation) ) {
[b583113]174 __cfaehm_cancelled_coroutine( cor, dst, _default_vtable );
[1c01c58]175 }
[aa00626]176
177 return cor;
[6a3d2e7]178}
179
[e84ab3d]180static inline void resume( coroutine$ * dst ) __attribute__((nonnull (1))) {
[14a61b5]181 // optimization : read TLS once and reuse it
182 // Safety note: this is preemption safe since if
183 // preemption occurs after this line, the pointer
184 // will also migrate which means this value will
185 // stay in syn with the TLS
[e84ab3d]186 coroutine$ * src = active_coroutine();
[77e6fcb]187
[4aa2fb2]188 // not resuming self ?
[77e6fcb]189 if ( src != dst ) {
[ee897e4b]190 assertf( dst->state != Halted ,
[77e6fcb]191 "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
192 "Possible cause is terminated coroutine's main routine has already returned.",
193 src->name, src, dst->name, dst );
194
[4aa2fb2]195 // set last resumer
[77e6fcb]196 dst->last = src;
[14a61b5]197 }
[77e6fcb]198
[4aa2fb2]199 // always done for performance testing
[ac2b598]200 $ctx_switch( src, dst );
[77e6fcb]201}
202
[6a3d2e7]203// Local Variables: //
204// mode: c //
205// tab-width: 4 //
206// End: //
Note: See TracBrowser for help on using the repository browser.