Commit 03fde419 authored by neverlord's avatar neverlord

c test

parent 0d233f48
...@@ -11,31 +11,6 @@ ...@@ -11,31 +11,6 @@
#include "cppa_fibre.h" #include "cppa_fibre.h"
static ucontext_t ctx[2];
__thread int m_count = 0;
struct cppa_fibre
{
ucontext_t m_context;
};
void cppa_fibre_ctor(cppa_fibre* instance);
void cppa_fibre_ctor(cppa_fibre* instance, void (*fun)());
void cppa_fibre_dtor(cppa_fibre* instance);
void cppa_fibre_switch(cppa_fibre* from, cppa_fibre* to);
void cppa_fibre_yield(int value);
int cppa_fibre_yielded_value();
typedef struct fibre_wrapper* fibre_ptr;
fibre_ptr get_fibre();
void coroutine() void coroutine()
{ {
for (;;) for (;;)
......
#include <iostream>
#include "cppa_fibre.h"
using std::cout;
using std::endl;
struct pseudo_worker
{
int m_value;
pseudo_worker() : m_value(0) { }
void operator()()
{
for (;;)
{
++m_value;
cout << "value = " << m_value << endl;
cppa_fibre_yield(0);
}
}
};
void coroutine()
{
auto pw = reinterpret_cast<pseudo_worker*>(cppa_fibre_init_switch_arg());
(*pw)();
}
int main()
{
pseudo_worker pw;
cppa_fibre fself;
cppa_fibre fcoroutine;
cppa_fibre_ctor(&fself);
cppa_fibre_ctor2(&fcoroutine, coroutine, &pw);
cppa_fibre_initialize(&fcoroutine);
for (int i = 1; i < 11; ++i)
{
cout << "i = " << i << endl;
cppa_fibre_switch(&fself, &fcoroutine);
}
cppa_fibre_dtor(&fself);
cppa_fibre_dtor(&fcoroutine);
return 0;
}
...@@ -2,24 +2,20 @@ ...@@ -2,24 +2,20 @@
#define _XOPEN_SOURCE #define _XOPEN_SOURCE
#endif #endif
#include <stdio.h>
#include <ucontext.h> #include <ucontext.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <stdlib.h>
#include <signal.h> #include <signal.h>
#include <stddef.h> #include <stddef.h>
#include <string.h>
#include <stdio.h>
#include "cppa_fibre.h" #include "cppa_fibre.h"
/* __thread void* s_switch_arg;
struct cppa_fibre_struct __thread int s_yield_value;
{ __thread ucontext_t* s_caller;
int m_initialized; __thread ucontext_t* s_callee;
ucontext_t m_context;
void (*m_fun)();
void* m_init_arg;
};
*/
void cppa_fibre_ctor(cppa_fibre* instance) void cppa_fibre_ctor(cppa_fibre* instance)
{ {
...@@ -51,34 +47,39 @@ void cppa_fibre_initialize(cppa_fibre* instance) ...@@ -51,34 +47,39 @@ void cppa_fibre_initialize(cppa_fibre* instance)
0); 0);
instance->m_context.uc_stack.ss_size = SIGSTKSZ; instance->m_context.uc_stack.ss_size = SIGSTKSZ;
makecontext(&(instance->m_context), instance->m_fun, 0); makecontext(&(instance->m_context), instance->m_fun, 0);
s_switch_arg = instance->m_init_arg;
} }
} }
void cppa_fibre_dtor(cppa_fibre* instance) void cppa_fibre_dtor(cppa_fibre* instance)
{ {
if (instance->m_state == 2)
{
munmap(instance->m_context.uc_stack.ss_sp, SIGSTKSZ);
}
} }
/* void* cppa_fibre_init_switch_arg()
* @brief Returns {
*/ return s_switch_arg;
void* cppa_fibre_init_switch_arg(); }
void cppa_fibre_switch(cppa_fibre* from, cppa_fibre* to);
/*
* Switches back to the calling fibre.
*/
void cppa_fibre_yield(int value);
/*
* Gets the yielded value of the client fibre.
*/
int cppa_fibre_yielded_value();
#ifdef __cplusplus void cppa_fibre_switch(cppa_fibre* from, cppa_fibre* to)
{
ucontext_t* ctx_from = &(from->m_context);
ucontext_t* ctx_to = &(to->m_context);
s_caller = ctx_from;
s_callee = ctx_to;
swapcontext(ctx_from, ctx_to);
} }
#endif
#endif void cppa_fibre_yield(int value)
{
s_yield_value = value;
swapcontext(s_callee, s_caller);
}
int cppa_fibre_yielded_value()
{
return s_yield_value;
}
...@@ -10,11 +10,7 @@ extern "C" { ...@@ -10,11 +10,7 @@ extern "C" {
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <ucontext.h> #include <ucontext.h>
#include <sys/mman.h>
#include <signal.h>
#include <stddef.h>
struct cppa_fibre_struct struct cppa_fibre_struct
{ {
...@@ -27,7 +23,7 @@ struct cppa_fibre_struct ...@@ -27,7 +23,7 @@ struct cppa_fibre_struct
void* m_init_arg; void* m_init_arg;
}; };
typedef cppa_fibre_struct cppa_fibre; typedef struct cppa_fibre_struct cppa_fibre;
void cppa_fibre_ctor(cppa_fibre* instance); void cppa_fibre_ctor(cppa_fibre* instance);
...@@ -43,6 +39,9 @@ void cppa_fibre_ctor2(cppa_fibre* instance, ...@@ -43,6 +39,9 @@ void cppa_fibre_ctor2(cppa_fibre* instance,
void (*fun)(), void (*fun)(),
void* switch_arg); void* switch_arg);
/*
* @warning call directly before the first switch
*/
void cppa_fibre_initialize(cppa_fibre* instance); void cppa_fibre_initialize(cppa_fibre* instance);
void cppa_fibre_dtor(cppa_fibre* instance); void cppa_fibre_dtor(cppa_fibre* instance);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment