Skip to content

Commit af4ed0c

Browse files
Merge pull request #3 from earlephilhower/yieldfatsearch
Add a yield every 5000 clusters searched by FatVolume
2 parents 67cf01b + 603a736 commit af4ed0c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/FatLib/FatVolume.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,15 @@ bool FatVolume::allocateCluster(uint32_t current, uint32_t* next) {
8484
find = m_allocSearchStart;
8585
setStart = true;
8686
}
87+
// Following loop may iterate over up to 268 million clusters, so we need to allow
88+
// the OS/hardware to do its work occasionally during the search or a WDT error will
89+
// occur on the ESP8266.
90+
int yieldCnt = 5000;
8791
while (1) {
92+
if (!(--yieldCnt)) {
93+
yieldCnt = 5000;
94+
SysCall::yield();
95+
}
8896
find++;
8997
if (find > m_lastCluster) {
9098
if (setStart) {
@@ -153,7 +161,15 @@ bool FatVolume::allocContiguous(uint32_t count,
153161
}
154162
endCluster = bgnCluster;
155163
// search the FAT for free clusters
164+
// Following loop may iterate over up to 268 million clusters, so we need to allow
165+
// the OS/hardware to do its work occasionally during the search or a WDT error will
166+
// occur on the ESP8266.
167+
int yieldCnt = 5000;
156168
while (1) {
169+
if (!(--yieldCnt)) {
170+
yieldCnt = 5000;
171+
SysCall::yield();
172+
}
157173
if (endCluster > m_lastCluster) {
158174
// Can't find space.
159175
DBG_FAIL_MACRO;

0 commit comments

Comments
 (0)