Changeset 9129a84


Ignore:
Timestamp:
Nov 28, 2016, 4:02:45 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, 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, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
1f44196
Parents:
f773f67
Message:

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

Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/examples/coroutine.c

    rf773f67 r9129a84  
    1212
    1313coroutine* this_coroutine(Fibonacci* this) {
    14 
     14      return &this->c;
    1515}
    1616
     
    1919      this->fn = 0;
    2020      fn1 = this->fn;
    21       suspend(this);            // return to last resume
     21      suspend();                // return to last resume
    2222
    2323      this->fn = 1;
    2424      fn2 = fn1;
    2525      fn1 = this->fn;
    26       suspend(this);            // return to last resume
     26      suspend();                // return to last resume
    2727
    2828      for ( ;; ) {
     
    3030            fn2 = fn1;
    3131            fn1 = this->fn;
    32             suspend(this);      // return to last resume
     32            suspend();  // return to last resume
    3333      }
    3434}
     
    3939}
    4040
    41 void main() {
     41int main() {
    4242      Fibonacci f1, f2;
    4343      for ( int i = 1; i <= 10; i += 1 ) {
    4444            sout | next(&f1) | ' ' | next(&f2) | endl;
    4545      }
     46
     47      return 0;
    4648}
  • src/libcfa/assert

    rf773f67 r9129a84  
    2222        #define __STRINGIFY__(str) #str
    2323        #define __VSTRINGIFY__(str) __STRINGIFY__(str)
    24         #define assertf(expr, fmt, ...) ((expr) ? static_cast<void>(0) : __assert_fail_f(__VSTRINGIFY__(expr), __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, ## __VA_ARGS__ ))
     24        #define assertf(expr, fmt, ...) ((expr) ? ((void)2) : __assert_fail_f(__VSTRINGIFY__(expr), __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, ## __VA_ARGS__ ))
    2525
    2626        void __assert_fail_f( const char *assertion, const char *file, unsigned int line, const char *function, const char *fmt, ... ) __attribute__((noreturn));
  • src/libcfa/concurrency/threads

    rf773f67 r9129a84  
    1717#define __THREADS_H__
    1818
     19#include <stdbool.h>
    1920
    2021struct coroutine {
    21       int blarg;
     22      coroutine* last;
     23      const char* name;
     24      bool notHalted;
    2225};
     26
     27void ?{}(coroutine* this);
    2328
    2429trait coroutine_t(dtype T) {
     
    2631};
    2732
    28 forall(dtype T | coroutine_t(T))
    29 void suspend(T* cor);
     33void suspend(void);
    3034
    3135forall(dtype T | coroutine_t(T))
  • 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.