40#define _DARWIN_C_SOURCE
46#include "sys/cooja_mt.h"
51#define MT_STATE_READY 1
52#define MT_STATE_RUNNING 2
53#define MT_STATE_EXITED 5
55static struct cooja_mt_thread *current;
56static struct cooja_mt_thread *cooja_running_thread;
60#pragma GCC diagnostic push
61#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
66cooja_mt_init(
struct cooja_mt_thread *t)
70 if(COOJA_MTARCH_STACKSIZE < MINSIGSTKSZ) {
76 pthread_t self = pthread_self();
78 stack_addr = pthread_get_stackaddr_np(self);
79 stack_size = pthread_get_stacksize_np(self);
82 if(pthread_getattr_np(self, &attrs) != 0) {
85 if(pthread_attr_getstack(&attrs, &stack_addr, &stack_size) != 0) {
89 if(getcontext(&t->ctxt) == -1) {
90 perror(
"getcontext failed");
93 t->ctxt.uc_stack.ss_sp = stack_addr;
94 t->ctxt.uc_stack.ss_flags = 0;
95 t->ctxt.uc_stack.ss_size = stack_size;
100cooja_mt_start(
struct cooja_mt_thread *caller,
101 struct cooja_mt_thread *t,
void (*function)(
void))
103 if(getcontext(&t->ctxt) == -1) {
104 perror(
"getcontext failed");
107 t->ctxt.uc_stack.ss_sp = t->stack;
108 t->ctxt.uc_stack.ss_flags = 0;
109 t->ctxt.uc_stack.ss_size =
sizeof(t->stack);
110 t->ctxt.uc_link = &caller->ctxt;
111 makecontext(&t->ctxt, function, 0);
112 t->state = MT_STATE_READY;
116cooja_mt_exec(
struct cooja_mt_thread *caller,
struct cooja_mt_thread *thread)
118 if(thread->state == MT_STATE_READY) {
119 thread->state = MT_STATE_RUNNING;
123 cooja_running_thread = thread;
124 if(swapcontext(&caller->ctxt, &thread->ctxt) == -1) {
125 perror(
"swapcontext");
127 cooja_running_thread = NULL;
134 current->state = MT_STATE_READY;
140 if(cooja_running_thread->ctxt.uc_link == NULL) {
143 if(swapcontext(&cooja_running_thread->ctxt,
144 cooja_running_thread->ctxt.uc_link) == -1) {
145 perror(
"swapcontext");
150#pragma GCC diagnostic pop