Skip to content
Snippets Groups Projects
WorkerSleepExample.cpp 1.95 KiB
Newer Older
  • Learn to ignore specific revisions
  • #include <stdio.h>
    #include <stdlib.h>
    
    #include <iostream>
    #include <list>
    #include <string>
    
    #include "BinaryPrivateSemaphore.hpp"
    
    #include "CountingPrivateSemaphore.hpp"
    #include "Debug.hpp"
    
    #include "PrivateSemaphore.hpp"
    #include "Runtime.hpp"
    
    #include "emper-version.h"
    
    
    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();