source: src/libcfa/concurrency/threads.c @ 9129a84

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 9129a84 was 9129a84, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Implemented suspend and resume for coroutines (CtxSw? does nothing)

  • Property mode set to 100644
File size: 1.6 KB
Line 
1#include "threads"
2#include "assert"
3
4#include <stddef.h>
5
6#include <fstream>
7 
8static coroutine main_coroutine;
9static coroutine* current_coroutine = &main_coroutine;
10
11void ctxSwitchDirect(void* src, void* dst) {
12        current_coroutine = dst;
13}
14
15coroutine* this_coroutine() {
16        return current_coroutine;
17}
18
19void ?{}(coroutine* this)
20{
21        this->last = NULL;
22      this->name = "A Coroutine";
23      this->notHalted = true;
24}
25
26void 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 );
39}
40
41forall(dtype T | coroutine_t(T))
42void resume(T* cor) {
43        coroutine* src = this_coroutine();              // optimization
44        coroutine* dst = this_coroutine(cor);
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
54}
Note: See TracBrowser for help on using the repository browser.