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