@@ -37,7 +37,6 @@ extern "C" {
37
37
38
38
#define LOOP_TASK_PRIORITY 1
39
39
#define LOOP_QUEUE_SIZE 1
40
- #define OPTIMISTIC_YIELD_TIME_US 16000
41
40
42
41
extern " C" void call_user_start ();
43
42
extern void loop ();
@@ -58,7 +57,7 @@ cont_t* g_pcont __attribute__((section(".noinit")));
58
57
static os_event_t s_loop_queue[LOOP_QUEUE_SIZE];
59
58
60
59
/* Used to implement optimistic_yield */
61
- static uint32_t s_micros_at_task_start ;
60
+ static uint32_t s_cycles_at_yield_start ;
62
61
63
62
/* For ets_intr_lock_nest / ets_intr_unlock_nest
64
63
* Max nesting seen by SDK so far is 2.
@@ -97,6 +96,7 @@ extern "C" bool can_yield() {
97
96
static inline void esp_yield_within_cont () __attribute__((always_inline));
98
97
static void esp_yield_within_cont () {
99
98
cont_yield (g_pcont);
99
+ s_cycles_at_yield_start = ESP.getCycleCount ();
100
100
run_scheduled_recurrent_functions ();
101
101
}
102
102
@@ -125,14 +125,19 @@ extern "C" void __yield() {
125
125
extern " C" void yield (void ) __attribute__ ((weak, alias(" __yield" )));
126
126
127
127
extern " C" void optimistic_yield (uint32_t interval_us) {
128
- if (can_yield () &&
129
- (system_get_time () - s_micros_at_task_start) > interval_us)
128
+ const uint32_t intvl_cycles = interval_us *
129
+ #if defined(F_CPU)
130
+ clockCyclesPerMicrosecond ();
131
+ #else
132
+ ESP.getCpuFreqMHz ();
133
+ #endif
134
+ if ((ESP.getCycleCount () - s_cycles_at_yield_start) > intvl_cycles &&
135
+ can_yield ())
130
136
{
131
137
yield ();
132
138
}
133
139
}
134
140
135
-
136
141
// Replace ets_intr_(un)lock with nestable versions
137
142
extern " C" void IRAM_ATTR ets_intr_lock () {
138
143
if (ets_intr_lock_stack_ptr < ETS_INTR_LOCK_NEST_MAX)
@@ -183,7 +188,7 @@ static void loop_wrapper() {
183
188
184
189
static void loop_task (os_event_t *events) {
185
190
(void ) events;
186
- s_micros_at_task_start = system_get_time ();
191
+ s_cycles_at_yield_start = ESP. getCycleCount ();
187
192
cont_run (g_pcont, &loop_wrapper);
188
193
if (cont_check (g_pcont) != 0 ) {
189
194
panic ();
@@ -211,7 +216,7 @@ extern void __unhandled_exception(const char *str);
211
216
static void __unhandled_exception_cpp ()
212
217
{
213
218
#ifndef __EXCEPTIONS
214
- abort ();
219
+ abort ();
215
220
#else
216
221
static bool terminating;
217
222
if (terminating)
0 commit comments