Skip to content
Snippets Groups Projects
CppApiTest.cpp 1.01 KiB
Newer Older
  • Learn to ignore specific revisions
  • // SPDX-License-Identifier: LGPL-3.0-or-later
    // Copyright © 2020 Florian Schmaus
    
    #include <atomic>		// for atomic_uint, __atomic_base
    #include <cstdlib>	// for exit, EXIT_FAILURE, EXIT_SUC...
    
    
    #include "CountingPrivateSemaphore.hpp"	 // for CountingPrivateSemaphore
    
    #include "Fiber.hpp"
    #include "Runtime.hpp"		 // for Runtime
    #include "emper-common.h"	 // for UNUSED_ARG
    #include "emper.hpp"			 // for async, spawn
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    
    static std::atomic_uint counter;
    
    
    static void increaseCounterByOne() { counter++; }
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    
    
    static void mainFiber() {
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    	const unsigned int FIBER_COUNT = 100;
    
    	CountingPrivateSemaphore cps;
    
    	for (unsigned int i = 0; i < FIBER_COUNT; ++i) {
    		spawn(&increaseCounterByOne, cps);
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    
    	cps.wait();
    
    	if (counter != FIBER_COUNT) {
    		exit(EXIT_FAILURE);
    	}
    
    	exit(EXIT_SUCCESS);
    }
    
    
    auto main(UNUSED_ARG int arg, UNUSED_ARG char* argv[]) -> int {
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    	Runtime runtime;
    
    
    	Fiber* alphaFiber = Fiber::from([] { async(&mainFiber); });
    	runtime.scheduleFromAnywhere(*alphaFiber);
    
    Florian Schmaus's avatar
    Florian Schmaus committed
    
    	runtime.waitUntilFinished();
    
    	return EXIT_FAILURE;
    }