Merge pull request #2386 from BrzVlad/fix-jit
[mono.git] / mono / io-layer / handles-private.h
1 /*
2  * handles-private.h:  Internal operations on handles
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *
7  * (C) 2002-2006 Novell, Inc.
8  */
9
10 #ifndef _WAPI_HANDLES_PRIVATE_H_
11 #define _WAPI_HANDLES_PRIVATE_H_
12
13 #include <config.h>
14 #include <glib.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <sys/types.h>
18
19 #include <mono/io-layer/wapi-private.h>
20 #include <mono/io-layer/shared.h>
21 #include <mono/utils/atomic.h>
22
23 #define _WAPI_PRIVATE_MAX_SLOTS         (1024 * 16)
24 #define _WAPI_PRIVATE_HANDLES(x) (_wapi_private_handles [x / _WAPI_HANDLE_INITIAL_COUNT][x % _WAPI_HANDLE_INITIAL_COUNT])
25 #define _WAPI_PRIVATE_HAVE_SLOT(x) ((GPOINTER_TO_UINT (x) / _WAPI_PRIVATE_MAX_SLOTS) < _WAPI_PRIVATE_MAX_SLOTS && \
26                                         _wapi_private_handles [GPOINTER_TO_UINT (x) / _WAPI_HANDLE_INITIAL_COUNT] != NULL)
27 #define _WAPI_PRIVATE_VALID_SLOT(x) (((x) / _WAPI_HANDLE_INITIAL_COUNT) < _WAPI_PRIVATE_MAX_SLOTS)
28
29 #undef DEBUG
30
31 extern struct _WapiHandleUnshared *_wapi_private_handles [];
32 extern struct _WapiHandleSharedLayout *_wapi_shared_layout;
33
34 extern guint32 _wapi_fd_reserve;
35 extern mono_mutex_t *_wapi_global_signal_mutex;
36 extern pthread_cond_t *_wapi_global_signal_cond;
37 extern int _wapi_sem_id;
38 extern gboolean _wapi_has_shut_down;
39
40 extern pid_t _wapi_getpid (void);
41 extern gpointer _wapi_handle_new (WapiHandleType type,
42                                   gpointer handle_specific);
43 extern gpointer _wapi_handle_new_fd (WapiHandleType type, int fd,
44                                      gpointer handle_specific);
45 extern gpointer _wapi_handle_new_from_offset (WapiHandleType type,
46                                               guint32 offset,
47                                               gboolean timestamp);
48 extern gboolean _wapi_lookup_handle (gpointer handle, WapiHandleType type,
49                                      gpointer *handle_specific);
50 extern gpointer _wapi_search_handle (WapiHandleType type,
51                                      gboolean (*check)(gpointer, gpointer),
52                                      gpointer user_data,
53                                      gpointer *handle_specific,
54                                      gboolean search_shared);
55 extern gint32 _wapi_search_handle_namespace (WapiHandleType type,
56                                              gchar *utf8_name);
57 extern void _wapi_handle_ref (gpointer handle);
58 extern void _wapi_handle_unref (gpointer handle);
59 extern void _wapi_handle_register_capabilities (WapiHandleType type,
60                                                 WapiHandleCapability caps);
61 extern gboolean _wapi_handle_test_capabilities (gpointer handle,
62                                                 WapiHandleCapability caps);
63 extern void _wapi_handle_ops_close (gpointer handle, gpointer data);
64 extern void _wapi_handle_ops_signal (gpointer handle);
65 extern gboolean _wapi_handle_ops_own (gpointer handle);
66 extern gboolean _wapi_handle_ops_isowned (gpointer handle);
67 extern guint32 _wapi_handle_ops_special_wait (gpointer handle,
68                                               guint32 timeout,
69                                               gboolean alertable);
70 extern void _wapi_handle_ops_prewait (gpointer handle);
71
72 extern gboolean _wapi_handle_count_signalled_handles (guint32 numhandles,
73                                                       gpointer *handles,
74                                                       gboolean waitall,
75                                                       guint32 *retcount,
76                                                       guint32 *lowest);
77 extern void _wapi_handle_unlock_handles (guint32 numhandles,
78                                          gpointer *handles);
79 extern int _wapi_handle_timedwait_signal (guint32 timeout, gboolean poll, gboolean *alerted);
80 extern int _wapi_handle_timedwait_signal_handle (gpointer handle, guint32 timeout, gboolean alertable, gboolean poll, gboolean *alerted);
81 extern gboolean _wapi_handle_get_or_set_share (guint64 device, guint64 inode,
82                                                guint32 new_sharemode,
83                                                guint32 new_access,
84                                                guint32 *old_sharemode,
85                                                guint32 *old_access,
86                                                struct _WapiFileShare **info);
87 extern void _wapi_handle_check_share (struct _WapiFileShare *share_info,
88                                       int fd);
89 extern void _wapi_handle_dump (void);
90 extern void _wapi_handle_foreach (WapiHandleType type,
91                                         gboolean (*on_each)(gpointer test, gpointer user),
92                                         gpointer user_data);
93 void _wapi_free_share_info (_WapiFileShare *share_info);
94
95 /* This is OK to use for atomic writes of individual struct members, as they
96  * are independent
97  */
98 #define WAPI_SHARED_HANDLE_DATA(handle) _wapi_shared_layout->handles[_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT((handle))).u.shared.offset]
99
100 #define WAPI_SHARED_HANDLE_TYPED_DATA(handle, type) _wapi_shared_layout->handles[_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT((handle))).u.shared.offset].u.type
101
102 static inline WapiHandleType _wapi_handle_type (gpointer handle)
103 {
104         guint32 idx = GPOINTER_TO_UINT(handle);
105         
106         if (!_WAPI_PRIVATE_VALID_SLOT (idx) || !_WAPI_PRIVATE_HAVE_SLOT (idx)) {
107                 return(WAPI_HANDLE_UNUSED);     /* An impossible type */
108         }
109         
110         return(_WAPI_PRIVATE_HANDLES(idx).type);
111 }
112
113 static inline void _wapi_handle_set_signal_state (gpointer handle,
114                                                   gboolean state,
115                                                   gboolean broadcast)
116 {
117         guint32 idx = GPOINTER_TO_UINT(handle);
118         struct _WapiHandleUnshared *handle_data;
119         int thr_ret;
120
121         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
122                 return;
123         }
124         
125         g_assert (!_WAPI_SHARED_HANDLE(_wapi_handle_type (handle)));
126         
127         handle_data = &_WAPI_PRIVATE_HANDLES(idx);
128         
129 #ifdef DEBUG
130         g_message ("%s: setting state of %p to %s (broadcast %s)", __func__,
131                    handle, state?"TRUE":"FALSE", broadcast?"TRUE":"FALSE");
132 #endif
133
134         if (state == TRUE) {
135                 /* Tell everyone blocking on a single handle */
136
137                 /* The condition the global signal cond is waiting on is the signalling of
138                  * _any_ handle. So lock it before setting the signalled state.
139                  */
140                 thr_ret = mono_os_mutex_lock (_wapi_global_signal_mutex);
141                 if (thr_ret != 0)
142                         g_warning ("Bad call to mono_os_mutex_lock result %d for global signal mutex", thr_ret);
143                 g_assert (thr_ret == 0);
144
145                 /* This function _must_ be called with
146                  * handle->signal_mutex locked
147                  */
148                 handle_data->signalled=state;
149                 
150                 if (broadcast == TRUE) {
151                         thr_ret = pthread_cond_broadcast (&handle_data->signal_cond);
152                         if (thr_ret != 0)
153                                 g_warning ("Bad call to pthread_cond_broadcast result %d for handle %p", thr_ret, handle);
154                         g_assert (thr_ret == 0);
155                 } else {
156                         thr_ret = pthread_cond_signal (&handle_data->signal_cond);
157                         if (thr_ret != 0)
158                                 g_warning ("Bad call to pthread_cond_signal result %d for handle %p", thr_ret, handle);
159                         g_assert (thr_ret == 0);
160                 }
161
162                 /* Tell everyone blocking on multiple handles that something
163                  * was signalled
164                  */                     
165                 thr_ret = pthread_cond_broadcast (_wapi_global_signal_cond);
166                 if (thr_ret != 0)
167                         g_warning ("Bad call to pthread_cond_broadcast result %d for handle %p", thr_ret, handle);
168                 g_assert (thr_ret == 0);
169                         
170                 thr_ret = mono_os_mutex_unlock (_wapi_global_signal_mutex);
171                 if (thr_ret != 0)
172                         g_warning ("Bad call to mono_os_mutex_unlock result %d for global signal mutex", thr_ret);
173                 g_assert (thr_ret == 0);
174         } else {
175                 handle_data->signalled=state;
176         }
177 }
178
179 static inline void _wapi_shared_handle_set_signal_state (gpointer handle,
180                                                          gboolean state)
181 {
182         guint32 idx = GPOINTER_TO_UINT(handle);
183         struct _WapiHandleUnshared *handle_data;
184         struct _WapiHandle_shared_ref *ref;
185         struct _WapiHandleShared *shared_data;
186         
187         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
188                 return;
189         }
190         
191         g_assert (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle)));
192         
193         handle_data = &_WAPI_PRIVATE_HANDLES(idx);
194         
195         ref = &handle_data->u.shared;
196         shared_data = &_wapi_shared_layout->handles[ref->offset];
197         shared_data->signalled = state;
198
199 #ifdef DEBUG
200         g_message ("%s: signalled shared handle offset 0x%x", __func__,
201                    ref->offset);
202 #endif
203 }
204
205 static inline gboolean _wapi_handle_issignalled (gpointer handle)
206 {
207         guint32 idx = GPOINTER_TO_UINT(handle);
208         
209         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
210                 return(FALSE);
211         }
212         
213         if (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle))) {
214                 return(WAPI_SHARED_HANDLE_DATA(handle).signalled);
215         } else {
216                 return(_WAPI_PRIVATE_HANDLES(idx).signalled);
217         }
218 }
219
220 static inline int _wapi_handle_lock_signal_mutex (void)
221 {
222 #ifdef DEBUG
223         g_message ("%s: lock global signal mutex", __func__);
224 #endif
225
226         return(mono_os_mutex_lock (_wapi_global_signal_mutex));
227 }
228
229 /* the parameter makes it easier to call from a pthread cleanup handler */
230 static inline int _wapi_handle_unlock_signal_mutex (void *unused)
231 {
232 #ifdef DEBUG
233         g_message ("%s: unlock global signal mutex", __func__);
234 #endif
235
236         return(mono_os_mutex_unlock (_wapi_global_signal_mutex));
237 }
238
239 static inline int _wapi_handle_lock_handle (gpointer handle)
240 {
241         guint32 idx = GPOINTER_TO_UINT(handle);
242         
243 #ifdef DEBUG
244         g_message ("%s: locking handle %p", __func__, handle);
245 #endif
246
247         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
248                 return(0);
249         }
250         
251         _wapi_handle_ref (handle);
252         
253         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
254                 return(0);
255         }
256         
257         return(mono_os_mutex_lock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex));
258 }
259
260 static inline int _wapi_handle_trylock_handle (gpointer handle)
261 {
262         guint32 idx = GPOINTER_TO_UINT(handle);
263         int ret;
264         
265 #ifdef DEBUG
266         g_message ("%s: locking handle %p", __func__, handle);
267 #endif
268
269         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
270                 return(0);
271         }
272         
273         _wapi_handle_ref (handle);
274         
275         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
276                 return(0);
277         }
278
279         ret = mono_os_mutex_trylock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
280         if (ret != 0) {
281                 _wapi_handle_unref (handle);
282         }
283         
284         return(ret);
285 }
286
287 static inline int _wapi_handle_unlock_handle (gpointer handle)
288 {
289         guint32 idx = GPOINTER_TO_UINT(handle);
290         int ret;
291         
292 #ifdef DEBUG
293         g_message ("%s: unlocking handle %p", __func__, handle);
294 #endif
295         
296         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
297                 return(0);
298         }
299         
300         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
301                 _wapi_handle_unref (handle);
302                 return(0);
303         }
304         
305         ret = mono_os_mutex_unlock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
306
307         _wapi_handle_unref (handle);
308         
309         return(ret);
310 }
311
312 static inline void _wapi_handle_spin (guint32 ms)
313 {
314         struct timespec sleepytime;
315         
316         g_assert (ms < 1000);
317         
318         sleepytime.tv_sec = 0;
319         sleepytime.tv_nsec = ms * 1000000;
320         
321         nanosleep (&sleepytime, NULL);
322 }
323
324 static inline int _wapi_handle_lock_shared_handles (void)
325 {
326         return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_SHARED_HANDLES));
327 }
328
329 static inline int _wapi_handle_trylock_shared_handles (void)
330 {
331         return(_wapi_shm_sem_trylock (_WAPI_SHARED_SEM_SHARED_HANDLES));
332 }
333
334 static inline int _wapi_handle_unlock_shared_handles (void)
335 {
336         return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_SHARED_HANDLES));
337 }
338
339 static inline int _wapi_namespace_lock (void)
340 {
341         return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_NAMESPACE));
342 }
343
344 /* This signature makes it easier to use in pthread cleanup handlers */
345 static inline int _wapi_namespace_unlock (gpointer data G_GNUC_UNUSED)
346 {
347         return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_NAMESPACE));
348 }
349
350 static inline void _wapi_handle_share_release (struct _WapiFileShare *info)
351 {
352         int thr_ret;
353
354         g_assert (info->handle_refs > 0);
355         
356         /* Prevent new entries racing with us */
357         thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
358         g_assert(thr_ret == 0);
359
360         if (InterlockedDecrement ((gint32 *)&info->handle_refs) == 0) {
361                 _wapi_free_share_info (info);
362         }
363
364         thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
365 }
366
367 #endif /* _WAPI_HANDLES_PRIVATE_H_ */