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