Ignore:
Timestamp:
Nov 28, 2016, 4:02:45 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
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
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/threads.c

    rf773f67 r9129a84  
    11#include "threads"
     2#include "assert"
    23
    3 forall(dtype T | coroutine_t(T))
    4 void suspend(T* cor) {
     4#include <stddef.h>
    55
     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 );
    639}
    740
    841forall(dtype T | coroutine_t(T))
    942void resume(T* cor) {
     43        coroutine* src = this_coroutine();              // optimization
     44        coroutine* dst = this_coroutine(cor);
    1045
     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
    1154}
Note: See TracChangeset for help on using the changeset viewer.