standardize usage of refcount

This commit is contained in:
zhao maosheng 2024-08-07 17:05:07 +08:00
parent 04b4889b31
commit 6b54ac354f
4 changed files with 21 additions and 20 deletions

View File

@ -90,7 +90,10 @@ struct dfs_dentry * dfs_dentry_ref(struct dfs_dentry *dentry)
RT_ASSERT(dentry); RT_ASSERT(dentry);
rt_atomic_add(&(dentry->ref_count), 1); rt_atomic_add(&(dentry->ref_count), 1);
if (dentry->vnode)
{
rt_atomic_add(&(dentry->vnode->ref_count), 1); // TODO: dont add vnode's ref
}
return dentry; return dentry;
} }
@ -98,17 +101,12 @@ struct dfs_dentry *dfs_dentry_unref(struct dfs_dentry *dentry)
{ {
rt_err_t ret = RT_EOK; rt_err_t ret = RT_EOK;
RT_ASSERT(dentry); RT_ASSERT(dentry && (dentry->flags & DENTRY_IS_ALLOCED));
ret = dfs_file_lock(); ret = dfs_file_lock();
if (ret == RT_EOK) if (ret == RT_EOK)
{ {
if (dentry->flags & DENTRY_IS_ALLOCED) if (rt_atomic_dec_and_test(&(dentry->ref_count)))
{
rt_atomic_sub(&(dentry->ref_count), 1);
}
if (rt_atomic_load(&(dentry->ref_count)) == 0)
{ {
DLOG(msg, "dentry", "dentry", DLOG_MSG, "free dentry, ref_count=0"); DLOG(msg, "dentry", "dentry", DLOG_MSG, "free dentry, ref_count=0");
if (dentry->flags & DENTRY_IS_ADDHASH) if (dentry->flags & DENTRY_IS_ADDHASH)

View File

@ -201,6 +201,14 @@ void dfs_file_deinit(struct dfs_file *file)
{ {
RT_ASSERT(file); RT_ASSERT(file);
if (file->fops && file->fops->close)
{
if (file->fops->close(file) != RT_EOK)
{
LOG_E("file %p close error", file);
}
}
rt_mutex_detach(&file->pos_lock); rt_mutex_detach(&file->pos_lock);
if (file->dentry) if (file->dentry)
@ -210,7 +218,7 @@ void dfs_file_deinit(struct dfs_file *file)
if (file->vnode) if (file->vnode)
{ {
dfs_vnode_unref(file->vnode); //dfs_vnode_unref(file->vnode);
} }
if (file->mmap_context) if (file->mmap_context)

View File

@ -233,11 +233,10 @@ struct dfs_mnt *dfs_mnt_lookup(const char *fullpath)
struct dfs_mnt* dfs_mnt_ref(struct dfs_mnt* mnt) struct dfs_mnt* dfs_mnt_ref(struct dfs_mnt* mnt)
{ {
if (mnt) RT_ASSERT(mnt);
{
rt_atomic_add(&(mnt->ref_count), 1); rt_atomic_add(&(mnt->ref_count), 1);
DLOG(note, "mnt", "mnt(%s),ref_count=%d", mnt->fs_ops->name, rt_atomic_load(&(mnt->ref_count))); DLOG(note, "mnt", "mnt(%s),ref_count=%d", mnt->fs_ops->name, rt_atomic_load(&(mnt->ref_count)));
}
return mnt; return mnt;
} }
@ -248,9 +247,7 @@ int dfs_mnt_unref(struct dfs_mnt* mnt)
if (mnt) if (mnt)
{ {
rt_atomic_sub(&(mnt->ref_count), 1); if (rt_atomic_dec_and_test(&(mnt->ref_count)))
if (rt_atomic_load(&(mnt->ref_count)) == 0)
{ {
dfs_lock(); dfs_lock();

View File

@ -721,9 +721,7 @@ static void dfs_page_unref(struct dfs_page *page)
dfs_aspace_lock(aspace); dfs_aspace_lock(aspace);
rt_atomic_sub(&(page->ref_count), 1); if (rt_atomic_dec_and_test(&(page->ref_count)))
if (rt_atomic_load(&(page->ref_count)) == 0)
{ {
dfs_page_unmap(page); dfs_page_unmap(page);