00001 #ifndef CMK_FCONTEXT_H
00002 #define CMK_FCONTEXT_H
00003
00004 #ifdef __cplusplus
00005 extern "C" {
00006 #endif
00007 typedef void * fcontext_t;
00008
00009 typedef struct uFcontext_stack_t {
00010 void *ss_sp;
00011 int ss_flags;
00012 size_t ss_size;
00013 } uFcontext_stack_t;
00014
00015 typedef struct transfer_t {
00016 fcontext_t fctx;
00017 void * data;
00018 } transfer_t;
00019
00020 extern void CthStartThread(transfer_t);
00021 typedef void (*uFcontext_fn_t)(transfer_t);
00022
00023 typedef struct data_t {
00024 void * from;
00025 void * data;
00026 } data_t;
00027
00028 typedef struct uFcontext_t {
00029 fcontext_t fctx;
00030 void (* func)(void *);
00031 uFcontext_stack_t uc_stack;
00032 struct uFcontext_t *uc_link;
00033 void *arg;
00034 data_t param;
00035 } uFcontext_t;
00036
00037 transfer_t jump_fcontext(fcontext_t const to, void *vp);
00038 fcontext_t make_fcontext(void *sp, size_t size, void (*fn)(transfer_t));
00039 transfer_t ontop_fcontext(fcontext_t const to, void *vp, transfer_t (*fn)(transfer_t));
00040
00041
00042 extern int getJcontext (uFcontext_t *__ucp);
00043
00044
00045 extern int setJcontext (uFcontext_t *__ucp);
00046
00047
00048
00049 extern int swapJcontext (uFcontext_t *__oucp, uFcontext_t *__ucp);
00050
00051 extern void makeJcontext(uFcontext_t *__ucp, uFcontext_fn_t, void (*fn)(void*), void *arg);
00052
00053 int getJcontext (uFcontext_t *__ucp) {
00054 return 0;
00055 }
00056
00057 int setJcontext (uFcontext_t *__ucp) {
00058 return swapJcontext(NULL, __ucp);
00059 }
00060
00061 void makeJcontext (uFcontext_t *__ucp, uFcontext_fn_t __func, void (*fn)(void *), void *arg) {
00062 __ucp->arg = arg;
00063 __ucp->uc_link = NULL;
00064 __ucp->func = fn;
00065 fcontext_t t = make_fcontext(__ucp->uc_stack.ss_sp, __ucp->uc_stack.ss_size, __func);
00066 __ucp->fctx = t;
00067 }
00068
00069 int swapJcontext(uFcontext_t *old_ucp, uFcontext_t *new_ucp) {
00070 new_ucp->param.from = old_ucp;
00071 new_ucp->param.data = new_ucp;
00072 transfer_t t = jump_fcontext(new_ucp->fctx, &(new_ucp->param));
00073 data_t *old_data = (data_t *)t.data;
00074 uFcontext_t *prev_ucp = (uFcontext_t *)old_data->from;
00075 if (prev_ucp)
00076 prev_ucp->fctx = t.fctx;
00077 return 0;
00078 }
00079 #ifdef __cplusplus
00080 }
00081 #endif
00082 #endif