37 #ifndef MTARCH_STACKSIZE
38 #define MTARCH_STACKSIZE 4096
41 #if defined(_WIN32) || defined(__CYGWIN__)
43 #define WIN32_LEAN_AND_MEAN
46 static void *main_fiber;
48 #elif defined(__linux) || defined(__APPLE__)
60 char stack[MTARCH_STACKSIZE];
64 static ucontext_t main_context;
65 static ucontext_t *running_context;
73 #if defined(_WIN32) || defined(__CYGWIN__)
75 main_fiber = ConvertThreadToFiber(
NULL);
83 #if defined(_WIN32) || defined(__CYGWIN__)
85 ConvertFiberToThread();
92 void (*
function)(
void *data),
95 #if defined(_WIN32) || defined(__CYGWIN__)
97 thread->mt_thread = CreateFiber(0, (LPFIBER_START_ROUTINE)
function, data);
99 #elif defined(__linux)
101 thread->mt_thread = malloc(
sizeof(
struct mtarch_t));
103 getcontext(&((
struct mtarch_t *)thread->mt_thread)->context);
105 ((
struct mtarch_t *)thread->mt_thread)->context.uc_link =
NULL;
106 ((
struct mtarch_t *)thread->mt_thread)->context.uc_stack.ss_sp =
107 ((
struct mtarch_t *)thread->mt_thread)->stack;
108 ((
struct mtarch_t *)thread->mt_thread)->context.uc_stack.ss_size =
109 sizeof(((
struct mtarch_t *)thread->mt_thread)->stack);
126 makecontext(&((
struct mtarch_t *)thread->mt_thread)->context,
127 (void (*)(
void))
function, 1, data);
135 #if defined(_WIN32) || defined(__CYGWIN__)
137 SwitchToFiber(main_fiber);
139 #elif defined(__linux)
141 swapcontext(running_context, &main_context);
149 #if defined(_WIN32) || defined(__CYGWIN__)
151 SwitchToFiber(thread->mt_thread);
153 #elif defined(__linux)
155 running_context = &((
struct mtarch_t *)thread->mt_thread)->context;
156 swapcontext(&main_context, running_context);
157 running_context =
NULL;
165 #if defined(_WIN32) || defined(__CYGWIN__)
167 DeleteFiber(thread->mt_thread);
169 #elif defined(linux) || defined(__linux)
171 free(thread->mt_thread);
void mtarch_init(void)
Initialize the architecture specific support functions for the multi-thread library.
void mtarch_yield(void)
Yield the processor.
Header file for the preemptive multitasking library for Contiki.
void mtarch_remove(void)
Uninstall library and clean up.
void mtarch_start(struct mtarch_thread *thread, void(*function)(void *data), void *data)
Setup the stack frame for a thread that is being started.
#define NULL
The null pointer.
void mtarch_exec(struct mtarch_thread *thread)
Start executing a thread.
void mtarch_stop(struct mtarch_thread *thread)
Clean up the stack of a thread.
Copyright (c) 2014, Analog Devices, Inc.