VSF Documented
vsf_eda_sync.c
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18
19/*============================ INCLUDES ======================================*/
20
22
23#if VSF_USE_KERNEL == ENABLED && defined(__EDA_GADGET__)
24
25/*============================ MACROS ========================================*/
26/*============================ MACROFIED FUNCTIONS ===========================*/
27/*============================ TYPES =========================================*/
28/*============================ GLOBAL VARIABLES ==============================*/
29/*============================ LOCAL VARIABLES ===============================*/
30/*============================ PROTOTYPES ====================================*/
31
32VSF_CAL_SECTION(".text.vsf.kernel.__vsf_teda_cancel_timer")
34
35/*============================ IMPLEMENTATION ================================*/
36
37
38#if VSF_KERNEL_CFG_SUPPORT_SYNC == ENABLED
39
40/*-----------------------------------------------------------------------------*
41 * vsf_sync_t for critical_section, semaphore, event *
42 *-----------------------------------------------------------------------------*/
43
44VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
45vsf_eda_t * __vsf_eda_set_timeout(vsf_eda_t *eda, vsf_timeout_tick_t timeout)
46{
47 eda = __vsf_eda_get_valid_eda(eda);
48
49 if (timeout > 0) {
50#if VSF_KERNEL_CFG_EDA_SUPPORT_TIMER == ENABLED
51 eda->flag.state.is_limitted = true;
53#else
54 #ifndef WEAK_VSF_KERNEL_ERR_REPORT
56 #else
57 WEAK_VSF_KERNEL_ERR_REPORT(VSF_KERNEL_ERR_EDA_DOES_NOT_SUPPORT_TIMER);
58 #endif
59#endif
60 }
61 return eda;
62}
63
64VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
66{
67 eda = __vsf_eda_get_valid_eda(eda);
68 VSF_KERNEL_ASSERT(eda != NULL);
69
71 vsf_eda_t, pending_node,
72 &sync->pending_list,
73 eda);
74
75 __vsf_eda_set_timeout(eda, timeout);
76}
77
78VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
79static vsf_eda_t *__vsf_eda_sync_get_eda_pending(vsf_sync_t *sync)
80{
81 vsf_eda_t *eda;
82
84 vsf_eda_t, pending_node,
85 &sync->pending_list,
86 eda);
87 return eda;
88}
89
90#if __IS_COMPILER_LLVM__ || __IS_COMPILER_ARM_COMPILER_6__
91# pragma clang diagnostic push
92# pragma clang diagnostic ignored "-Wcast-align"
93#endif
94
95#if __IS_COMPILER_GCC__
96# pragma GCC diagnostic push
97# pragma GCC diagnostic ignored "-Wcast-align"
98#endif
99
100VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
101static vsf_sync_reason_t __vsf_eda_sync_get_reason( vsf_sync_t *sync,
102 vsf_evt_t evt,
103 bool dequeue_eda)
104{
105 vsf_eda_t *eda = vsf_eda_get_cur();
106 VSF_KERNEL_ASSERT(eda != NULL);
108
109 VSF_KERNEL_ASSERT((sync != NULL) && (eda != NULL));
110
111#if VSF_KERNEL_CFG_EDA_SUPPORT_TIMER == ENABLED
112 if (evt == VSF_EVT_TIMER) {
113 vsf_protect_t origlevel = vsf_protect_sched();
114 if (eda->flag.state.is_sync_got) {
115 vsf_unprotect_sched(origlevel);
116 return VSF_SYNC_PENDING;
117 }
118 if (dequeue_eda) {
120 vsf_eda_t, pending_node,
121 &sync->pending_list,
122 eda);
123 }
124 vsf_unprotect_sched(origlevel);
125 reason = VSF_SYNC_TIMEOUT;
126 } else {
128#else
129 {
130#endif
131 if (evt == VSF_EVT_SYNC) {
132 reason = VSF_SYNC_GET;
133 eda->flag.state.is_sync_got = false;
134 } else if (evt == VSF_EVT_SYNC_CANCEL) {
135 reason = VSF_SYNC_CANCEL;
136 } else {
137 VSF_KERNEL_ASSERT(false);
138 }
139 }
140 eda->flag.state.is_limitted = false;
141 return reason;
142}
143
144#if __IS_COMPILER_LLVM__ || __IS_COMPILER_ARM_COMPILER_6__
145# pragma clang diagnostic pop
146#endif
147#if __IS_COMPILER_GCC__
148# pragma GCC diagnostic pop
149#endif
150
151VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
153{
154 VSF_KERNEL_ASSERT(pthis != NULL);
155
156 pthis->cur_union.cur_value = cur;
157 pthis->max_union.max_value = max;
158 vsf_dlist_init(&pthis->pending_list);
159 if (pthis->cur_union.bits.has_owner) {
160 ((vsf_sync_owner_t *)pthis)->eda_owner = NULL;
161 }
162 return VSF_ERR_NONE;
163}
164
165#if VSF_SYNC_CFG_SUPPORT_ISR == ENABLED
166VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
168{
169 VSF_KERNEL_ASSERT(pthis != NULL);
170 return vsf_eda_post_msg((vsf_eda_t *)&__vsf_eda.task, pthis);
171}
172#endif // VSF_SYNC_CFG_SUPPORT_ISR
173
174VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
176{
177 vsf_protect_t origlevel;
178 vsf_eda_t *eda_pending;
179
180 VSF_KERNEL_ASSERT(pthis != NULL);
181
182 origlevel = vsf_protect_sched();
183 if (pthis->cur_union.bits.cur >= pthis->max_union.bits.max) {
184 vsf_unprotect_sched(origlevel);
185 return VSF_ERR_OVERRUN;
186 }
187 pthis->cur_union.bits.cur++;
188#if VSF_KERNEL_CFG_SUPPORT_DYNAMIC_PRIOTIRY == ENABLED
189 if (pthis->cur_union.bits.has_owner) {
190 eda = __vsf_eda_get_valid_eda(eda);
191 VSF_KERNEL_ASSERT(((vsf_sync_owner_t *)pthis)->eda_owner == eda);
192 ((vsf_sync_owner_t *)pthis)->eda_owner = NULL;
193 if (eda->cur_priority != eda->priority) {
194 __vsf_eda_set_priority(eda, (vsf_prio_t)eda->priority);
195 } else if (eda->flag.state.is_new_prio) {
196 vsf_evtq_t *evtq;
197 // assert new_priority is a priority boost
198 VSF_KERNEL_ASSERT(eda->new_priority > eda->priority);
199 eda->flag.state.is_new_prio = false;
200
201 evtq = __vsf_os_evtq_get((vsf_prio_t)eda->priority);
202 VSF_KERNEL_ASSERT(evtq != NULL);
203 __vsf_os_evtq_set_priority(evtq, eda->priority);
204 }
205 }
206#endif
207
208 while (1) {
209 if (pthis->cur_union.bits.cur > 0) {
210 eda_pending = __vsf_eda_sync_get_eda_pending(pthis);
211 if (eda_pending != NULL) {
212 eda_pending->flag.state.is_sync_got = true;
213 if (!manual) {
214 pthis->cur_union.bits.cur--;
215 }
216#if VSF_KERNEL_CFG_SUPPORT_DYNAMIC_PRIOTIRY == ENABLED
217 if (pthis->cur_union.bits.has_owner) {
218 ((vsf_sync_owner_t *)pthis)->eda_owner = eda_pending;
219 }
220#endif
221 }
222 vsf_unprotect_sched(origlevel);
223
224 if (eda_pending != NULL) {
225 vsf_err_t err = __vsf_eda_post_evt_ex(eda_pending, VSF_EVT_SYNC, true);
226 VSF_KERNEL_ASSERT(!err);
227 VSF_UNUSED_PARAM(err);
228 origlevel = vsf_protect_sched();
229 continue;
230 } else {
231 break;
232 }
233 } else {
234 vsf_unprotect_sched(origlevel);
235 break;
236 }
237 }
238 return VSF_ERR_NONE;
239}
240
241VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
243{
244 return __vsf_eda_sync_increase_ex(pthis, eda, pthis->max_union.bits.manual_rst);
245}
246
247VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
249{
250 return vsf_eda_sync_increase_ex(pthis, NULL);
251}
252
253VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
255{
256 vsf_protect_t origlevel = vsf_protect_sched();
257 pthis->cur_union.bits.cur = 0;
258 vsf_unprotect_sched(origlevel);
259}
260
261VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
263{
264 vsf_protect_t origlevel;
265
266 VSF_KERNEL_ASSERT(pthis != NULL);
267
268 eda = __vsf_eda_get_valid_eda(eda);
269
270 origlevel = vsf_protect_sched();
271 if ((pthis->cur_union.bits.cur > 0) && vsf_dlist_is_empty(&pthis->pending_list)) {
272 if (!manual) {
273 pthis->cur_union.bits.cur--;
274 }
275#if VSF_KERNEL_CFG_SUPPORT_DYNAMIC_PRIOTIRY == ENABLED
276 if (pthis->cur_union.bits.has_owner) {
277 VSF_KERNEL_ASSERT((NULL == ((vsf_sync_owner_t *)pthis)->eda_owner) && (0 == pthis->cur_union.bits.cur));
278 ((vsf_sync_owner_t *)pthis)->eda_owner = eda;
279 }
280#endif
281 vsf_unprotect_sched(origlevel);
282 return VSF_ERR_NONE;
283 }
284
285 if (timeout != 0) {
286#if VSF_KERNEL_CFG_SUPPORT_DYNAMIC_PRIOTIRY == ENABLED
287 if (pthis->cur_union.bits.has_owner) {
289 vsf_prio_t cur_priority = __vsf_eda_get_cur_priority(eda);
291 vsf_eda_t, pending_node,
292 &pthis->pending_list,
293 eda,
294 _->cur_priority < cur_priority);
295 __vsf_eda_set_timeout(eda, timeout);
296 } else
297#endif
298 {
299 __vsf_eda_sync_pend(pthis, eda, timeout);
300 }
301#if VSF_KERNEL_CFG_SUPPORT_DYNAMIC_PRIOTIRY == ENABLED
302 if (pthis->cur_union.bits.has_owner) {
303 vsf_eda_t *eda_owner = ((vsf_sync_owner_t *)pthis)->eda_owner;
304
305 if (eda->cur_priority > eda_owner->cur_priority) {
306 __vsf_eda_set_priority(eda_owner, (vsf_prio_t)eda->cur_priority);
307 }
308 }
309#endif
310 }
311 vsf_unprotect_sched(origlevel);
312 return VSF_ERR_NOT_READY;
313}
314
315VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
317{
318 return __vsf_eda_sync_decrease_ex(pthis, timeout, eda, pthis->max_union.bits.manual_rst);
319}
320
321VSF_CAL_SECTION(".text.vsf.kernel.vsf_sync")
323{
324 return vsf_eda_sync_decrease_ex(pthis, timeout, NULL);
325}
326
327VSF_CAL_SECTION(".text.vsf.kernel.vsf_eda_sync_cancel")
329{
330 vsf_eda_t *eda;
331 vsf_protect_t origlevel;
332
333 VSF_KERNEL_ASSERT(pthis != NULL);
334
335 do {
336 origlevel = vsf_protect_sched();
337 eda = __vsf_eda_sync_get_eda_pending(pthis);
338 if (eda != NULL) {
339 eda->flag.state.is_sync_got = true;
340 vsf_unprotect_sched(origlevel);
342 } else {
343 vsf_unprotect_sched(origlevel);
344 }
345 } while (eda != NULL);
346}
347
348VSF_CAL_SECTION(".text.vsf.kernel.vsf_eda_sync_get_reason")
350{
351 return __vsf_eda_sync_get_reason(pthis, evt, true);
352}
353
354
355#endif
356
357
358#endif
#define VSF_CAL_SECTION(__SEC)
Definition __compiler.h:181
#define VSF_UNUSED_PARAM(__VAL)
Definition __type.h:190
vsf_err_t
Definition __type.h:42
@ VSF_ERR_NONE
none error
Definition __type.h:44
@ VSF_ERR_NOT_READY
service not ready yet
Definition __type.h:43
@ VSF_ERR_OVERRUN
overrun
Definition __type.h:60
Definition vsf_eda.h:766
Definition vsf_eda.h:892
Definition vsf_eda.h:864
Definition vsf_eda.h:834
__le16 timeout
Definition ieee80211.h:94
#define NULL
Definition stddef.h:52
#define max(x, y)
Definition minmax.h:12
unsigned short uint_fast16_t
Definition stdint.h:25
Definition vsf_evtq.h:51
void sync(void)
Definition vsf_linux_fs.c:2146
uintalu_t vsf_protect_t
Definition vsf_arch_abstraction.h:53
vsf_eda_t * vsf_eda_get_cur(void)
Definition vsf_eda.c:410
vsf_err_t __vsf_os_evtq_set_priority(vsf_evtq_t *pthis, vsf_prio_t priority)
Definition vsf_os.c:342
vsf_err_t vsf_eda_post_msg(vsf_eda_t *pthis, void *msg)
Definition vsf_eda.c:949
vsf_err_t __vsf_eda_post_evt_ex(vsf_eda_t *pthis, vsf_evt_t evt, bool force)
Definition vsf_eda.c:941
void vsf_kernel_err_report(enum vsf_kernel_error_t err)
Definition vsf_eda.c:403
int16_t vsf_evt_t
Definition vsf_eda.h:654
vsf_err_t vsf_eda_sync_init(vsf_sync_t *pthis, uint_fast16_t cur_value, uint_fast16_t max_value)
vsf_sync_reason_t vsf_eda_sync_get_reason(vsf_sync_t *pthis, vsf_evt_t evt)
vsf_err_t vsf_eda_sync_increase(vsf_sync_t *pthis)
@ VSF_KERNEL_ERR_EDA_DOES_NOT_SUPPORT_TIMER
Definition vsf_eda.h:1087
@ VSF_EVT_SYNC_CANCEL
Definition vsf_eda.h:616
@ VSF_EVT_SYNC
Definition vsf_eda.h:615
@ VSF_EVT_TIMER
Definition vsf_eda.h:612
vsf_err_t vsf_eda_sync_decrease(vsf_sync_t *pthis, vsf_timeout_tick_t timeout)
VSF_KERNEL_TIMEOUT_TICK_T vsf_timeout_tick_t
Definition vsf_eda.h:592
vsf_err_t vsf_teda_set_timer_ex(vsf_teda_t *pthis, vsf_systimer_tick_t tick)
vsf_err_t __vsf_eda_sync_decrease_ex(vsf_sync_t *pthis, vsf_timeout_tick_t timeout, vsf_eda_t *eda, bool manual)
void vsf_eda_sync_force_reset(vsf_sync_t *pthis)
vsf_err_t __vsf_eda_sync_increase_ex(vsf_sync_t *pthis, vsf_eda_t *eda, bool manual)
vsf_err_t vsf_eda_sync_increase_isr(vsf_sync_t *pthis)
vsf_err_t vsf_eda_sync_decrease_ex(vsf_sync_t *pthis, vsf_timeout_tick_t timeout, vsf_eda_t *eda)
vsf_err_t __vsf_teda_cancel_timer(vsf_teda_t *pthis)
vsf_sync_reason_t
Definition vsf_eda.h:1053
@ VSF_SYNC_GET
Definition vsf_eda.h:1057
@ VSF_SYNC_FAIL
Definition vsf_eda.h:1054
@ VSF_SYNC_CANCEL
Definition vsf_eda.h:1058
@ VSF_SYNC_TIMEOUT
Definition vsf_eda.h:1055
@ VSF_SYNC_PENDING
Definition vsf_eda.h:1056
vsf_err_t vsf_eda_sync_increase_ex(vsf_sync_t *pthis, vsf_eda_t *eda)
void vsf_eda_sync_cancel(vsf_sync_t *pthis)
vsf_prio_t __vsf_eda_get_cur_priority(vsf_eda_t *pthis)
Definition vsf_evtq_list.c:75
vsf_err_t __vsf_eda_set_priority(vsf_eda_t *pthis, vsf_prio_t priority)
Definition vsf_evtq_list.c:82
#define VSF_KERNEL_ASSERT
Definition vsf_kernel_cfg.h:32
vsf_prio_t
Definition vsf_kernel_common.h:37
void __vsf_eda_sync_pend(vsf_sync_t *sync, vsf_eda_t *eda, vsf_timeout_tick_t timeout)
#define vsf_dlist_insert( __host_type, __member, __list_ptr, __item_ptr, __cond)
Definition vsf_list.h:769
#define vsf_dlist_queue_dequeue(__host_type, __member, __list_ptr, __item_ref_ptr)
Definition vsf_list.h:859
#define vsf_dlist_remove( __host_type, __member, __list_ptr, __item_ptr)
Definition vsf_list.h:810
#define vsf_dlist_is_empty(__list_ptr)
Definition vsf_list.h:705
#define vsf_dlist_init(__list_ptr)
Definition vsf_list.h:702
#define vsf_dlist_queue_enqueue(__host_type, __member, __list_ptr, __item_ptr)
Definition vsf_list.h:852
struct @733::@738 _
#define vsf_protect_sched()
Definition vsf_os.h:88
#define vsf_unprotect_sched(__prot)
Definition vsf_os.h:89
vsf_evtq_t * __vsf_os_evtq_get(vsf_prio_t priority)
Definition vsf_os.c:325