Normalize line endings.
[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 <signal.h>
17 #include <string.h>
18 #include <sys/types.h>
19
20 #include <mono/io-layer/wapi-private.h>
21 #include <mono/io-layer/misc-private.h>
22 #include <mono/io-layer/collection.h>
23 #include <mono/io-layer/shared.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 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_wait_signal (gboolean poll);
82 extern int _wapi_handle_timedwait_signal (struct timespec *timeout, gboolean poll);
83 extern int _wapi_handle_wait_signal_handle (gpointer handle, gboolean alertable);
84 extern int _wapi_handle_timedwait_signal_handle (gpointer handle,
85                                                                                                  struct timespec *timeout, gboolean alertable, gboolean poll);
86 extern gboolean _wapi_handle_get_or_set_share (dev_t device, ino_t inode,
87                                                guint32 new_sharemode,
88                                                guint32 new_access,
89                                                guint32 *old_sharemode,
90                                                guint32 *old_access,
91                                                struct _WapiFileShare **info);
92 extern void _wapi_handle_check_share (struct _WapiFileShare *share_info,
93                                       int fd);
94 extern void _wapi_handle_dump (void);
95 extern void _wapi_handle_update_refs (void);
96 extern void _wapi_handle_foreach (WapiHandleType type,
97                                         gboolean (*on_each)(gpointer test, gpointer user),
98                                         gpointer user_data);
99
100 /* This is OK to use for atomic writes of individual struct members, as they
101  * are independent
102  */
103 #define WAPI_SHARED_HANDLE_DATA(handle) _wapi_shared_layout->handles[_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT((handle))).u.shared.offset]
104
105 #define WAPI_SHARED_HANDLE_TYPED_DATA(handle, type) _wapi_shared_layout->handles[_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT((handle))).u.shared.offset].u.type
106
107 static inline WapiHandleType _wapi_handle_type (gpointer handle)
108 {
109         guint32 idx = GPOINTER_TO_UINT(handle);
110         
111         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
112                 return(WAPI_HANDLE_COUNT);      /* An impossible type */
113         }
114         
115         return(_WAPI_PRIVATE_HANDLES(idx).type);
116 }
117
118 static inline void _wapi_handle_set_signal_state (gpointer handle,
119                                                   gboolean state,
120                                                   gboolean broadcast)
121 {
122         guint32 idx = GPOINTER_TO_UINT(handle);
123         struct _WapiHandleUnshared *handle_data;
124         int thr_ret;
125
126         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
127                 return;
128         }
129         
130         g_assert (!_WAPI_SHARED_HANDLE(_wapi_handle_type (handle)));
131         
132         handle_data = &_WAPI_PRIVATE_HANDLES(idx);
133         
134 #ifdef DEBUG
135         g_message ("%s: setting state of %p to %s (broadcast %s)", __func__,
136                    handle, state?"TRUE":"FALSE", broadcast?"TRUE":"FALSE");
137 #endif
138
139         if (state == TRUE) {
140                 /* Tell everyone blocking on a single handle */
141
142                 /* The condition the global signal cond is waiting on is the signalling of
143                  * _any_ handle. So lock it before setting the signalled state.
144                  */
145                 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup, (void *)_wapi_global_signal_mutex);
146                 thr_ret = mono_mutex_lock (_wapi_global_signal_mutex);
147                 if (thr_ret != 0)
148                         g_warning ("Bad call to mono_mutex_lock result %d for global signal mutex", thr_ret);
149                 g_assert (thr_ret == 0);
150
151                 /* This function _must_ be called with
152                  * handle->signal_mutex locked
153                  */
154                 handle_data->signalled=state;
155                 
156                 if (broadcast == TRUE) {
157                         thr_ret = pthread_cond_broadcast (&handle_data->signal_cond);
158                         if (thr_ret != 0)
159                                 g_warning ("Bad call to pthread_cond_broadcast result %d for handle %p", thr_ret, handle);
160                         g_assert (thr_ret == 0);
161                 } else {
162                         thr_ret = pthread_cond_signal (&handle_data->signal_cond);
163                         if (thr_ret != 0)
164                                 g_warning ("Bad call to pthread_cond_signal result %d for handle %p", thr_ret, handle);
165                         g_assert (thr_ret == 0);
166                 }
167
168                 /* Tell everyone blocking on multiple handles that something
169                  * was signalled
170                  */                     
171                 thr_ret = pthread_cond_broadcast (_wapi_global_signal_cond);
172                 if (thr_ret != 0)
173                         g_warning ("Bad call to pthread_cond_broadcast result %d for handle %p", thr_ret, handle);
174                 g_assert (thr_ret == 0);
175                         
176                 thr_ret = mono_mutex_unlock (_wapi_global_signal_mutex);
177                 if (thr_ret != 0)
178                         g_warning ("Bad call to mono_mutex_unlock result %d for global signal mutex", thr_ret);
179                 g_assert (thr_ret == 0);
180
181                 pthread_cleanup_pop (0);
182         } else {
183                 handle_data->signalled=state;
184         }
185 }
186
187 static inline void _wapi_shared_handle_set_signal_state (gpointer handle,
188                                                          gboolean state)
189 {
190         guint32 idx = GPOINTER_TO_UINT(handle);
191         struct _WapiHandleUnshared *handle_data;
192         struct _WapiHandle_shared_ref *ref;
193         struct _WapiHandleShared *shared_data;
194         
195         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
196                 return;
197         }
198         
199         g_assert (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle)));
200         
201         handle_data = &_WAPI_PRIVATE_HANDLES(idx);
202         
203         ref = &handle_data->u.shared;
204         shared_data = &_wapi_shared_layout->handles[ref->offset];
205         shared_data->signalled = state;
206
207 #ifdef DEBUG
208         g_message ("%s: signalled shared handle offset 0x%x", __func__,
209                    ref->offset);
210 #endif
211 }
212
213 static inline gboolean _wapi_handle_issignalled (gpointer handle)
214 {
215         guint32 idx = GPOINTER_TO_UINT(handle);
216         
217         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
218                 return(FALSE);
219         }
220         
221         if (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle))) {
222                 return(WAPI_SHARED_HANDLE_DATA(handle).signalled);
223         } else {
224                 return(_WAPI_PRIVATE_HANDLES(idx).signalled);
225         }
226 }
227
228 static inline int _wapi_handle_lock_signal_mutex (void)
229 {
230 #ifdef DEBUG
231         g_message ("%s: lock global signal mutex", __func__);
232 #endif
233
234         return(mono_mutex_lock (_wapi_global_signal_mutex));
235 }
236
237 /* the parameter makes it easier to call from a pthread cleanup handler */
238 static inline int _wapi_handle_unlock_signal_mutex (void *unused)
239 {
240 #ifdef DEBUG
241         g_message ("%s: unlock global signal mutex", __func__);
242 #endif
243
244         return(mono_mutex_unlock (_wapi_global_signal_mutex));
245 }
246
247 static inline int _wapi_handle_lock_handle (gpointer handle)
248 {
249         guint32 idx = GPOINTER_TO_UINT(handle);
250         
251 #ifdef DEBUG
252         g_message ("%s: locking handle %p", __func__, handle);
253 #endif
254
255         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
256                 return(0);
257         }
258         
259         _wapi_handle_ref (handle);
260         
261         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
262                 return(0);
263         }
264         
265         return(mono_mutex_lock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex));
266 }
267
268 static inline int _wapi_handle_trylock_handle (gpointer handle)
269 {
270         guint32 idx = GPOINTER_TO_UINT(handle);
271         int ret;
272         
273 #ifdef DEBUG
274         g_message ("%s: locking handle %p", __func__, handle);
275 #endif
276
277         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
278                 return(0);
279         }
280         
281         _wapi_handle_ref (handle);
282         
283         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
284                 return(0);
285         }
286
287         ret = mono_mutex_trylock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
288         if (ret != 0) {
289                 _wapi_handle_unref (handle);
290         }
291         
292         return(ret);
293 }
294
295 static inline int _wapi_handle_unlock_handle (gpointer handle)
296 {
297         guint32 idx = GPOINTER_TO_UINT(handle);
298         int ret;
299         
300 #ifdef DEBUG
301         g_message ("%s: unlocking handle %p", __func__, handle);
302 #endif
303         
304         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
305                 return(0);
306         }
307         
308         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
309                 _wapi_handle_unref (handle);
310                 return(0);
311         }
312         
313         ret = mono_mutex_unlock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
314
315         _wapi_handle_unref (handle);
316         
317         return(ret);
318 }
319
320 static inline void _wapi_handle_spin (guint32 ms)
321 {
322         struct timespec sleepytime;
323         
324         g_assert (ms < 1000);
325         
326         sleepytime.tv_sec = 0;
327         sleepytime.tv_nsec = ms * 1000000;
328         
329         nanosleep (&sleepytime, NULL);
330 }
331
332 static inline int _wapi_handle_lock_shared_handles (void)
333 {
334         return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_SHARED_HANDLES));
335 }
336
337 static inline int _wapi_handle_trylock_shared_handles (void)
338 {
339         return(_wapi_shm_sem_trylock (_WAPI_SHARED_SEM_SHARED_HANDLES));
340 }
341
342 static inline int _wapi_handle_unlock_shared_handles (void)
343 {
344         return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_SHARED_HANDLES));
345 }
346
347 static inline int _wapi_namespace_lock (void)
348 {
349         return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_NAMESPACE));
350 }
351
352 /* This signature makes it easier to use in pthread cleanup handlers */
353 static inline int _wapi_namespace_unlock (gpointer data G_GNUC_UNUSED)
354 {
355         return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_NAMESPACE));
356 }
357
358 static inline void _wapi_handle_share_release (struct _WapiFileShare *info)
359 {
360         int thr_ret;
361         
362         g_assert (info->handle_refs > 0);
363         
364         /* Prevent new entries racing with us */
365         thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
366         g_assert(thr_ret == 0);
367
368         if (InterlockedDecrement ((gint32 *)&info->handle_refs) == 0) {
369                 memset (info, '\0', sizeof(struct _WapiFileShare));
370         }
371
372         thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
373 }
374
375 #endif /* _WAPI_HANDLES_PRIVATE_H_ */