Merge pull request #2353 from ludovic-henry/fix-servicemodel-15153
[mono.git] / mono / metadata / monitor.h
1 /*
2  * monitor.h: Monitor locking functions
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *
7  * (C) 2003 Ximian, Inc
8  */
9
10 #ifndef _MONO_METADATA_MONITOR_H_
11 #define _MONO_METADATA_MONITOR_H_
12
13 #include <glib.h>
14 #include <mono/metadata/object.h>
15 #include <mono/io-layer/io-layer.h>
16 #include "mono/utils/mono-compiler.h"
17
18 G_BEGIN_DECLS
19
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
25
26 struct _MonoThreadsSync
27 {
28         /*
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.
33          *
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.
36          */
37         guint32 status;                 /* entry_count (16) | owner_id (16) */
38         guint32 nest;
39 #ifdef HAVE_MOVING_COLLECTOR
40         gint32 hash_code;
41 #endif
42         HANDLE entry_sem;
43         GSList *wait_list;
44         void *data;
45 };
46
47 /*
48  * Lock word format:
49  *
50  * The least significant bit stores whether a hash for the object is computed
51  * which is stored either in the lock word or in the MonoThreadsSync structure
52  * that the lock word points to.
53  *
54  * The second bit stores whether the lock word is inflated, containing an
55  * address to the MonoThreadsSync structure.
56  *
57  * If both bits are 0, either the lock word is free (entire lock word is 0)
58  * or it is a thin/flat lock.
59  *
60  * 32-bit
61  *            LOCK_WORD_FLAT:    [owner:22 | nest:8 | status:2]
62  *       LOCK_WORD_THIN_HASH:    [hash:30 | status:2]
63  *        LOCK_WORD_INFLATED:    [sync:30 | status:2]
64  *        LOCK_WORD_FAT_HASH:    [sync:30 | status:2]
65  *
66  * 64-bit
67  *            LOCK_WORD_FLAT:    [unused:22 | owner:32 | nest:8 | status:2]
68  *       LOCK_WORD_THIN_HASH:    [hash:62 | status:2]
69  *        LOCK_WORD_INFLATED:    [sync:62 | status:2]
70  *        LOCK_WORD_FAT_HASH:    [sync:62 | status:2]
71  *
72  * In order to save processing time and to have one additional value, the nest
73  * count starts from 0 for the lock word (just valid thread ID in the lock word
74  * means that the thread holds the lock once, although nest is 0).
75  * FIXME Have the same convention on inflated locks
76  */
77
78 typedef union {
79 #if SIZEOF_REGISTER == 8
80         guint64 lock_word;
81 #elif SIZEOF_REGISTER == 4
82         guint32 lock_word;
83 #endif
84         MonoThreadsSync *sync;
85 } LockWord;
86
87
88 enum {
89         LOCK_WORD_FLAT = 0,
90         LOCK_WORD_HAS_HASH = 1,
91         LOCK_WORD_INFLATED = 2,
92
93         LOCK_WORD_STATUS_BITS = 2,
94         LOCK_WORD_NEST_BITS = 8,
95
96         LOCK_WORD_STATUS_MASK = (1 << LOCK_WORD_STATUS_BITS) - 1,
97         LOCK_WORD_NEST_MASK = ((1 << LOCK_WORD_NEST_BITS) - 1) << LOCK_WORD_STATUS_BITS,
98
99         LOCK_WORD_HASH_SHIFT = LOCK_WORD_STATUS_BITS,
100         LOCK_WORD_NEST_SHIFT = LOCK_WORD_STATUS_BITS,
101         LOCK_WORD_OWNER_SHIFT = LOCK_WORD_STATUS_BITS + LOCK_WORD_NEST_BITS
102 };
103
104 MONO_API void mono_locks_dump (gboolean include_untaken);
105
106 void mono_monitor_init (void);
107 void mono_monitor_cleanup (void);
108
109 guint32 mono_monitor_get_object_monitor_gchandle (MonoObject *object);
110
111 void mono_monitor_threads_sync_members_offset (int *status_offset, int *nest_offset);
112 #define MONO_THREADS_SYNC_MEMBER_OFFSET(o)      ((o)>>8)
113 #define MONO_THREADS_SYNC_MEMBER_SIZE(o)        ((o)&0xff)
114
115 extern gboolean ves_icall_System_Threading_Monitor_Monitor_try_enter(MonoObject *obj, guint32 ms);
116 extern gboolean ves_icall_System_Threading_Monitor_Monitor_test_owner(MonoObject *obj);
117 extern gboolean ves_icall_System_Threading_Monitor_Monitor_test_synchronised(MonoObject *obj);
118 extern void ves_icall_System_Threading_Monitor_Monitor_pulse(MonoObject *obj);
119 extern void ves_icall_System_Threading_Monitor_Monitor_pulse_all(MonoObject *obj);
120 extern gboolean ves_icall_System_Threading_Monitor_Monitor_wait(MonoObject *obj, guint32 ms);
121 extern void ves_icall_System_Threading_Monitor_Monitor_try_enter_with_atomic_var (MonoObject *obj, guint32 ms, char *lockTaken);
122
123 G_END_DECLS
124
125 #endif /* _MONO_METADATA_MONITOR_H_ */