Skip to content
Snippets Groups Projects
SimplestFibTest.cpp 2.17 KiB
Newer Older
  • Learn to ignore specific revisions
  • #include <cstdlib>	 // for abort, exit, EXIT_SUCCESS
    #include <iostream>	 // for operator<<, basic_ostream::o...
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    
    
    #include "BinaryPrivateSemaphore.hpp"		 // for BPS
    #include "CountingPrivateSemaphore.hpp"	 // for CPS
    #include "Debug.hpp"										 // for WDBG
    #include "Fiber.hpp"										 // for Fiber
    #include "PrivateSemaphore.hpp"					 // for PS
    #include "Runtime.hpp"									 // for Runtime
    #include "emper-common.h"								 // for UNUSED_ARG
    #include "emper.hpp"										 // for async
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    
    typedef struct {
    	int n;
    	int* result;
    	PS* sem;
    } fibParams;
    
    
    static void fib(void* voidParams) {
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    	fibParams* params = static_cast<fibParams*>(voidParams);
    	int n = params->n;
    
    	if (!result) {
    
    		std::cerr << "voidParams: " << voidParams << " n: " << params->n << " sem: " << params->sem
    							<< std::endl;
    		abort();
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    	PS* sem = params->sem;
    
    	if (n < 2) {
    		*result = n;
    	} else {
    		CPS newSem(2);
    
    		int a, b;
    
    		fibParams newParams1;
    		newParams1.n = n - 1;
    		newParams1.result = &a;
    		newParams1.sem = &newSem;
    		fibParams newParams2;
    		newParams2.n = n - 2;
    		newParams2.result = &b;
    		newParams2.sem = &newSem;
    
    		Fiber* f1 = Fiber::from(&fib, &newParams1);
    		Fiber* f2 = Fiber::from(&fib, &newParams2);
    
    		Runtime* runtime = Runtime::getRuntime();
    		runtime->schedule(*f1);
    		runtime->schedule(*f2);
    
    		WDBG("fib: Calling wait for n=" << n);
    		newSem.wait();
    
    		*result = a + b;
    	}
    
    	WDBG("fib: Calling signalAndExit for n=" << n);
    	sem->signalAndExit();
    }
    
    static void fibKickoff() {
    
    	const int fibNum = 2;
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    	int result;
    	BPS sem;
    
    	fibParams params = {fibNum, &result, &sem};
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    	Fiber* fibFiber = Fiber::from(fib, &params);
    	async(fibFiber);
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    
    	sem.wait();
    
    	std::cout << "fib(" << fibNum << ") = " << result << std::endl;
    	exit(EXIT_SUCCESS);
    }
    
    
    int main(UNUSED_ARG int argc, UNUSED_ARG char* argv[]) {
    	// const unsigned nthreads = std::thread::hardware_concurrency();
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    	const unsigned nthreads = 2;
    
    	std::cout << "Number of threads: " << nthreads << std::endl;
    
    	Runtime runtime(nthreads);
    
    	Fiber* fibFiber = Fiber::from(&fibKickoff);
    
    	std::cout << "Just alloacted alpha fiber at " << fibFiber << std::endl;
    
    	runtime.schedule(*fibFiber);
    
    	runtime.waitUntilFinished();
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    	return 0;
    }