]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/posix-fd: Provide public ofile refcounting
authorAndrei Tatar <andrei@unikraft.io>
Wed, 25 Sep 2024 14:42:26 +0000 (16:42 +0200)
committerUnikraft Bot <monkey@unikraft.io>
Tue, 25 Feb 2025 07:58:01 +0000 (07:58 +0000)
This change adds public helpers for refcounting open file descriptions.

Signed-off-by: Andrei Tatar <andrei@unikraft.io>
Approved-by: Sergiu Moga <sergiu@unikraft.io>
Reviewed-by: Sergiu Moga <sergiu@unikraft.io>
GitHub-Closes: #1592

lib/posix-fd/include/uk/posix-fd.h
lib/posix-fdtab/fdtab.c

index cec9de3787b6cfb2ca81601af6045e08eef29c50..6532f78522b9db2d9849b5d60545dc9d1b6d77ef 100644 (file)
@@ -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 \
index 607ea240d18df3d610413db3eb6418a3bc2e90aa..1a34e8f31cfbf9b8e2e0e795a2caa2affc325ea3 100644 (file)
@@ -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);
        }