36 #if defined(_WIN32) || defined(__CYGWIN__)
38 #define WIN32_LEAN_AND_MEAN
41 static void *main_fiber;
54 static ucontext_t main_context;
55 static ucontext_t *running_context;
65 #if defined(_WIN32) || defined(__CYGWIN__)
67 main_fiber = ConvertThreadToFiber(
NULL);
75 #if defined(_WIN32) || defined(__CYGWIN__)
77 ConvertFiberToThread();
84 void (*
function)(
void *data),
87 #if defined(_WIN32) || defined(__CYGWIN__)
89 thread->mt_thread = CreateFiber(0, (LPFIBER_START_ROUTINE)
function, data);
93 thread->mt_thread = malloc(
sizeof(
struct mtarch_t));
95 getcontext(&((
struct mtarch_t *)thread->mt_thread)->context);
97 ((
struct mtarch_t *)thread->mt_thread)->context.uc_link =
NULL;
98 ((
struct mtarch_t *)thread->mt_thread)->context.uc_stack.ss_sp =
99 ((
struct mtarch_t *)thread->mt_thread)->stack;
100 ((
struct mtarch_t *)thread->mt_thread)->context.uc_stack.ss_size =
101 sizeof(((
struct mtarch_t *)thread->mt_thread)->stack);
118 makecontext(&((
struct mtarch_t *)thread->mt_thread)->context,
119 (void (*)(
void))
function, 1, data);
127 #if defined(_WIN32) || defined(__CYGWIN__)
129 SwitchToFiber(main_fiber);
133 swapcontext(running_context, &main_context);
141 #if defined(_WIN32) || defined(__CYGWIN__)
143 SwitchToFiber(thread->mt_thread);
147 running_context = &((
struct mtarch_t *)thread->mt_thread)->context;
148 swapcontext(&main_context, running_context);
149 running_context =
NULL;
157 #if defined(_WIN32) || defined(__CYGWIN__)
159 DeleteFiber(thread->mt_thread);
163 free(thread->mt_thread);