[2020-02] Fix leak in assembly-specific dllmap lookups (#21053)
[mono-project.git] / mono / metadata / monitor.h
blob711fe17240ea45e97202d5f30bc5417dce1f739a
1 /**
2 * \file
3 * Monitor locking functions
5 * Author:
6 * Dick Porter (dick@ximian.com)
8 * (C) 2003 Ximian, Inc
9 */
11 #ifndef _MONO_METADATA_MONITOR_H_
12 #define _MONO_METADATA_MONITOR_H_
14 #include <glib.h>
15 #include <mono/metadata/object.h>
16 #include <mono/utils/mono-compiler.h>
17 #include <mono/utils/mono-coop-mutex.h>
18 #include <mono/metadata/icalls.h>
20 #define OWNER_MASK 0x0000ffff
21 #define ENTRY_COUNT_MASK 0xffff0000
22 #define ENTRY_COUNT_WAITERS 0x80000000
23 #define ENTRY_COUNT_ZERO 0x7fff0000
24 #define ENTRY_COUNT_SHIFT 16
26 struct _MonoThreadsSync
29 * The entry count field can be negative, which would mean that the entry_sem is
30 * signaled and nobody is waiting to acquire it. This can happen when the thread
31 * that was waiting is either interrupted or timeouts, and the owner releases
32 * the lock before the forementioned thread updates the entry count.
34 * The 0 entry_count value is encoded as ENTRY_COUNT_ZERO, positive numbers being
35 * greater than it and negative numbers smaller than it.
37 guint32 status; /* entry_count (16) | owner_id (16) */
38 guint32 nest;
39 #ifdef HAVE_MOVING_COLLECTOR
40 gint32 hash_code;
41 #endif
42 GSList *wait_list;
43 void *data;
44 MonoCoopMutex *entry_mutex;
45 MonoCoopCond *entry_cond;
49 * Lock word format:
51 * The least significant bit stores whether a hash for the object is computed
52 * which is stored either in the lock word or in the MonoThreadsSync structure
53 * that the lock word points to.
55 * The second bit stores whether the lock word is inflated, containing an
56 * address to the MonoThreadsSync structure.
58 * If both bits are 0, either the lock word is free (entire lock word is 0)
59 * or it is a thin/flat lock.
61 * 32-bit
62 * LOCK_WORD_FLAT: [owner:22 | nest:8 | status:2]
63 * LOCK_WORD_THIN_HASH: [hash:30 | status:2]
64 * LOCK_WORD_INFLATED: [sync:30 | status:2]
65 * LOCK_WORD_FAT_HASH: [sync:30 | status:2]
67 * 64-bit
68 * LOCK_WORD_FLAT: [unused:22 | owner:32 | nest:8 | status:2]
69 * LOCK_WORD_THIN_HASH: [hash:62 | status:2]
70 * LOCK_WORD_INFLATED: [sync:62 | status:2]
71 * LOCK_WORD_FAT_HASH: [sync:62 | status:2]
73 * In order to save processing time and to have one additional value, the nest
74 * count starts from 0 for the lock word (just valid thread ID in the lock word
75 * means that the thread holds the lock once, although nest is 0).
76 * FIXME Have the same convention on inflated locks
79 typedef union {
80 gsize lock_word;
81 MonoThreadsSync *sync;
82 } LockWord;
84 enum {
85 LOCK_WORD_FLAT = 0,
86 LOCK_WORD_HAS_HASH = 1,
87 LOCK_WORD_INFLATED = 2,
89 LOCK_WORD_STATUS_BITS = 2,
90 LOCK_WORD_NEST_BITS = 8,
92 LOCK_WORD_STATUS_MASK = (1 << LOCK_WORD_STATUS_BITS) - 1,
93 LOCK_WORD_NEST_MASK = ((1 << LOCK_WORD_NEST_BITS) - 1) << LOCK_WORD_STATUS_BITS,
95 LOCK_WORD_HASH_SHIFT = LOCK_WORD_STATUS_BITS,
96 LOCK_WORD_NEST_SHIFT = LOCK_WORD_STATUS_BITS,
97 LOCK_WORD_OWNER_SHIFT = LOCK_WORD_STATUS_BITS + LOCK_WORD_NEST_BITS
100 MONO_API void
101 mono_locks_dump (gboolean include_untaken);
103 void
104 mono_monitor_init (void);
106 void
107 mono_monitor_cleanup (void);
109 ICALL_EXTERN_C
110 MonoBoolean
111 mono_monitor_enter_internal (MonoObject *obj);
113 ICALL_EXTERN_C
114 void
115 mono_monitor_enter_v4_internal (MonoObject *obj, MonoBoolean *lock_taken);
117 ICALL_EXTERN_C
118 guint32
119 mono_monitor_enter_fast (MonoObject *obj);
121 ICALL_EXTERN_C
122 guint32
123 mono_monitor_enter_v4_fast (MonoObject *obj, MonoBoolean *lock_taken);
125 guint32
126 mono_monitor_get_object_monitor_gchandle (MonoObject *object);
128 void
129 mono_monitor_threads_sync_members_offset (int *status_offset, int *nest_offset);
130 #define MONO_THREADS_SYNC_MEMBER_OFFSET(o) ((o)>>8)
131 #define MONO_THREADS_SYNC_MEMBER_SIZE(o) ((o)&0xff)
133 #if ENABLE_NETCORE
134 ICALL_EXPORT
135 gint64
136 ves_icall_System_Threading_Monitor_Monitor_LockContentionCount (void);
137 #endif
139 #endif /* _MONO_METADATA_MONITOR_H_ */