-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathperfc_task_coroutine.c
More file actions
220 lines (184 loc) · 7.01 KB
/
perfc_task_coroutine.c
File metadata and controls
220 lines (184 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/****************************************************************************
* Copyright 2025 Gorgon Meducer (Email:embedded_zhuoran@hotmail.com) *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
****************************************************************************/
/*============================ INCLUDES ======================================*/
#include "__perfc_task_common.h"
#include "perf_counter.h"
#include "cmsis_compiler.h"
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#ifdef __cplusplus
extern "C" {
#endif
/*!
* \addtogroup gLanguageExtensios 4 Language Extensions
* @{
*/
/*============================ MACROS ========================================*/
/*============================ MACROFIED FUNCTIONS ===========================*/
/*============================ TYPES =========================================*/
/*============================ GLOBAL VARIABLES ==============================*/
/*============================ LOCAL VARIABLES ===============================*/
/*============================ PROTOTYPES ====================================*/
/*============================ IMPLEMENTATION ================================*/
__attribute__((noreturn))
__attribute__((noinline))
static
void __perfc_coroutine_loop(
perfc_coroutine_t *ptCoroutine,
perfc_coroutine_task_handler_t *fnHandler)
{
jmp_buf tYieldPoint;
ptCoroutine->ptYieldPoint = &tYieldPoint;
do {
/* set the initial yield point */
if (0 == setjmp(*(jmp_buf *)(ptCoroutine->ptYieldPoint))) {
/* the setup phase, return to exit point */
longjmp(*(jmp_buf *)(ptCoroutine->ptCaller), 1);
}
ptCoroutine->tReturn.nResult = (*fnHandler)(ptCoroutine);
} while(1);
}
__attribute__((noinline))
int perfc_coroutine_init( perfc_coroutine_t *ptTask,
perfc_coroutine_task_handler_t *fnHandler,
void *pStackBase,
size_t tSizeInByte)
{
do {
size_t nJmpbufSize = (sizeof(jmp_buf) + 7) & (~((uintptr_t)0x07));
if ( (NULL == ptTask)
|| (NULL == fnHandler)
|| (NULL == pStackBase)
|| (tSizeInByte < (nJmpbufSize + 17 * sizeof(uint64_t)))) {
break;
}
memset(ptTask, 0, sizeof(*ptTask));
ptTask->pStackBase = pStackBase;
#if __PERFC_STACK_GROWS_UPWARD__
uintptr_t pStackLimit = (uintptr_t)pStackBase + tSizeInByte;
# if !defined(__PERFC_COROUTINE_NO_STACK_CHECK__)
perfc_stack_fill((uintptr_t)pStackBase, pStackLimit);
# endif
uintptr_t pStackTop = (uintptr_t)pStackBase;
pStackTop = (pStackTop + 7) & (~((uintptr_t)0x07));
#else
uintptr_t pStackTop = (uintptr_t)pStackBase + tSizeInByte;
/* force 8bytes alignment */
pStackTop &= (~((uintptr_t)0x07));
pStackTop -= 8;
# if !defined(__PERFC_COROUTINE_NO_STACK_CHECK__)
perfc_stack_fill(pStackTop, (uintptr_t)pStackBase);
# endif
#endif
typedef volatile struct {
perfc_coroutine_t *ptTask;
perfc_coroutine_task_handler_t *fnHandler;
uintptr_t pnTaskStack;
void (*fnLoop)(
perfc_coroutine_t *ptCoroutine,
perfc_coroutine_task_handler_t *fnHandler);
} __local_t;
static __local_t s_tWhiteBoard;
static __local_t * volatile s_ptLocal = &s_tWhiteBoard;
static __local_t * volatile s_ptLocal2 = &s_tWhiteBoard;
#define local (*s_ptLocal)
#define local2 (*s_ptLocal2)
__IRQ_SAFE {
local.pnTaskStack = pStackTop;
local.fnHandler = fnHandler;
local.ptTask = ptTask;
local.fnLoop = &__perfc_coroutine_loop;
jmp_buf tReturnPoint;
ptTask->ptCaller = &tReturnPoint;
if (setjmp(*(jmp_buf *)&tReturnPoint) == 0) {
__perfc_port_set_sp(local.pnTaskStack);
local2.fnLoop(local2.ptTask, local2.fnHandler);
assert(false);
while(1) __NOP();
}
}
return 0;
} while(0);
return -1;
}
__WEAK
__attribute__((noinline))
void perf_coroutine_report_error( perfc_coroutine_t *ptTask,
perfc_coroutine_err_t tError)
{
UNUSED_PARAM(ptTask);
UNUSED_PARAM(tError);
assert(tError == PERFC_CR_ERR_NONE);
while(1) __NOP();
}
size_t perfc_coroutine_stack_remain(perfc_coroutine_t *ptTask)
{
if (NULL == ptTask) {
return 0;
}
#if !defined(__PERFC_COROUTINE_NO_STACK_CHECK__)
return perfc_stack_remain((uintptr_t)ptTask->pStackBase);
#else
return 0;
#endif
}
perfc_coroutine_rt_t perfc_coroutine_call(perfc_coroutine_t *ptTask)
{
if (NULL == ptTask) {
return (perfc_coroutine_rt_t)-1;
}
jmp_buf tReturnPoint;
ptTask->ptCaller = &tReturnPoint;
if (0 == setjmp(tReturnPoint)) {
/* go to the yield point */
longjmp(*(ptTask->ptYieldPoint), 1);
assert(false);
while(1) __NOP(); /* we should not reach here */
}
/* perform stack overflow detection */
#if !defined(__PERFC_COROUTINE_NO_STACK_CHECK__) && !defined(__PERFC_STACK_GROWS_UPWARD__)
do {
uint64_t *pdwCanary = (uint64_t *)
( ((uintptr_t)(ptTask->pStackBase) + 7)
& (~((uintptr_t)0x07)));
if (*pdwCanary != __PERFC_STACK_WATERMARK_U64__) {
/* report stackover flow */
perf_coroutine_report_error(ptTask, PERFC_CR_ERR_STACK_OVERFLOW);
}
} while(0);
#endif
return ptTask->tReturn;
}
void perfc_coroutine_yield(perfc_coroutine_t *ptTask)
{
if (NULL == ptTask) {
return ;
}
if (0 == setjmp(*(ptTask->ptYieldPoint))) {
/* yeild */
assert(NULL != ptTask->ptCaller);
longjmp(*(ptTask->ptCaller), 1);
assert(false);
while(1) __NOP(); /* we should not reach here */
}
/* return from yield */
}
/*! @} */
#ifdef __cplusplus
}
#endif