Skip to content

Commit e95596e

Browse files
committed
Merge pull request esp8266#585 from esp8266/optimistic-yield
Pass timeout to optimistic_yield, add cont_can_yield check
2 parents 636e250 + 3730ef8 commit e95596e

File tree

11 files changed

+193
-177
lines changed

11 files changed

+193
-177
lines changed

hardware/esp8266com/esp8266/cores/esp8266/Arduino.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ extern "C" {
3838
#include "esp8266_peri.h"
3939
#include "twi.h"
4040

41-
void yield(void);
42-
void optimistic_yield(void);
43-
4441
#define HIGH 0x1
4542
#define LOW 0x0
4643

@@ -140,8 +137,8 @@ void timer0_detachInterrupt(void);
140137
void ets_intr_lock();
141138
void ets_intr_unlock();
142139

143-
// level (0-15),
144-
// level 15 will disable ALL interrupts,
140+
// level (0-15),
141+
// level 15 will disable ALL interrupts,
145142
// level 0 will disable most software interrupts
146143
//
147144
#define xt_disable_interrupts(state, level) __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state))
@@ -207,6 +204,9 @@ void detachInterrupt(uint8_t);
207204
void setup(void);
208205
void loop(void);
209206

207+
void yield(void);
208+
void optimistic_yield(uint32_t interval_us);
209+
210210
// Get the bit location within the hardware port of the given virtual pin.
211211
// This comes from the pins_*.c file for the active board configuration.
212212
#define digitalPinToPort(pin) (0)

hardware/esp8266com/esp8266/cores/esp8266/HardwareSerial.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*
1+
/*
22
HardwareSerial.cpp - esp8266 UART support
33
44
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
55
This file is part of the esp8266 core for Arduino environment.
6-
6+
77
This library is free software; you can redistribute it and/or
88
modify it under the terms of the GNU Lesser General Public
99
License as published by the Free Software Foundation; either
@@ -559,7 +559,7 @@ int HardwareSerial::available(void) {
559559
}
560560

561561
if (!result) {
562-
optimistic_yield();
562+
optimistic_yield(USD(_uart->uart_nr) / 128);
563563
}
564564

565565
return result;
Lines changed: 90 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*
1+
/*
22
cont.S - continuations support for Xtensa call0 ABI
33
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
44
This file is part of the esp8266 core for Arduino environment.
5-
5+
66
This library is free software; you can redistribute it and/or
77
modify it under the terms of the GNU Lesser General Public
88
License as published by the Free Software Foundation; either
@@ -18,106 +18,109 @@
1818
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1919
*/
2020

21-
.text
22-
.align 4
23-
.literal_position
24-
.global cont_yield
25-
.type cont_yield, @function
21+
.text
22+
.align 4
23+
.literal_position
24+
.global cont_yield
25+
.type cont_yield, @function
2626
cont_yield:
27-
/* a1: sp */
28-
/* a2: void* cont_ctx */
29-
/* adjust stack and save registers */
30-
addi a1, a1, -24
31-
s32i a12, a1, 0
32-
s32i a13, a1, 4
33-
s32i a14, a1, 8
34-
s32i a15, a1, 12
35-
s32i a0, a1, 16
36-
s32i a2, a1, 20
27+
/* a1: sp */
28+
/* a2: void* cont_ctx */
29+
/* adjust stack and save registers */
30+
addi a1, a1, -24
31+
s32i a12, a1, 0
32+
s32i a13, a1, 4
33+
s32i a14, a1, 8
34+
s32i a15, a1, 12
35+
s32i a0, a1, 16
36+
s32i a2, a1, 20
3737

38-
/* &cont_continue -> cont_ctx.pc_yield */
39-
movi a3, cont_continue
40-
s32i a3, a2, 8
41-
/* sp -> cont_ctx.sp_yield */
42-
s32i a1, a2, 12
38+
/* &cont_continue -> cont_ctx.pc_yield */
39+
movi a3, cont_continue
40+
s32i a3, a2, 8
41+
/* sp -> cont_ctx.sp_yield */
42+
s32i a1, a2, 12
4343

44-
/* a0 <- cont_ctx.pc_ret */
45-
l32i a0, a2, 0
46-
/* sp <- cont_ctx.sp_ret */
47-
l32i a1, a2, 4
48-
jx a0
44+
/* a0 <- cont_ctx.pc_ret */
45+
l32i a0, a2, 0
46+
/* sp <- cont_ctx.sp_ret */
47+
l32i a1, a2, 4
48+
jx a0
4949

5050
cont_continue:
51-
l32i a12, a1, 0
52-
l32i a13, a1, 4
53-
l32i a14, a1, 8
54-
l32i a15, a1, 12
55-
l32i a0, a1, 16
56-
l32i a2, a1, 20
57-
addi a1, a1, 24
58-
ret
59-
.size cont_yield, . - cont_yield
51+
l32i a12, a1, 0
52+
l32i a13, a1, 4
53+
l32i a14, a1, 8
54+
l32i a15, a1, 12
55+
l32i a0, a1, 16
56+
l32i a2, a1, 20
57+
addi a1, a1, 24
58+
ret
59+
.size cont_yield, . - cont_yield
6060

6161
////////////////////////////////////////////////////
6262

63-
.text
64-
.align 4
65-
.literal_position
66-
.global cont_run
67-
.type cont_run, @function
63+
.text
64+
.align 4
65+
.literal_position
66+
.global cont_run
67+
.type cont_run, @function
6868
cont_run:
69-
/* a1: sp */
70-
/* a2: void* cont_ctx */
71-
/* a3: void (*pfn) */
69+
/* a1: sp */
70+
/* a2: void* cont_ctx */
71+
/* a3: void (*pfn) */
7272

73-
/* adjust stack and save registers */
74-
addi a1, a1, -20
75-
s32i a12, a1, 0
76-
s32i a13, a1, 4
77-
s32i a14, a1, 8
78-
s32i a15, a1, 12
79-
s32i a0, a1, 16
73+
/* adjust stack and save registers */
74+
addi a1, a1, -20
75+
s32i a12, a1, 0
76+
s32i a13, a1, 4
77+
s32i a14, a1, 8
78+
s32i a15, a1, 12
79+
s32i a0, a1, 16
8080

81-
/* cont_ret -> a4 -> cont_ctx.pc_ret*/
82-
movi a4, cont_ret
83-
s32i a4, a2, 0
84-
/* sp -> cont_ctx.sp_ret */
85-
s32i a1, a2, 4
81+
/* cont_ret -> a4 -> cont_ctx.pc_ret*/
82+
movi a4, cont_ret
83+
s32i a4, a2, 0
84+
/* sp -> cont_ctx.sp_ret */
85+
s32i a1, a2, 4
8686

87-
/* if cont_ctx.pc_yield != 0, goto cont_resume */
88-
l32i a4, a2, 8
89-
bnez a4, cont_resume
90-
/* else */
91-
/* set new stack*/
92-
l32i a1, a2, 16;
93-
/* goto pfn */
94-
movi a0, cont_norm
95-
jx a3
87+
/* if cont_ctx.pc_yield != 0, goto cont_resume */
88+
l32i a4, a2, 8
89+
bnez a4, cont_resume
90+
/* else */
91+
/* set new stack*/
92+
l32i a1, a2, 16;
93+
/* goto pfn */
94+
movi a0, cont_norm
95+
jx a3
9696

9797
cont_resume:
98-
/* a1 <- cont_ctx.sp_yield */
99-
l32i a1, a2, 12
100-
/* reset yield flag, 0 -> cont_ctx.pc_yield */
101-
movi a3, 0
102-
s32i a3, a2, 8
103-
/* jump to saved cont_ctx.pc_yield */
104-
movi a0, cont_ret
105-
jx a4
98+
/* a1 <- cont_ctx.sp_yield */
99+
l32i a1, a2, 12
100+
/* reset yield flag, 0 -> cont_ctx.pc_yield */
101+
movi a3, 0
102+
s32i a3, a2, 8
103+
/* jump to saved cont_ctx.pc_yield */
104+
movi a0, cont_ret
105+
jx a4
106106

107107
cont_norm:
108-
/* calculate pointer to cont_ctx.struct_start from sp */
109-
l32i a2, a1, 4
110-
/* sp <- cont_ctx.sp_ret */
111-
l32i a1, a2, 4
108+
/* calculate pointer to cont_ctx.struct_start from sp */
109+
l32i a2, a1, 4
110+
/* sp <- cont_ctx.sp_ret */
111+
l32i a1, a2, 4
112+
/* 0 -> cont_ctx.pc_ret */
113+
movi a4, 0
114+
s32i a4, a2, 0
112115

113116
cont_ret:
114-
/* restore registers */
115-
l32i a12, a1, 0
116-
l32i a13, a1, 4
117-
l32i a14, a1, 8
118-
l32i a15, a1, 12
119-
l32i a0, a1, 16
120-
/* adjust stack and return */
121-
addi a1, a1, 20
122-
ret
123-
.size cont_run, . - cont_run
117+
/* restore registers */
118+
l32i a12, a1, 0
119+
l32i a13, a1, 4
120+
l32i a14, a1, 8
121+
l32i a15, a1, 12
122+
l32i a0, a1, 16
123+
/* adjust stack and return */
124+
addi a1, a1, 20
125+
ret
126+
.size cont_run, . - cont_run

hardware/esp8266com/esp8266/cores/esp8266/cont.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*
1+
/*
22
cont.h - continuations support for Xtensa call0 ABI
33
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
44
This file is part of the esp8266 core for Arduino environment.
5-
5+
66
This library is free software; you can redistribute it and/or
77
modify it under the terms of the GNU Lesser General Public
88
License as published by the Free Software Foundation; either
@@ -21,6 +21,8 @@
2121
#ifndef CONT_H_
2222
#define CONT_H_
2323

24+
#include <stdbool.h>
25+
2426
#ifndef CONT_STACKSIZE
2527
#define CONT_STACKSIZE 4096
2628
#endif
@@ -58,4 +60,8 @@ void cont_yield(cont_t*);
5860
// return 1 if guard bytes were overwritten.
5961
int cont_check(cont_t* cont);
6062

63+
// Check if yield() may be called. Returns true if we are running inside
64+
// continuation stack
65+
bool cont_can_yield(cont_t* cont);
66+
6167
#endif /* CONT_H_ */

hardware/esp8266com/esp8266/cores/esp8266/cont_util.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*
1+
/*
22
cont_util.s - continuations support for Xtensa call0 ABI
33
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
44
This file is part of the esp8266 core for Arduino environment.
5-
5+
66
This library is free software; you can redistribute it and/or
77
modify it under the terms of the GNU Lesser General Public
88
License as published by the Free Software Foundation; either
@@ -19,6 +19,9 @@
1919
*/
2020

2121
#include "cont.h"
22+
#include <stddef.h>
23+
#include "ets_sys.h"
24+
2225

2326
#define CONT_STACKGUARD 0xfeefeffe
2427

@@ -34,3 +37,8 @@ int cont_check(cont_t* cont) {
3437

3538
return 0;
3639
}
40+
41+
bool cont_can_yield(cont_t* cont) {
42+
return !ETS_INTR_WITHINISR() &&
43+
cont->pc_ret != 0 && cont->pc_yield == 0;
44+
}

0 commit comments

Comments
 (0)