[coop] Introduce new runtime handle API
[mono-project.git] / mono / metadata / handle.h
blob7f8039786d6f41fcef32ccc6117e7d8727d227f9
1 /*
2 * handle.h: Handle to object in native code
4 * Authors:
5 * - Ludovic Henry <ludovic@xamarin.com>
7 * Copyright 2015 Xamarin, Inc. (www.xamarin.com)
8 */
10 #ifndef __MONO_HANDLE_H__
11 #define __MONO_HANDLE_H__
13 #include <config.h>
14 #include <glib.h>
16 #include "object.h"
17 #include "class.h"
18 #include "class-internals.h"
19 #include "threads-types.h"
21 #include "mono/utils/mono-threads-coop.h"
23 G_BEGIN_DECLS
25 typedef struct _MonoHandleStorage MonoHandleStorage;
26 typedef MonoHandleStorage* MonoHandle;
29 * DO NOT ACCESS DIRECTLY
30 * USE mono_handle_obj BELOW TO ACCESS OBJ
32 * The field obj is not private as there is no way to do that
33 * in C, but using a C++ template would simplify that a lot
35 struct _MonoHandleStorage {
36 MonoObject *obj;
39 #ifndef CHECKED_BUILD
41 #define mono_handle_obj(handle) ((handle)->obj)
43 #define mono_handle_assign(handle,rawptr) do { (handle)->obj = (rawptr); } while(0)
45 #else
47 static inline void
48 mono_handle_check_in_critical_section (const gchar* file, const gint lineno)
50 MonoThreadInfo *info = (MonoThreadInfo*) mono_thread_info_current_unchecked ();
51 if (info && !info->inside_critical_region)
52 g_error ("Assertion at %s:%d: mono_handle_check_in_critical_section failed", file, lineno);
55 #define mono_handle_obj(handle) (mono_handle_check_in_critical_section (__FILE__, __LINE__), (handle)->obj)
57 #define mono_handle_assign(handle,rawptr) do { mono_handle_check_in_critical_section (__FILE__, __LINE__); (handle)->obj = (rawptr); } while (0)
59 #endif
61 static inline MonoClass*
62 mono_handle_class (MonoHandle handle)
64 return handle->obj->vtable->klass;
67 static inline MonoDomain*
68 mono_handle_domain (MonoHandle handle)
70 return handle->obj->vtable->domain;
73 #define MONO_HANDLE_TYPE_DECL(type) typedef struct { type *obj; } type ## HandleStorage ; \
74 typedef type ## HandleStorage * type ## Handle
75 #define MONO_HANDLE_TYPE(type) type ## Handle
76 #define MONO_HANDLE_NEW(type,obj) ((type ## Handle) mono_handle_new ((MonoObject*) (obj)))
77 #define MONO_HANDLE_ELEVATE(type,handle) ((type ## Handle) mono_handle_elevate ((MonoObject*) (handle)->obj))
79 #define MONO_HANDLE_ASSIGN(handle,rawptr) \
80 do { \
81 mono_handle_assign ((handle), (rawptr)); \
82 } while (0)
84 #define MONO_HANDLE_SETREF(handle,fieldname,value) \
85 do { \
86 MonoHandle __value = (MonoHandle) (value); \
87 MONO_PREPARE_CRITICAL; \
88 MONO_OBJECT_SETREF (mono_handle_obj ((handle)), fieldname, mono_handle_obj (__value)); \
89 MONO_FINISH_CRITICAL; \
90 } while (0)
92 #define MONO_HANDLE_SET(handle,fieldname,value) \
93 do { \
94 MONO_PREPARE_CRITICAL; \
95 mono_handle_obj ((handle))->fieldname = (value); \
96 MONO_FINISH_CRITICAL; \
97 } while (0)
99 #define MONO_HANDLE_ARRAY_SETREF(handle,index,value) \
100 do { \
101 MonoHandle __value = (MonoHandle) (value); \
102 MONO_PREPARE_CRITICAL; \
103 mono_array_setref (mono_handle_obj ((handle)), (index), mono_handle_obj (__value)); \
104 MONO_FINISH_CRITICAL; \
105 } while (0)
107 #define MONO_HANDLE_ARRAY_SET(handle,type,index,value) \
108 do { \
109 MONO_PREPARE_CRITICAL; \
110 mono_array_set (mono_handle_obj ((handle)), (type), (index), (value)); \
111 MONO_FINISH_CRITICAL; \
112 } while (0)
114 /* handle arena specific functions */
116 typedef struct _MonoHandleArena MonoHandleArena;
118 gsize
119 mono_handle_arena_size (gsize nb_handles);
121 MonoHandle
122 mono_handle_new (MonoObject *rawptr);
124 MonoHandle
125 mono_handle_elevate (MonoHandle handle);
127 /* Some common handle types */
129 MONO_HANDLE_TYPE_DECL (MonoArray);
130 MONO_HANDLE_TYPE_DECL (MonoString);
132 G_END_DECLS
134 #endif /* __MONO_HANDLE_H__ */