Skip to content
Snippets Groups Projects
WorkerSleepExample.cpp 2.14 KiB
Newer Older
  • Learn to ignore specific revisions
  • #include <stdlib.h>	 // for exit, EXIT_SUCCESS
    
    #include <chrono>		 // for milliseconds, operator+, hig...
    #include <iostream>	 // for operator<<, basic_ostream, endl
    #include <ratio>		 // for ratio
    
    #include "CountingPrivateSemaphore.hpp"	 // for CPS
    #include "Fiber.hpp"										 // for Fiber
    #include "Runtime.hpp"									 // for Runtime
    #include "emper-common.h"								 // for UNUSED_ARG, workerid_t
    #include "emper-version.h"							 // for EMPER_FULL_VERSION
    #include "emper.hpp"										 // for spawn
    
    
    static unsigned int ITERATIONS = 10;
    
    static std::chrono::milliseconds SINGLE_FIBER_DURATION = std::chrono::milliseconds(3000);
    
    static std::chrono::milliseconds MULTI_FIBER_DURATION = std::chrono::milliseconds(2000);
    
    
    template <typename Rep, typename Period>
    
    static void letsGetBusy(std::chrono::duration<Rep, Period> duration) {
    
    	const std::chrono::time_point<std::chrono::high_resolution_clock> now =
    			std::chrono::high_resolution_clock::now();
    
    	const std::chrono::time_point<std::chrono::high_resolution_clock> deadline = now + duration;
    
    
    	while (std::chrono::high_resolution_clock::now() < deadline)
    		;
    
    }
    
    static void alphaFiber() {
    	const Runtime* runtime = Runtime::getRuntime();
    	const workerid_t workerCount = runtime->getWorkerCount();
    
    
    	std::cout << "Starting WorkerSleepExample with " << workerCount << " workers using " << ITERATIONS
    						<< " iterations." << std::endl
    						<< "Single fiber duration: " << SINGLE_FIBER_DURATION.count()
    						<< ", Multi fiber duration: " << MULTI_FIBER_DURATION.count() << std::endl
    						<< "EMPER version: " << EMPER_FULL_VERSION << std::endl;
    
    
    	for (unsigned int i = 0; i < ITERATIONS; ++i) {
    		letsGetBusy(SINGLE_FIBER_DURATION);
    
    		CPS cps;
    		for (workerid_t j = 0; j < workerCount; ++j) {
    
    			spawn([] { letsGetBusy(MULTI_FIBER_DURATION); }, cps);
    
    		}
    		cps.wait();
    	}
    
    	std::cout << "Finished WorkerSleepExample" << std::endl;
    	exit(EXIT_SUCCESS);
    }
    
    
    int main(UNUSED_ARG int argc, UNUSED_ARG char* argv[]) {
    
    	Runtime runtime;
    
    	Fiber* fibFiber = Fiber::from(&alphaFiber);
    
    	std::cout << "Just alloacted alpha fiber at " << fibFiber << std::endl;
    
    	runtime.schedule(*fibFiber);
    
    	runtime.waitUntilFinished();