Contiki-NG
cooja_mt.h
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.h of the Contiki Multi-threading library.
36 * Fredrik Osterlind <fros@sics.se>
37 */
38#ifndef COOJA_MT_H_
39#define COOJA_MT_H_
40
41#include "contiki.h"
42
43#include <stdint.h>
44
45/**
46 * An opaque structure that is used for holding the state of a thread.
47 *
48 * This structure typically holds the entire stack for the thread.
49 */
50struct cooja_mtarch_thread;
51
52/**
53 * Setup the stack frame for a thread that is being started.
54 *
55 * This function is called by the mt_start() function in order to set
56 * up the architecture specific stack of the thread to be started.
57 *
58 * \param thread A pointer to a struct mtarch_thread for the thread to
59 * be started.
60 *
61 * \param function A pointer to the function that the thread will
62 * start executing the first time it is scheduled to run.
63 *
64 * \param data A pointer to the argument that the function should be
65 * passed.
66 */
67void cooja_mtarch_start(struct cooja_mtarch_thread *thread,
68 void (* function)(void *data),
69 void *data);
70
71/**
72 * Yield the processor.
73 *
74 * This function is called by the mt_yield() function, which is called
75 * from the running thread in order to give up the processor.
76 *
77 */
78void cooja_mtarch_yield(void);
79
80/**
81 * Start executing a thread.
82 *
83 * This function is called from mt_exec() and the purpose of the
84 * function is to start execution of the thread. The function should
85 * switch in the stack of the thread, and does not return until the
86 * thread has explicitly yielded (using mt_yield()) or until it is
87 * preempted.
88 *
89 */
90void cooja_mtarch_exec(struct cooja_mtarch_thread *thread);
91
92
93#ifndef COOJA_MTARCH_STACKSIZE
94#define COOJA_MTARCH_STACKSIZE 1024
95#endif /* COOJA_MTARCH_STACKSIZE */
96
97struct cooja_mtarch_thread {
98 uintptr_t sp; /* Note: stack pointer must be first var in struct! */
99 uintptr_t stack[COOJA_MTARCH_STACKSIZE];
100} __attribute__ ((aligned (16)));
101
102struct cooja_mt_thread {
103 int state;
104 struct cooja_mtarch_thread thread;
105};
106
107/**
108 * No error.
109 *
110 * \hideinitializer
111 */
112#define MT_OK 1
113
114/**
115 * Starts a multithreading thread.
116 *
117 * \param thread Pointer to an mt_thread struct that must have been
118 * previously allocated by the caller.
119 *
120 * \param function A pointer to the entry function of the thread that is
121 * to be set up.
122 *
123 * \param data A pointer that will be passed to the entry function.
124 *
125 */
126void cooja_mt_start(struct cooja_mt_thread *thread, void (* function)(void *), void *data);
127
128/**
129 * Execute parts of a thread.
130 *
131 * This function is called by a Contiki process and runs a
132 * thread. The function does not return until the thread has yielded,
133 * or is preempted.
134 *
135 * \note The thread must first be initialized with the mt_init() function.
136 *
137 * \param thread A pointer to a struct mt_thread block that must be
138 * allocated by the caller.
139 *
140 */
141void cooja_mt_exec(struct cooja_mt_thread *thread);
142
143/**
144 * Voluntarily give up the processor.
145 *
146 * This function is called by a running thread in order to give up
147 * control of the CPU.
148 *
149 */
150void cooja_mt_yield(void);
151
152#endif /* MT_H_ */