[coop] Checked build GC critical regions.
[mono-project.git] / mono / metadata / handle.h
blobbf94296791fc6a9b06c82fa25870344919569b70
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 ()
50 MONO_REQ_GC_UNSAFE_MODE;
53 #define mono_handle_obj(handle) (mono_handle_check_in_critical_section (), (handle)->obj)
55 #define mono_handle_assign(handle,rawptr) do { mono_handle_check_in_critical_section (); (handle)->obj = (rawptr); } while (0)
57 #endif
59 static inline MonoClass*
60 mono_handle_class (MonoHandle handle)
62 return handle->obj->vtable->klass;
65 static inline MonoDomain*
66 mono_handle_domain (MonoHandle handle)
68 return handle->obj->vtable->domain;
71 #define MONO_HANDLE_TYPE_DECL(type) typedef struct { type *obj; } type ## HandleStorage ; \
72 typedef type ## HandleStorage * type ## Handle
73 #define MONO_HANDLE_TYPE(type) type ## Handle
74 #define MONO_HANDLE_NEW(type,obj) ((type ## Handle) mono_handle_new ((MonoObject*) (obj)))
75 #define MONO_HANDLE_ELEVATE(type,handle) ((type ## Handle) mono_handle_elevate ((MonoObject*) (handle)->obj))
77 #define MONO_HANDLE_ASSIGN(handle,rawptr) \
78 do { \
79 mono_handle_assign ((handle), (rawptr)); \
80 } while (0)
82 #define MONO_HANDLE_SETREF(handle,fieldname,value) \
83 do { \
84 MonoHandle __value = (MonoHandle) (value); \
85 MONO_PREPARE_GC_CRITICAL_REGION; \
86 MONO_OBJECT_SETREF (mono_handle_obj ((handle)), fieldname, mono_handle_obj (__value)); \
87 MONO_FINISH_GC_CRITICAL_REGION; \
88 } while (0)
90 #define MONO_HANDLE_SET(handle,fieldname,value) \
91 do { \
92 MONO_PREPARE_GC_CRITICAL_REGION; \
93 mono_handle_obj ((handle))->fieldname = (value); \
94 MONO_FINISH_GC_CRITICAL_REGION; \
95 } while (0)
97 #define MONO_HANDLE_ARRAY_SETREF(handle,index,value) \
98 do { \
99 MonoHandle __value = (MonoHandle) (value); \
100 MONO_PREPARE_GC_CRITICAL_REGION; \
101 mono_array_setref (mono_handle_obj ((handle)), (index), mono_handle_obj (__value)); \
102 MONO_FINISH_GC_CRITICAL_REGION; \
103 } while (0)
105 #define MONO_HANDLE_ARRAY_SET(handle,type,index,value) \
106 do { \
107 MONO_PREPARE_GC_CRITICAL_REGION; \
108 mono_array_set (mono_handle_obj ((handle)), (type), (index), (value)); \
109 MONO_FINISH_GC_CRITICAL_REGION; \
110 } while (0)
112 /* handle arena specific functions */
114 typedef struct _MonoHandleArena MonoHandleArena;
116 gsize
117 mono_handle_arena_size (gsize nb_handles);
119 MonoHandle
120 mono_handle_new (MonoObject *rawptr);
122 MonoHandle
123 mono_handle_elevate (MonoHandle handle);
125 /* Some common handle types */
127 MONO_HANDLE_TYPE_DECL (MonoArray);
128 MONO_HANDLE_TYPE_DECL (MonoString);
130 G_END_DECLS
132 #endif /* __MONO_HANDLE_H__ */