解决多线程dmabuffer竞态问题
This commit is contained in:
@@ -110,6 +110,9 @@ zvfs_debug_has_fd_mapping(int fd)
|
||||
return found;
|
||||
}
|
||||
|
||||
/* close 路径辅助:在文件后半段实现。 */
|
||||
static int zvfs_detach_fd_mapping(int fd, int do_sync_md);
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* 内部:路径判定辅助 */
|
||||
/* ------------------------------------------------------------------ */
|
||||
@@ -779,6 +782,74 @@ fopen64(const char *path, const char *mode)
|
||||
return fp;
|
||||
}
|
||||
|
||||
int
|
||||
fclose(FILE *stream)
|
||||
{
|
||||
ZVFS_HOOK_ENTER();
|
||||
|
||||
int ret;
|
||||
int ret_errno = 0;
|
||||
int bk_rc = 0;
|
||||
int bk_errno = 0;
|
||||
int fd = -1;
|
||||
int need_bookkeeping = 0;
|
||||
|
||||
if (!stream) {
|
||||
errno = EINVAL;
|
||||
ZVFS_HOOK_LEAVE();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!ZVFS_IN_HOOK()) {
|
||||
fd = fileno(stream);
|
||||
if (fd >= 0 && zvfs_is_zvfs_fd(fd)) {
|
||||
need_bookkeeping = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!real_fclose) {
|
||||
errno = ENOSYS;
|
||||
ZVFS_HOOK_LEAVE();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ZVFS_IN_HOOK() || !need_bookkeeping) {
|
||||
ret = real_fclose(stream);
|
||||
ZVFS_HOOK_LEAVE();
|
||||
return ret;
|
||||
}
|
||||
|
||||
zvfs_ensure_init();
|
||||
|
||||
ret = real_fclose(stream);
|
||||
if (ret < 0) {
|
||||
ret_errno = errno;
|
||||
}
|
||||
|
||||
/*
|
||||
* 无论 real_fclose 是否报错,都尝试回收 zvfs bookkeeping。
|
||||
* 某些 libc 实现即使返回 EOF,也可能已经关闭了底层 fd。
|
||||
*/
|
||||
if (zvfs_detach_fd_mapping(fd, 1) < 0) {
|
||||
bk_rc = -1;
|
||||
bk_errno = errno;
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
errno = ret_errno;
|
||||
ZVFS_HOOK_LEAVE();
|
||||
return -1;
|
||||
}
|
||||
if (bk_rc < 0) {
|
||||
errno = bk_errno;
|
||||
ZVFS_HOOK_LEAVE();
|
||||
return -1;
|
||||
}
|
||||
|
||||
ZVFS_HOOK_LEAVE();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* creat */
|
||||
/* ------------------------------------------------------------------ */
|
||||
@@ -1109,6 +1180,7 @@ close(int fd)
|
||||
|
||||
int __close(int fd) { return close(fd); }
|
||||
int __libc_close(int fd) { return close(fd); }
|
||||
int __close_nocancel(int fd) { return close(fd); }
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* dup helper */
|
||||
|
||||
Reference in New Issue
Block a user