[runtime] Add a simple linearizable property bag implementation.
authorRodrigo Kumpera <kumpera@gmail.com>
Tue, 16 Sep 2014 22:55:46 +0000 (18:55 -0400)
committerRodrigo Kumpera <kumpera@gmail.com>
Thu, 10 Nov 2016 22:55:42 +0000 (14:55 -0800)
mono/metadata/Makefile.am
mono/metadata/property-bag.c [new file with mode: 0644]
mono/metadata/property-bag.h [new file with mode: 0644]

index a48d586713f751a7f02520017476835819ec8c35..bc369533a3f86bdc79f4d09e1c21cce51c292667 100644 (file)
@@ -201,6 +201,8 @@ common_sources = \
        number-ms.h             \
        object-internals.h      \
        opcodes.c               \
+       property-bag.h  \
+       property-bag.c  \
        socket-io.c             \
        socket-io.h             \
        w32process.c            \
diff --git a/mono/metadata/property-bag.c b/mono/metadata/property-bag.c
new file mode 100644 (file)
index 0000000..28e8113
--- /dev/null
@@ -0,0 +1,49 @@
+
+/*
+ * property-bag.c: Linearizable property bag.
+ *
+ * Authors:
+ *   Rodrigo Kumpera (kumpera@gmail.com)
+ *
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
+ */
+#include <mono/metadata/property-bag.h>
+#include <mono/utils/atomic.h>
+#include <mono/utils/mono-membar.h>
+
+void*
+mono_property_bag_get (MonoPropertyBag *bag, int tag)
+{
+       MonoPropertyBagItem *item;
+       
+       for (item = bag->head; item && item->tag <= tag; item = item->next) {
+               if (item->tag == tag)
+                       return item;
+       }
+       return NULL;
+}
+
+void*
+mono_property_bag_add (MonoPropertyBag *bag, void *value)
+{
+       MonoPropertyBagItem *cur, **prev, *item = value;
+       int tag = item->tag;
+       mono_memory_barrier (); //publish the values in value
+
+retry:
+       prev = &bag->head;
+       while (1) {
+               cur = *prev;
+               if (!cur || cur->tag > tag) {
+                       item->next = cur;
+                       if (InterlockedCompareExchangePointer ((void*)prev, item, cur) == cur)
+                               return item;
+                       goto retry;
+               } else if (cur->tag == tag) {
+                       return cur;
+               } else {
+                       prev = &cur->next;
+               }
+       }
+       return value;
+}
diff --git a/mono/metadata/property-bag.h b/mono/metadata/property-bag.h
new file mode 100644 (file)
index 0000000..8f3f0ee
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * property-bag.h: Linearizable property bag.
+ *
+ * Authors:
+ *   Rodrigo Kumpera (kumpera@gmail.com)
+ *
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
+ */
+#ifndef __MONO_METADATA_PROPERTY_BAG_H__
+#define __MONO_METADATA_PROPERTY_BAG_H__
+
+#include <mono/utils/mono-compiler.h>
+
+typedef struct _MonoPropertyBagItem MonoPropertyBagItem;
+
+struct _MonoPropertyBagItem {
+       MonoPropertyBagItem *next;
+       int tag;
+};
+
+typedef struct {
+       MonoPropertyBagItem *head;
+} MonoPropertyBag;
+
+void* mono_property_bag_get (MonoPropertyBag *bag, int tag);
+void* mono_property_bag_add (MonoPropertyBag *bag, void *value);
+
+#endif