2007-09-02 Zoltan Varga <vargaz@gmail.com>
[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
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 (void);
82 extern int _wapi_handle_timedwait_signal (struct timespec *timeout);
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);
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                 /* This function _must_ be called with
143                  * handle->signal_mutex locked
144                  */
145                 handle_data->signalled=state;
146                 
147                 if (broadcast == TRUE) {
148                         thr_ret = pthread_cond_broadcast (&handle_data->signal_cond);
149                         g_assert (thr_ret == 0);
150                 } else {
151                         thr_ret = pthread_cond_signal (&handle_data->signal_cond);
152                         g_assert (thr_ret == 0);
153                 }
154
155                 /* Tell everyone blocking on multiple handles that something
156                  * was signalled
157                  */
158                 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup, (void *)&_wapi_global_signal_mutex);
159                 thr_ret = mono_mutex_lock (&_wapi_global_signal_mutex);
160                 g_assert (thr_ret == 0);
161                         
162                 thr_ret = pthread_cond_broadcast (&_wapi_global_signal_cond);
163                 g_assert (thr_ret == 0);
164                         
165                 thr_ret = mono_mutex_unlock (&_wapi_global_signal_mutex);
166                 g_assert (thr_ret == 0);
167                 pthread_cleanup_pop (0);
168         } else {
169                 handle_data->signalled=state;
170         }
171 }
172
173 static inline void _wapi_shared_handle_set_signal_state (gpointer handle,
174                                                          gboolean state)
175 {
176         guint32 idx = GPOINTER_TO_UINT(handle);
177         struct _WapiHandleUnshared *handle_data;
178         struct _WapiHandle_shared_ref *ref;
179         struct _WapiHandleShared *shared_data;
180         
181         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
182                 return;
183         }
184         
185         g_assert (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle)));
186         
187         handle_data = &_WAPI_PRIVATE_HANDLES(idx);
188         
189         ref = &handle_data->u.shared;
190         shared_data = &_wapi_shared_layout->handles[ref->offset];
191         shared_data->signalled = state;
192
193 #ifdef DEBUG
194         g_message ("%s: signalled shared handle offset 0x%x", __func__,
195                    ref->offset);
196 #endif
197 }
198
199 static inline gboolean _wapi_handle_issignalled (gpointer handle)
200 {
201         guint32 idx = GPOINTER_TO_UINT(handle);
202         
203         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
204                 return(FALSE);
205         }
206         
207         if (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle))) {
208                 return(WAPI_SHARED_HANDLE_DATA(handle).signalled);
209         } else {
210                 return(_WAPI_PRIVATE_HANDLES(idx).signalled);
211         }
212 }
213
214 static inline int _wapi_handle_lock_signal_mutex (void)
215 {
216 #ifdef DEBUG
217         g_message ("%s: lock global signal mutex", __func__);
218 #endif
219
220         return(mono_mutex_lock (&_wapi_global_signal_mutex));
221 }
222
223 /* the parameter makes it easier to call from a pthread cleanup handler */
224 static inline int _wapi_handle_unlock_signal_mutex (void *unused)
225 {
226 #ifdef DEBUG
227         g_message ("%s: unlock global signal mutex", __func__);
228 #endif
229
230         return(mono_mutex_unlock (&_wapi_global_signal_mutex));
231 }
232
233 static inline int _wapi_handle_lock_handle (gpointer handle)
234 {
235         guint32 idx = GPOINTER_TO_UINT(handle);
236         
237 #ifdef DEBUG
238         g_message ("%s: locking handle %p", __func__, handle);
239 #endif
240
241         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
242                 return(0);
243         }
244         
245         _wapi_handle_ref (handle);
246         
247         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
248                 return(0);
249         }
250         
251         return(mono_mutex_lock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex));
252 }
253
254 static inline int _wapi_handle_trylock_handle (gpointer handle)
255 {
256         guint32 idx = GPOINTER_TO_UINT(handle);
257         int ret;
258         
259 #ifdef DEBUG
260         g_message ("%s: locking handle %p", __func__, handle);
261 #endif
262
263         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
264                 return(0);
265         }
266         
267         _wapi_handle_ref (handle);
268         
269         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
270                 return(0);
271         }
272
273         ret = mono_mutex_trylock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
274         if (ret != 0) {
275                 _wapi_handle_unref (handle);
276         }
277         
278         return(ret);
279 }
280
281 static inline int _wapi_handle_unlock_handle (gpointer handle)
282 {
283         guint32 idx = GPOINTER_TO_UINT(handle);
284         int ret;
285         
286 #ifdef DEBUG
287         g_message ("%s: unlocking handle %p", __func__, handle);
288 #endif
289         
290         if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
291                 return(0);
292         }
293         
294         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
295                 _wapi_handle_unref (handle);
296                 return(0);
297         }
298         
299         ret = mono_mutex_unlock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
300
301         _wapi_handle_unref (handle);
302         
303         return(ret);
304 }
305
306 static inline void _wapi_handle_spin (guint32 ms)
307 {
308         struct timespec sleepytime;
309         
310         g_assert (ms < 1000);
311         
312         sleepytime.tv_sec = 0;
313         sleepytime.tv_nsec = ms * 1000000;
314         
315         nanosleep (&sleepytime, NULL);
316 }
317
318 static inline int _wapi_handle_lock_shared_handles (void)
319 {
320         return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_SHARED_HANDLES));
321 }
322
323 static inline int _wapi_handle_trylock_shared_handles (void)
324 {
325         return(_wapi_shm_sem_trylock (_WAPI_SHARED_SEM_SHARED_HANDLES));
326 }
327
328 static inline int _wapi_handle_unlock_shared_handles (void)
329 {
330         return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_SHARED_HANDLES));
331 }
332
333 static inline int _wapi_namespace_lock (void)
334 {
335         return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_NAMESPACE));
336 }
337
338 /* This signature makes it easier to use in pthread cleanup handlers */
339 static inline int _wapi_namespace_unlock (gpointer data G_GNUC_UNUSED)
340 {
341         return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_NAMESPACE));
342 }
343
344 static inline void _wapi_handle_share_release (struct _WapiFileShare *info)
345 {
346         int thr_ret;
347         
348         g_assert (info->handle_refs > 0);
349         
350         /* Prevent new entries racing with us */
351         thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
352         g_assert(thr_ret == 0);
353
354         if (InterlockedDecrement ((gint32 *)&info->handle_refs) == 0) {
355                 memset (info, '\0', sizeof(struct _WapiFileShare));
356         }
357
358         thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
359 }
360
361 #endif /* _WAPI_HANDLES_PRIVATE_H_ */