From: Andrei Tatar Date: Wed, 25 Sep 2024 14:42:26 +0000 (+0200) Subject: lib/posix-fd: Provide public ofile refcounting X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=59993608a4cc890e0ae0234d7c2ab41cc9458e96;p=unikraft%2Funikraft.git lib/posix-fd: Provide public ofile refcounting This change adds public helpers for refcounting open file descriptions. Signed-off-by: Andrei Tatar Approved-by: Sergiu Moga Reviewed-by: Sergiu Moga GitHub-Closes: #1592 --- diff --git a/lib/posix-fd/include/uk/posix-fd.h b/lib/posix-fd/include/uk/posix-fd.h index cec9de378..6532f7852 100644 --- a/lib/posix-fd/include/uk/posix-fd.h +++ b/lib/posix-fd/include/uk/posix-fd.h @@ -44,6 +44,36 @@ void uk_ofile_init(struct uk_ofile *of, of->pos = 0; } +/** + * Acquire a reference on open file description `of`. + * + * @param of Open file description + */ +static inline +void uk_ofile_acquire(struct uk_ofile *of) +{ + uk_refcount_acquire(&of->refcnt); +} + +/** + * Release a reference held on open file description `of`. + * + * Do not call directly unless you are prepared to handle cleanup after the last + * reference is dropped. Instead use the release function provided by the lib + * where you got the open file reference from. + * + * @param of Open file description + * + * @return + * == 0: There are remaining references held + * != 0: The last reference has just been released + */ +static inline +int uk_ofile_release(struct uk_ofile *of) +{ + return uk_refcount_release(&of->refcnt); +} + /* Mode bits from fcntl.h that open files are interested in */ #define UKFD_MODE_MASK \ diff --git a/lib/posix-fdtab/fdtab.c b/lib/posix-fdtab/fdtab.c index 607ea240d..1a34e8f31 100644 --- a/lib/posix-fdtab/fdtab.c +++ b/lib/posix-fdtab/fdtab.c @@ -145,11 +145,11 @@ static inline void ofile_del(struct uk_fdtab *tab, struct uk_ofile *of) static inline void ofile_acq(struct uk_ofile *of) { - uk_refcount_acquire(&of->refcnt); + uk_ofile_acquire(of); } static inline void ofile_rel(struct uk_fdtab *tab, struct uk_ofile *of) { - if (uk_refcount_release(&of->refcnt)) { + if (uk_ofile_release(of)) { uk_file_release(of->file); ofile_del(tab, of); }