Source-Changes-HG archive

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

[src/riastradh-drm2]: src/sys/external/bsd/drm2/include/linux Implement krefs...



details:   https://anonhg.NetBSD.org/src/rev/533cae73a35e
branches:  riastradh-drm2
changeset: 788018:533cae73a35e
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Wed Jul 24 01:51:36 2013 +0000

description:
Implement krefs in <linux/kref.h>.

diffstat:

 sys/external/bsd/drm2/include/linux/kref.h |  77 +++++++++++++++++++++++++++++-
 1 files changed, 76 insertions(+), 1 deletions(-)

diffs (89 lines):

diff -r 1aae266c9f92 -r 533cae73a35e sys/external/bsd/drm2/include/linux/kref.h
--- a/sys/external/bsd/drm2/include/linux/kref.h        Wed Jul 24 01:51:21 2013 +0000
+++ b/sys/external/bsd/drm2/include/linux/kref.h        Wed Jul 24 01:51:36 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kref.h,v 1.1.2.1 2013/07/24 00:33:12 riastradh Exp $   */
+/*     $NetBSD: kref.h,v 1.1.2.2 2013/07/24 01:51:36 riastradh Exp $   */
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -32,4 +32,79 @@
 #ifndef _LINUX_KREF_H_
 #define _LINUX_KREF_H_
 
+#include <sys/types.h>
+#include <sys/atomic.h>
+#include <sys/systm.h>
+
+#include <linux/mutex.h>
+
+struct kref {
+       unsigned int kr_count;
+};
+
+static inline void
+kref_init(struct kref *kref)
+{
+       kref->kr_count = 1;
+}
+
+static inline void
+kref_get(struct kref *kref)
+{
+       const unsigned int count __unused =
+           atomic_inc_uint_nv(&kref->kr_count);
+
+       KASSERT(count > 1);
+}
+
+static inline int
+kref_sub(struct kref *kref, unsigned int count, void (*release)(struct kref *))
+{
+       unsigned int old, new;
+
+       do {
+               old = kref->kr_count;
+               KASSERT(count <= old);
+               new = (old - count);
+       } while (atomic_cas_uint(&kref->kr_count, old, new) == old);
+
+       if (new == 0) {
+               (*release)(kref);
+               return 1;
+       }
+
+       return 0;
+}
+
+static inline int
+kref_put(struct kref *kref, void (*release)(struct kref *))
+{
+
+       return kref_sub(kref, 1, release);
+}
+
+static inline int
+kref_put_mutex(struct kref *kref, void (*release)(struct kref *),
+    struct mutex *interlock)
+{
+       unsigned int old, new;
+
+       do {
+               old = kref->kr_count;
+               KASSERT(old > 0);
+               if (old == 1) {
+                       mutex_lock(interlock);
+                       if (atomic_add_int_nv(&kref->kr_count, -1) == 0) {
+                               (*release)(kref);
+                               return 1;
+                       }
+                       mutex_unlock(interlock);
+                       return 0;
+               }
+               new = (old - 1);
+       } while (atomic_cas_uint(&kref->kr_count, old, new) != old);
+
+       return 0;
+}
+
 #endif  /* _LINUX_KREF_H_ */



Home | Main Index | Thread Index | Old Index