/* Records when the fiber has started and when it is done so that we know when to free its resources. It is called in the fiber's context of execution. */ static void fiberStart( void (*func)(void) ) { fiberList[currentFiber].active = 1; func(); fiberList[currentFiber].active = 0; /* Yield control, but because active == 0, this will free the fiber */ fiberYield(); }
// Records when the fiber has started and when it is done // so that we know when to free its resources. It is called in the fiber's // context of execution. static void fiberStart( ctr_object* block ) { fiberList[currentFiber].active = 1; if(FIBER_YIELDED != NULL) ctr_block_run_variadic(block, 1, FIBER_YIELDED); else ctr_block_run_variadic(block, 0); fiberList[currentFiber].active = 0; // Yield control, but because active == 0, this will free the fiber fiberYield(); }
void fiber1() { int i; for ( i = 0; i < 5; ++ i ) { printf( "Hey, I'm fiber #1: %d\n", i ); fiberYield(); } return; }
void squares() { int i; /*sleep( 5 ); */ for ( i = 0; i < 10; ++ i ) { printf( "%d*%d = %d\n", i, i, i*i ); fiberYield(); } }
int waitForAllFibers() { int fibersRemaining = 0; /* If we are in a fiber, wait for all the *other* fibers to quit */ if ( inFiber ) fibersRemaining = 1; LF_DEBUG_OUT1( "Waiting until there are only %d threads remaining...", fibersRemaining ); while ( numFibers > fibersRemaining ) { fiberYield(); } return LF_NOERROR; }
void fibonacchi() { int i; int fib[2] = { 0, 1 }; /*sleep( 2 ); */ printf( "fibonacchi(0) = 0\nfibonnachi(1) = 1\n" ); for( i = 2; i < 15; ++ i ) { int nextFib = fib[0] + fib[1]; printf( "fibonacchi(%d) = %d\n", i, nextFib ); fib[0] = fib[1]; fib[1] = nextFib; fiberYield(); } }
ctr_object* ctr_fiber_yield(ctr_object* myself, ctr_argument* argumentList) { //int fiber = ctr_internal_object_find_property(myself, ctr_build_string_from_cstring("fiberId"), CTR_CATEGORY_PRIVATE_PROPERTY)->value.nvalue; FIBER_YIELDED = argumentList->object; // record the output, or set to NULL fiberYield(); return myself; //Won't really reach here until the end of the fiber chain }