Skip to content

Commit bd33d37

Browse files
mysterywolfGuozhanxin
authored andcommitted
该问题在Keil-MDK中没有一个太好的解决方案。本PR为权衡利弊之后得到的一套解决方案。Keil曾经提供了底层桩函数_sys_ensure,但是目前已经被废弃。因此唯一的解决方案就是每写完一句,就调用fsync去同步一次。
该问题在mbedos社区也进行过讨论,他们也面临和我们相同的两个问题: 1. Keil-MDK无有效的fflush解决方案,mbedos依然在使用淘汰的_sys_ensure桩函数。 2. Keil-MDK没有fileno函数,我方少部分软件包因此而无法使用Keil平台编译。 参考:ARMmbed/mbed-os#1601 issue: #4928 经过进一步测试发现,Keil-MDK中的fflush函数已经退化成服务于STDOUT的函数,Keil-MDK内部有一个buffer可以存放fputc(stdout) 发过来的数据,如果调用fflush,这些数据是可以flush出去的。但是如果是真正的文件数据,fflush函数并没有提供任何桩函数接口可以让文件flush数据出去。
1 parent eb12619 commit bd33d37

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

components/libc/compilers/armlibc/syscalls.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -226,6 +226,11 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
226226
size = write(fh, buf, len);
227227
if (size >= 0)
228228
{
229+
/*
230+
fflush doesn't have a good solution in Keil-MDK,
231+
so it has to sync/flush when for each writen.
232+
*/
233+
fsync(fh);
229234
return len - size; /* success */
230235
}
231236
else
@@ -239,6 +244,18 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
239244
}
240245
}
241246

247+
/*
248+
* Flush any OS buffers associated with fh, ensuring that the file
249+
* is up to date on disk. Result is >=0 if OK, negative for an
250+
* error.
251+
* This function is deprecated. It is never called by any other library function,
252+
* and you are not required to re-implement it if you are retargeting standard I/O (stdio).
253+
*/
254+
int _sys_ensure(FILEHANDLE fh)
255+
{
256+
return fsync(fh);
257+
}
258+
242259
/*
243260
* Move the file position to a given offset from the file start.
244261
* Returns >=0 on success, <0 on failure.

0 commit comments

Comments
 (0)