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