EVE 1.0
thread.h
Go to the documentation of this file.
1 #ifndef EVE_THREAD_H_INCLUDED
2 #define EVE_THREAD_H_INCLUDED
3 
4 /**********************************************************************/
5 /*
6  * Copyright (c) 2016, Jetro AS
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  * derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
23  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
25  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
29  * OF SUCH DAMAGE.
30  *
31  * This file is part of the EVE platform.
32  */
33 
34  /**
35  * \file
36  * \brief Header file for the thread emulation layer.
37  *
38  * \author DT, Jetro AS
39  */ /******************************************************************/
40 
41 #include <lib/dlist.h>
42 
43 /**
44  * \defgroup thread Thread emulation
45  * \ingroup eve_scheduling
46  * \{
47  *
48  * Thread emulation layer provides RTOS-like thread abstraction on top of
49  * Contiki protothreads. The threads use a common physical call stack,
50  * exactly like the Contiki protothreads do, but the stack content can be
51  * saved in an external buffer at thread yield. The stored stack state
52  * can be restored later, emulating resuming of the thread.
53  * In general it resembles setjmp() / longjmp() semantic, but with addition
54  * to stack snapshoting and "return to jump point" functionality.
55  */
56 
57 /**
58  * \def EVE_THREAD_EMBEDDED_CSTATE
59  * Normally disabled, reduces static RAM usage but increrases dynaamic one
60  * and CPU clocks required.
61  */
62 #define EVE_THREAD_EMBEDDED_CSTATE 0
63 
64 /**
65  * \def EVE_THREAD_FPU_SUPPORT
66  * Normally enabled, allows FPU usage in the treads
67  */
68 #define EVE_THREAD_FPU_SUPPORT 1
69 
70 /**
71  * \def THREAD_CPU_REGISTERS_COUNT
72  * Number of registers stored in the context
73  */
74 #if EVE_THREAD_FPU_SUPPORT != 0
75  #define THREAD_CPU_REGISTERS_COUNT (16 + 9) /* {s16-s31, r4-r11, r15} */
76 #else /* EVE_THREAD_FPU_SUPPORT */
77  #define THREAD_CPU_REGISTERS_COUNT (9) /* {r4-r11, r15} */
78 #endif /* EVE_THREAD_FPU_SUPPORT */
79 
80 #if EVE_THREAD_EMBEDDED_CSTATE != 0
81  #define THREAD_CPU_STATE_REGISTER_COUNT 0
82 #else /* EVE_THREAD_EMBEDDED_CSTATE */
83  #define THREAD_CPU_STATE_REGISTER_COUNT THREAD_CPU_REGISTERS_COUNT
84 #endif /* EVE_THREAD_EMBEDDED_CSTATE */
85 
86 /**
87  * Stack pool state (dynanic)
88  */
90 {
91  struct dlist_t Head; /**< Linked list of allocated stacks */
92  uint32_t Reserved;
93 };
94 
95 /**
96  * Stack pool instance (constant)
97  */
99 {
100  struct stack_pool_state_t *State; /**< Pointer to the state of the stack pool */
101  uint32_t *Arena; /**< Pointer to the memory buffer allocated for the pool */
102  uint32_t ArenaSize; /**< Size of the stack pool memory buffer */
103 };
104 
105 /**
106  * Thread state (dynanic)
107  */
108 struct thread_t
109 {
110  struct dlist_t Link; /**< Linked list entry */
111  uint32_t *Mark; /**< Base stack pointer for the thread */
112  uint32_t *Sp; /**< Stack pointer at ThreadBegin() entry */
113  uint32_t Stack; /**< Offset in the stack pool to the allocated stack snapshot */
114  uint32_t StackSize; /**< Amount of bytes used for the thread in the stack pool */
115  uint32_t StackReserved; /**< Amount of bytes allocated for the thread in the stack pool */
116  const struct stack_pool_t *Pool; /**< Pointer to the thread's stack pool */
117  uint32_t CpuState[THREAD_CPU_STATE_REGISTER_COUNT]; /**< CPU state at ThreadYield() entry */
118  uint32_t BackjumpState[THREAD_CPU_REGISTERS_COUNT]; /**< CPU state at ThreadBegin() entry */
119 };
120 
121 /**********************************************************************/
122 /**
123  * Starts or resumes thread execution
124  *
125  * \param Thread pointer to the thread instance
126  */
127 uint32_t ThreadBegin(struct thread_t *Thread);
128 
129 /**********************************************************************/
130 /**
131  * Yields the thread and returns control to the return of ThreadBegin
132  *
133  * \param Thread pointer to the thread instance
134  * \param Value value to be passed as the return code of ThreadBegin
135  */
136 void ThreadYield(struct thread_t *Thread, uint32_t Value);
137 
138 /**********************************************************************/
139 /**
140  * Ends the thread ans deallocates stack from the stack pool
141  *
142  * \param Thread pointer to the thread instance
143  */
144 void ThreadEnd(struct thread_t *Thread);
145 
146 /** @} */ /* thread */
147 
148 #endif /* EVE_THREAD_H_INCLUDED */
uint32_t ArenaSize
Definition: thread.h:102
uint32_t * Arena
Definition: thread.h:101
const struct stack_pool_t * Pool
Definition: thread.h:116
The code implements Dummy Headed Doubly Linked Circularlist (DHDLC) primitive.
uint32_t ThreadBegin(struct thread_t *Thread)
void ThreadEnd(struct thread_t *Thread)
uint32_t * Sp
Definition: thread.h:112
#define THREAD_CPU_REGISTERS_COUNT
Definition: thread.h:75
struct dlist_t Head
Definition: thread.h:91
uint32_t StackSize
Definition: thread.h:114
uint32_t StackReserved
Definition: thread.h:115
void ThreadYield(struct thread_t *Thread, uint32_t Value)
uint32_t Stack
Definition: thread.h:113
Definition: dlist.h:66
uint32_t * Mark
Definition: thread.h:111
struct stack_pool_state_t * State
Definition: thread.h:100