Contiki-NG
Loading...
Searching...
No Matches
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#ifndef _XOPEN_SOURCE
42/* Enable POSIX.1-2001, required on OS X. */
43#define _XOPEN_SOURCE 600
44#endif
45#include <ucontext.h>
46
47#ifndef COOJA_MTARCH_STACKSIZE
48#ifdef __APPLE__
49#define COOJA_MTARCH_STACKSIZE 32768
50#else
51#define COOJA_MTARCH_STACKSIZE 8192
52#endif
53#endif /* COOJA_MTARCH_STACKSIZE */
54
55struct cooja_mt_thread {
56 char stack[COOJA_MTARCH_STACKSIZE];
57 ucontext_t ctxt;
58 int state;
59};
60
61/**
62 * Inintializes the main thread structure.
63 *
64 * \param thread Pointer to the (implicit) main thread.
65 */
66int cooja_mt_init(struct cooja_mt_thread *thread);
67
68/**
69 * Starts a multithreading thread.
70 *
71 * \param caller Pointer to a struct for the calling thread.
72 *
73 * \param thread Pointer to an mt_thread struct that must have been
74 * previously allocated by the caller.
75 *
76 * \param function A pointer to the entry function of the thread that is
77 * to be set up.
78 */
79void cooja_mt_start(struct cooja_mt_thread *caller,
80 struct cooja_mt_thread *thread, void (*function)(void));
81
82/**
83 * Execute parts of a thread.
84 *
85 * This function is called by a Contiki process and runs a
86 * thread. The function does not return until the thread has yielded,
87 * or is preempted.
88 *
89 * \note The thread must first be initialized with the cooja_mt_start() function.
90 *
91 * \param caller Pointer to a struct for the calling thread.
92 *
93 * \param thread A pointer to a struct mt_thread block that must be
94 * allocated by the caller.
95 *
96 */
97void cooja_mt_exec(struct cooja_mt_thread *caller,
98 struct cooja_mt_thread *thread);
99
100/**
101 * Voluntarily give up the processor.
102 *
103 * This function is called by a running thread in order to give up
104 * control of the CPU.
105 *
106 */
107void cooja_mt_yield(void);
108
109#endif /* MT_H_ */