-
Florian Schmaus authored
We need to spawn another fiber in fibKickoff
Florian Schmaus authoredWe need to spawn another fiber in fibKickoff
SimplestFibTest.cpp 1.85 KiB
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <list>
#include <string>
#include <thread>
#include "Runtime.hpp"
#include "Common.hpp"
#include "PrivateSemaphore.hpp"
#include "BinaryPrivateSemaphore.hpp"
#include "CountingPrivateSemaphore.hpp"
#include "Debug.hpp"
#include "emper.hpp"
typedef struct {
int n;
int* result;
PS* sem;
} fibParams;
static void fib(void *voidParams) {
fibParams* params = static_cast<fibParams*>(voidParams);
int n = params->n;
int *result = params->result;
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 = 4;
int result;
BPS sem;
fibParams params = { fibNum, &result, &sem };
Fiber* fibFiber = Fiber::from(fib, ¶ms);
async(fibFiber);
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();
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();
return 0;
}