Changeset 9129a84 for src/libcfa/concurrency/threads.c
- Timestamp:
- Nov 28, 2016, 4:02:45 PM (6 years ago)
- Branches:
- aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
- Children:
- 1f44196
- Parents:
- f773f67
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/threads.c
rf773f67 r9129a84 1 1 #include "threads" 2 #include "assert" 2 3 3 forall(dtype T | coroutine_t(T)) 4 void suspend(T* cor) { 4 #include <stddef.h> 5 5 6 #include <fstream> 7 8 static coroutine main_coroutine; 9 static coroutine* current_coroutine = &main_coroutine; 10 11 void ctxSwitchDirect(void* src, void* dst) { 12 current_coroutine = dst; 13 } 14 15 coroutine* this_coroutine() { 16 return current_coroutine; 17 } 18 19 void ?{}(coroutine* this) 20 { 21 this->last = NULL; 22 this->name = "A Coroutine"; 23 this->notHalted = true; 24 } 25 26 void suspend() { 27 coroutine* src = this_coroutine(); // optimization 28 29 assertf( src->last == (coroutine*)0, 30 "Attempt to suspend coroutine %.256s (%p) that has never been resumed.\n" 31 "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", 32 src->name, src ); 33 assertf( src->last->notHalted, 34 "Attempt by coroutine %.256s (%p) to suspend back to terminated coroutine %.256s (%p).\n" 35 "Possible cause is terminated coroutine's main routine has already returned.", 36 src->name, src, src->last->name, src->last ); 37 38 ctxSwitchDirect( src, src->last ); 6 39 } 7 40 8 41 forall(dtype T | coroutine_t(T)) 9 42 void resume(T* cor) { 43 coroutine* src = this_coroutine(); // optimization 44 coroutine* dst = this_coroutine(cor); 10 45 46 if ( src != dst ) { // not resuming self ? 47 assertf( dst->notHalted , 48 "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n" 49 "Possible cause is terminated coroutine's main routine has already returned.", 50 src->name, src, dst->name, dst ); 51 dst->last = src; // set last resumer 52 } // if 53 ctxSwitchDirect( src, dst ); // always done for performance testing 11 54 }
Note: See TracChangeset
for help on using the changeset viewer.