Newer
Older
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2020 Florian Schmaus
#include <chrono> // for milliseconds, operator+, hig...
#include <iostream> // for operator<<, basic_ostream, endl
#include "CountingPrivateSemaphore.hpp" // for CPS
#include "Emper.hpp"
#include "Fiber.hpp" // for Fiber
#include "Runtime.hpp" // for Runtime
#include "emper-common.h" // for UNUSED_ARG, workerid_t
#include "emper-config.h"
#include "emper.hpp" // for spawn
#ifdef EMPER_HAS_COMPARE_H
#include <compare> // for operator>=, strong_ordering
#endif
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;
// TODO: The suppressed linter error below may be a false positive
// reported by clang-tidy.
// NOLINTNEXTLINE(modernize-use-nullptr)
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::getFullVersion() << 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);
}
auto main(UNUSED_ARG int argc, UNUSED_ARG char* argv[]) -> int {
Runtime runtime;
Fiber* fibFiber = Fiber::from(&alphaFiber);
std::cout << "Just alloacted alpha fiber at " << fibFiber << std::endl;
runtime.scheduleFromAnywhere(*fibFiber);