[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC PATCH 04/10] xen: add reference counter support


  • To: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>
  • Date: Wed, 31 Aug 2022 14:10:59 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=epam.com; dmarc=pass action=none header.from=epam.com; dkim=pass header.d=epam.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=8pxAUO1JX4YrzbhZ2dXAYA6LnDaQYu3aeZFrK4TB2Vg=; b=TICh7n6uPBfXv7fpBpV0XOmjOyeUtb78AvszL1B7DMr/jo2bKQ1hYUOIvOxn6MiCuBze9IvERmjual8fcP7EdAouF7emC8XeN2KWaxI9HF+57UxQi4nJiOICTDoiGyM3Bj6AaabikYVwSyT97wnIvFuibXxRJ8FZV6CMoVC0THpX+V5JL3A8qztM83NAPdQd2B5KcZpJPzCcMDUMDlvoxmU9Md5wQgwVKOyKLiTaMz4X5uNtDZKju8LJCWWkckvieYbR+gpVvqMCpc0fqfXS9BJw7SobfkMWq2UH+7waD5KZ6Il/OktsjyLvZkjbRXbG0NaO9i3zPdN/uzspxAMGnQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MJGXXIDHoHiJtLrVaEAuqTYW+gejAS7XKvxe4xb4XAqEVKvEji2TnqMCChDZ9CtjME+d7JYJd92U1Jj/RMvPnNgwwrNbYVFVXmGZMZtUPFBwTsuP1BN5DDMHHf3X18kU/5mkl7BINV31ycs+ZJp5Hq3LnTb28aMLi8ZO88R2Yoywa17in5+ik3FEJjM/h2iztgfnAWhMc8Yo/RypRVqreccNz02NwyYeB7aJHDREo5Kwvm/1ggYizVwRxLtLjd2zhjEhmAhnXjX8zrLbqFNUye9G1+YyshAsiMebw18Oa9cKXoaYK0diX9mIoWm/5hov2K7tRJ8lHCkkVrg2a35+ew==
  • Cc: Oleksandr Andrushchenko <Oleksandr_Andrushchenko@xxxxxxxx>, Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, George Dunlap <george.dunlap@xxxxxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Julien Grall <julien@xxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>
  • Delivery-date: Wed, 31 Aug 2022 14:11:22 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Thread-index: AQHYvUN+XE3jlJIvEkq5uMVpvnNlQg==
  • Thread-topic: [RFC PATCH 04/10] xen: add reference counter support

We can use reference counter to ease up object lifetime management.
This patch adds very basic support for reference counters. refcnt
should be used in the following way:

1. Protected structure should have refcnt_t field

2. This field should be initialized with refcnt_init() during object
construction.

3. If code holds a valid pointer to a structure/object it can increase
refcount with refcnt_get(). No additional locking is required.

4. Code should call refcnt_put() before dropping pointer to a
protected structure. `destructor` is a call back function that should
destruct object and free all resources, including structure protected
itself. Destructor will be called if reference counter reaches zero.

5. If code does not hold a valid pointer to a protected structure it
should use other locking mechanism to obtain a pointer. For example,
it should lock a list that hold protected objects.

Signed-off-by: Volodymyr Babchuk <volodymyr_babchuk@xxxxxxxx>
---
 xen/include/xen/refcnt.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100644 xen/include/xen/refcnt.h

diff --git a/xen/include/xen/refcnt.h b/xen/include/xen/refcnt.h
new file mode 100644
index 0000000000..7f5395a21c
--- /dev/null
+++ b/xen/include/xen/refcnt.h
@@ -0,0 +1,28 @@
+#ifndef __XEN_REFCNT_H__
+#define __XEN_REFCNT_H__
+
+#include <asm/atomic.h>
+
+typedef atomic_t refcnt_t;
+
+static inline void refcnt_init(refcnt_t *refcnt)
+{
+       atomic_set(refcnt, 1);
+}
+
+static inline void refcnt_get(refcnt_t *refcnt)
+{
+#ifndef NDEBUG
+       ASSERT(atomic_add_unless(refcnt, 1, 0) > 0);
+#else
+       atomic_add_unless(refcnt, 1, 0);
+#endif
+}
+
+static inline void refcnt_put(refcnt_t *refcnt, void (*destructor)(refcnt_t 
*refcnt))
+{
+       if ( atomic_dec_and_test(refcnt) )
+               destructor(refcnt);
+}
+
+#endif
-- 
2.36.1



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.