Skip to content

Commit

Permalink
standardize usage of refcount
Browse files Browse the repository at this point in the history
  • Loading branch information
zmshahaha committed Aug 9, 2024
1 parent 04b4889 commit 6b54ac3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
14 changes: 6 additions & 8 deletions components/dfs/dfs_v2/src/dfs_dentry.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,23 @@ struct dfs_dentry * dfs_dentry_ref(struct dfs_dentry *dentry)
RT_ASSERT(dentry);

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;
}

struct dfs_dentry *dfs_dentry_unref(struct dfs_dentry *dentry)
{
rt_err_t ret = RT_EOK;

RT_ASSERT(dentry);
RT_ASSERT(dentry && (dentry->flags & DENTRY_IS_ALLOCED));

ret = dfs_file_lock();
if (ret == RT_EOK)
{
if (dentry->flags & DENTRY_IS_ALLOCED)
{
rt_atomic_sub(&(dentry->ref_count), 1);
}

if (rt_atomic_load(&(dentry->ref_count)) == 0)
if (rt_atomic_dec_and_test(&(dentry->ref_count)))
{
DLOG(msg, "dentry", "dentry", DLOG_MSG, "free dentry, ref_count=0");
if (dentry->flags & DENTRY_IS_ADDHASH)
Expand Down
10 changes: 9 additions & 1 deletion components/dfs/dfs_v2/src/dfs_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ void dfs_file_deinit(struct dfs_file *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);

if (file->dentry)
Expand All @@ -210,7 +218,7 @@ void dfs_file_deinit(struct dfs_file *file)

if (file->vnode)
{
dfs_vnode_unref(file->vnode);
//dfs_vnode_unref(file->vnode);
}

if (file->mmap_context)
Expand Down
13 changes: 5 additions & 8 deletions components/dfs/dfs_v2/src/dfs_mnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,10 @@ struct dfs_mnt *dfs_mnt_lookup(const char *fullpath)

struct dfs_mnt* dfs_mnt_ref(struct dfs_mnt* mnt)
{
if (mnt)
{
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)));
}
RT_ASSERT(mnt);

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)));

return mnt;
}
Expand All @@ -248,9 +247,7 @@ int dfs_mnt_unref(struct dfs_mnt* mnt)

if (mnt)
{
rt_atomic_sub(&(mnt->ref_count), 1);

if (rt_atomic_load(&(mnt->ref_count)) == 0)
if (rt_atomic_dec_and_test(&(mnt->ref_count)))
{
dfs_lock();

Expand Down
4 changes: 1 addition & 3 deletions components/dfs/dfs_v2/src/dfs_pcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,7 @@ static void dfs_page_unref(struct dfs_page *page)

dfs_aspace_lock(aspace);

rt_atomic_sub(&(page->ref_count), 1);

if (rt_atomic_load(&(page->ref_count)) == 0)
if (rt_atomic_dec_and_test(&(page->ref_count)))
{
dfs_page_unmap(page);

Expand Down

0 comments on commit 6b54ac3

Please sign in to comment.