Skip to content

Instantly share code, notes, and snippets.

@danielealbano
Created August 28, 2021 00:54
Show Gist options
  • Save danielealbano/147254fc406de3d74aba68fb7b3cde2d to your computer and use it in GitHub Desktop.
Save danielealbano/147254fc406de3d74aba68fb7b3cde2d to your computer and use it in GitHub Desktop.
cachegrand fiber - example
// requires
// https://github.com/danielealbano/cachegrand/blob/41-implement-fibers-support/src/fiber.c
// https://github.com/danielealbano/cachegrand/blob/41-implement-fibers-support/src/fiber.h
// https://github.com/danielealbano/cachegrand/blob/41-implement-fibers-support/src/fiber.s
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "fiber.h"
void foo(fiber_t* fiber_from, fiber_t* fiber_to) {
printf("Hello from foo!\n");
printf("User data: %ld\n", (long)fiber_to->start_fp_user_data);
fiber_context_swap(fiber_to, fiber_from);
}
void boo(fiber_t* fiber_from, fiber_t* fiber_to) {
printf("Hello from boo!\n");
printf("User data: %ld\n", (long)fiber_to->start_fp_user_data);
fiber_context_swap(fiber_to, fiber_from);
}
int main() {
fiber_t *fiber_foo, *fiber_boo, fiber_main = { 0 };
fiber_foo = fiber_new(1024 * 1024 * 2, foo, (void*)123);
fiber_boo = fiber_new(1024 * 1024 * 2, boo, (void*)321);
fiber_context_swap(&fiber_main, fiber_foo);
fiber_context_swap(&fiber_main, fiber_boo);
fiber_free(fiber_foo);
fiber_free(fiber_boo);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment