Contiki-NG
Loading...
Searching...
No Matches
cooja_mt.c
1/*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 * Author: Adam Dunkels <adam@sics.se>
32 *
33 */
34/*
35 * This file is ripped from mt.c of the Contiki Multi-threading library.
36 * Fredrik Osterlind <fros@sics.se>
37 */
38
39#ifdef __APPLE__
40#define _DARWIN_C_SOURCE /* For pthread_get_stackaddr_np() */
41#else
42#define _GNU_SOURCE /* For pthread_getattr_np(). */
43#endif
44#include <pthread.h>
45
46#include "sys/cooja_mt.h"
47
48#include <signal.h>
49#include <stdio.h>
50
51#define MT_STATE_READY 1
52#define MT_STATE_RUNNING 2
53#define MT_STATE_EXITED 5
54
55static struct cooja_mt_thread *current;
56static struct cooja_mt_thread *cooja_running_thread;
57
58#ifdef __APPLE__
59/* Avoid deprecated warnings for ucontext.h functions on OS X. */
60#pragma GCC diagnostic push
61#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
62#endif
63
64/*--------------------------------------------------------------------------*/
65int
66cooja_mt_init(struct cooja_mt_thread *t)
67{
68#ifdef __APPLE__
69 /* Apple's makecontext() fails if size is less than MINSIGSTKSZ. */
70 if(COOJA_MTARCH_STACKSIZE < MINSIGSTKSZ) {
71 return -1;
72 }
73#endif
74 void *stack_addr;
75 size_t stack_size;
76 pthread_t self = pthread_self();
77#ifdef __APPLE__
78 stack_addr = pthread_get_stackaddr_np(self);
79 stack_size = pthread_get_stacksize_np(self);
80#else
81 pthread_attr_t attrs;
82 if(pthread_getattr_np(self, &attrs) != 0) {
83 return -2;
84 }
85 if(pthread_attr_getstack(&attrs, &stack_addr, &stack_size) != 0) {
86 return -3;
87 }
88#endif
89 if(getcontext(&t->ctxt) == -1) {
90 perror("getcontext failed");
91 return -4;
92 }
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;
96 return 0;
97}
98/*--------------------------------------------------------------------------*/
99void
100cooja_mt_start(struct cooja_mt_thread *caller,
101 struct cooja_mt_thread *t, void (*function)(void))
102{
103 if(getcontext(&t->ctxt) == -1) {
104 perror("getcontext failed");
105 return;
106 }
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;
113}
114/*--------------------------------------------------------------------------*/
115void
116cooja_mt_exec(struct cooja_mt_thread *caller, struct cooja_mt_thread *thread)
117{
118 if(thread->state == MT_STATE_READY) {
119 thread->state = MT_STATE_RUNNING;
120 current = thread;
121 /* Switch context to the thread. The function call will not return
122 until the the thread has yielded, or is preempted. */
123 cooja_running_thread = thread;
124 if(swapcontext(&caller->ctxt, &thread->ctxt) == -1) {
125 perror("swapcontext");
126 }
127 cooja_running_thread = NULL;
128 }
129}
130/*--------------------------------------------------------------------------*/
131void
132cooja_mt_yield(void)
133{
134 current->state = MT_STATE_READY;
135 current = NULL;
136 /* This function is called from the running thread, and we call the
137 switch function in order to switch the thread to the main Contiki
138 program instead. For us, the switch function will not return
139 until the next time we are scheduled to run. */
140 if(cooja_running_thread->ctxt.uc_link == NULL) {
141 return;
142 }
143 if(swapcontext(&cooja_running_thread->ctxt,
144 cooja_running_thread->ctxt.uc_link) == -1) {
145 perror("swapcontext");
146 }
147}
148
149#ifdef __APPLE__
150#pragma GCC diagnostic pop
151#endif