Module: xenomai-forge
Branch: next
Commit: 4ad95399abfdb3c1c722c1650de0970a55ffe155
URL:    
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=4ad95399abfdb3c1c722c1650de0970a55ffe155

Author: Gilles Chanteperdrix <gilles.chanteperd...@xenomai.org>
Date:   Sat Dec 28 21:57:41 2013 +0100

cobalt: add xnid, rbtree based per-process ids table

To be used by named semaphores and file descriptors.

---

 include/cobalt/kernel/tree.h |   74 ++++++++++++++++++++++++++++++++++++++++++
 kernel/cobalt/Makefile       |    3 +-
 kernel/cobalt/tree.c         |   58 +++++++++++++++++++++++++++++++++
 3 files changed, 134 insertions(+), 1 deletion(-)

diff --git a/include/cobalt/kernel/tree.h b/include/cobalt/kernel/tree.h
new file mode 100644
index 0000000..993d907
--- /dev/null
+++ b/include/cobalt/kernel/tree.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2014 Gilles Chanteperdrix <gilles.chanteperd...@xenomai.org>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _COBALT_KERNEL_TREE_H
+#define _COBALT_KERNEL_TREE_H
+
+#include <linux/rbtree.h>
+#include <cobalt/kernel/assert.h>
+
+struct xnid {
+       unsigned long id;
+       struct rb_node link;
+};
+
+static inline void xntree_init(struct rb_root *t)
+{
+       *t = RB_ROOT;
+}
+
+void xntree_cleanup(struct rb_root *t, void *cookie,
+               void (*destroy)(void *cookie, struct xnid *id));
+
+int xnid_enter(struct rb_root *t, struct xnid *xnid, unsigned long id);
+
+static inline unsigned long xnid_id(struct xnid *i)
+{
+       return i->id;
+}
+
+static inline
+struct xnid *xnid_fetch(struct rb_root *t, unsigned long id)
+{
+       struct rb_node *node = t->rb_node;
+
+       while (node) {
+               struct xnid *i = container_of(node, struct xnid, link);
+
+               if (id < i->id)
+                       node = node->rb_left;
+               else if (id > i->id)
+                       node = node->rb_right;
+               else
+                       return i;
+       }
+
+       return NULL;
+}
+
+static inline int xnid_remove(struct rb_root *t, struct xnid *xnid)
+{
+#if XENO_DEBUG(NUCLEUS)
+       if (xnid_fetch(t, xnid->id) != xnid)
+               return -ENOENT;
+#endif
+       rb_erase(&xnid->link, t);
+       return 0;
+}
+
+#endif /* _COBALT_KERNEL_TREE_H */
diff --git a/kernel/cobalt/Makefile b/kernel/cobalt/Makefile
index 7e65f10..af4c2e0 100644
--- a/kernel/cobalt/Makefile
+++ b/kernel/cobalt/Makefile
@@ -17,7 +17,8 @@ xenomai-y :=  apc.o           \
                shadow.o        \
                synch.o         \
                thread.o        \
-               timer.o
+               timer.o         \
+               tree.o
 
 xenomai-$(CONFIG_XENO_OPT_SCHED_QUOTA) += sched-quota.o
 xenomai-$(CONFIG_XENO_OPT_SCHED_WEAK) += sched-weak.o
diff --git a/kernel/cobalt/tree.c b/kernel/cobalt/tree.c
new file mode 100644
index 0000000..1e40183
--- /dev/null
+++ b/kernel/cobalt/tree.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2014 Gilles Chanteperdrix <gilles.chanteperd...@xenomai.org>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <cobalt/kernel/tree.h>
+
+void xntree_cleanup(struct rb_root *t, void *cookie,
+               void (*destroy)(void *cookie, struct xnid *id))
+{
+       struct rb_node *node, *next;
+
+       node = rb_first(t);
+       while (node) {
+               next = rb_next(node);
+
+               /* destroy is expected to remove the node from the rbtree */
+               destroy(cookie, container_of(node, struct xnid, link));
+
+               node = next;
+       }
+}
+
+int xnid_enter(struct rb_root *t, struct xnid *xnid, unsigned long id)
+{
+       struct rb_node **new = &t->rb_node, *parent = NULL;
+
+       while (*new) {
+               struct xnid *i = container_of(*new, struct xnid, link);
+
+               parent = *new;
+               if (id < i->id)
+                       new = &((*new)->rb_left);
+               else if (id > i->id)
+                       new = &((*new)->rb_right);
+               else
+                       return -EBUSY;
+       }
+
+       xnid->id = id;
+       rb_link_node(&xnid->link, parent, new);
+       rb_insert_color(&xnid->link, t);
+
+       return 0;
+}


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to