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