2005-09-27 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 Ximian, 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
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
24 #define _WAPI_PRIVATE_MAX_SLOTS         1024
25 #define _WAPI_PRIVATE_HANDLES(x) (_wapi_private_handles [x / _WAPI_HANDLE_INITIAL_COUNT][x % _WAPI_HANDLE_INITIAL_COUNT])
26 #define _WAPI_PRIVATE_HAVE_SLOT(x) ((GPOINTER_TO_UINT (x) / _WAPI_PRIVATE_MAX_SLOTS) < _WAPI_PRIVATE_MAX_SLOTS && \
27                                         _wapi_private_handles [GPOINTER_TO_UINT (x) / _WAPI_HANDLE_INITIAL_COUNT] != NULL)
28 #undef DEBUG
29
30 extern struct _WapiHandleUnshared *_wapi_private_handles [];
31 extern struct _WapiHandleSharedLayout *_wapi_shared_layout;
32 extern struct _WapiFileShareLayout *_wapi_fileshare_layout;
33
34 extern guint32 _wapi_fd_reserve;
35 extern mono_mutex_t _wapi_global_signal_mutex;
36 extern pthread_cond_t _wapi_global_signal_cond;
37 extern int _wapi_sem_id;
38
39 extern gpointer _wapi_handle_new (WapiHandleType type,
40                                   gpointer handle_specific);
41 extern gpointer _wapi_handle_new_fd (WapiHandleType type, int fd,
42                                      gpointer handle_specific);
43 extern gpointer _wapi_handle_new_from_offset (WapiHandleType type,
44                                               guint32 offset);
45 extern gboolean _wapi_lookup_handle (gpointer handle, WapiHandleType type,
46                                      gpointer *handle_specific);
47 extern gboolean _wapi_copy_handle (gpointer handle, WapiHandleType type,
48                                    struct _WapiHandleShared *handle_specific);
49 extern gboolean _wapi_replace_handle (gpointer handle, WapiHandleType type,
50                                   struct _WapiHandleShared *handle_specific);
51 extern gpointer _wapi_search_handle (WapiHandleType type,
52                                      gboolean (*check)(gpointer, gpointer),
53                                      gpointer user_data,
54                                      gpointer *handle_specific);
55 extern gint32 _wapi_search_handle_namespace (WapiHandleType type,
56                                              gchar *utf8_name);
57 extern void _wapi_handle_ref (gpointer handle);
58 extern void _wapi_handle_unref (gpointer handle);
59 extern void _wapi_handle_register_capabilities (WapiHandleType type,
60                                                 WapiHandleCapability caps);
61 extern gboolean _wapi_handle_test_capabilities (gpointer handle,
62                                                 WapiHandleCapability caps);
63 extern void _wapi_handle_ops_close (gpointer handle, gpointer data);
64 extern void _wapi_handle_ops_signal (gpointer handle);
65 extern gboolean _wapi_handle_ops_own (gpointer handle);
66 extern gboolean _wapi_handle_ops_isowned (gpointer handle);
67 extern guint32 _wapi_handle_ops_special_wait (gpointer handle,
68                                               guint32 timeout);
69
70 extern gboolean _wapi_handle_count_signalled_handles (guint32 numhandles,
71                                                       gpointer *handles,
72                                                       gboolean waitall,
73                                                       guint32 *retcount,
74                                                       guint32 *lowest);
75 extern void _wapi_handle_unlock_handles (guint32 numhandles,
76                                          gpointer *handles);
77 extern int _wapi_handle_wait_signal (void);
78 extern int _wapi_handle_timedwait_signal (struct timespec *timeout);
79 extern int _wapi_handle_wait_signal_poll_share (void);
80 extern int _wapi_handle_timedwait_signal_poll_share (struct timespec *timeout);
81 extern int _wapi_handle_wait_signal_handle (gpointer handle);
82 extern int _wapi_handle_timedwait_signal_handle (gpointer handle,
83                                                  struct timespec *timeout);
84 extern gboolean _wapi_handle_get_or_set_share (dev_t device, ino_t 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
98 /* This is OK to use for atomic writes of individual struct members, as they
99  * are independent
100  */
101 #define WAPI_SHARED_HANDLE_METADATA(handle) _wapi_shared_layout->metadata[_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT((handle))).u.shared.offset]
102
103 /* Use this for read-only accesses to shared handle structs, so that
104  * if a handle is updated we always look at the latest copy.  For
105  * write access, use handle_copy/handle_replace so that all the
106  * handle-type-specific data is updated atomically.
107  */
108 #define WAPI_SHARED_HANDLE_TYPED_DATA(handle, type) _wapi_shared_layout->handles[_wapi_shared_layout->metadata[_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT((handle))).u.shared.offset].offset].u.type
109
110 static inline WapiHandleType _wapi_handle_type (gpointer handle)
111 {
112         guint32 idx = GPOINTER_TO_UINT(handle);
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         g_assert (!_WAPI_SHARED_HANDLE(_wapi_handle_type (handle)));
126         
127         handle_data = &_WAPI_PRIVATE_HANDLES(idx);
128         
129 #ifdef DEBUG
130         g_message ("%s: setting state of %p to %s (broadcast %s)", __func__,
131                    handle, state?"TRUE":"FALSE", broadcast?"TRUE":"FALSE");
132 #endif
133
134         if (state == TRUE) {
135                 /* Tell everyone blocking on a single handle */
136
137                 /* This function _must_ be called with
138                  * handle->signal_mutex locked
139                  */
140                 handle_data->signalled=state;
141                 
142                 if (broadcast == TRUE) {
143                         thr_ret = pthread_cond_broadcast (&handle_data->signal_cond);
144                         g_assert (thr_ret == 0);
145                 } else {
146                         thr_ret = pthread_cond_signal (&handle_data->signal_cond);
147                         g_assert (thr_ret == 0);
148                 }
149
150                 /* Tell everyone blocking on multiple handles that something
151                  * was signalled
152                  */
153                 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup, (void *)&_wapi_global_signal_mutex);
154                 thr_ret = mono_mutex_lock (&_wapi_global_signal_mutex);
155                 g_assert (thr_ret == 0);
156                         
157                 thr_ret = pthread_cond_broadcast (&_wapi_global_signal_cond);
158                 g_assert (thr_ret == 0);
159                         
160                 thr_ret = mono_mutex_unlock (&_wapi_global_signal_mutex);
161                 g_assert (thr_ret == 0);
162                 pthread_cleanup_pop (0);
163         } else {
164                 handle_data->signalled=state;
165         }
166 }
167
168 static inline void _wapi_shared_handle_set_signal_state (gpointer handle,
169                                                          gboolean state)
170 {
171         guint32 idx = GPOINTER_TO_UINT(handle);
172         struct _WapiHandleUnshared *handle_data;
173         struct _WapiHandle_shared_ref *ref;
174         struct _WapiHandleSharedMetadata *shared_meta;
175         
176         g_assert (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle)));
177         
178         handle_data = &_WAPI_PRIVATE_HANDLES(idx);
179         
180         ref = &handle_data->u.shared;
181         shared_meta = &_wapi_shared_layout->metadata[ref->offset];
182         shared_meta->signalled = state;
183
184 #ifdef DEBUG
185         g_message ("%s: signalled shared handle offset 0x%x", __func__,
186                    ref->offset);
187 #endif
188
189         InterlockedIncrement (&_wapi_shared_layout->signal_count);
190 }
191
192 static inline gboolean _wapi_handle_issignalled (gpointer handle)
193 {
194         guint32 idx = GPOINTER_TO_UINT(handle);
195         
196         if (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle))) {
197                 return(WAPI_SHARED_HANDLE_METADATA(handle).signalled);
198         } else {
199                 return(_WAPI_PRIVATE_HANDLES(idx).signalled);
200         }
201 }
202
203 static inline int _wapi_handle_lock_signal_mutex (void)
204 {
205 #ifdef DEBUG
206         g_message ("%s: lock global signal mutex", __func__);
207 #endif
208
209         return(mono_mutex_lock (&_wapi_global_signal_mutex));
210 }
211
212 /* the parameter makes it easier to call from a pthread cleanup handler */
213 static inline int _wapi_handle_unlock_signal_mutex (void *unused)
214 {
215 #ifdef DEBUG
216         g_message ("%s: unlock global signal mutex", __func__);
217 #endif
218
219         return(mono_mutex_unlock (&_wapi_global_signal_mutex));
220 }
221
222 static inline int _wapi_handle_lock_handle (gpointer handle)
223 {
224         guint32 idx = GPOINTER_TO_UINT(handle);
225         
226 #ifdef DEBUG
227         g_message ("%s: locking handle %p", __func__, handle);
228 #endif
229
230         _wapi_handle_ref (handle);
231         
232         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
233                 return(0);
234         }
235         
236         return(mono_mutex_lock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex));
237 }
238
239 static inline int _wapi_handle_trylock_handle (gpointer handle)
240 {
241         guint32 idx = GPOINTER_TO_UINT(handle);
242         
243 #ifdef DEBUG
244         g_message ("%s: locking handle %p", __func__, handle);
245 #endif
246
247         _wapi_handle_ref (handle);
248         
249         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
250                 return(0);
251         }
252
253         return(mono_mutex_trylock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex));
254 }
255
256 static inline int _wapi_handle_unlock_handle (gpointer handle)
257 {
258         guint32 idx = GPOINTER_TO_UINT(handle);
259         int ret;
260         
261 #ifdef DEBUG
262         g_message ("%s: unlocking handle %p", __func__, handle);
263 #endif
264         
265         if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
266                 _wapi_handle_unref (handle);
267                 return(0);
268         }
269         
270         ret = mono_mutex_unlock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
271
272         _wapi_handle_unref (handle);
273         
274         return(ret);
275 }
276
277 static inline void _wapi_handle_spin (guint32 ms)
278 {
279         struct timespec sleepytime;
280         
281         g_assert (ms < 1000);
282         
283         sleepytime.tv_sec = 0;
284         sleepytime.tv_nsec = ms * 1000000;
285         
286         nanosleep (&sleepytime, NULL);
287 }
288
289 static inline int _wapi_handle_lock_shared_handles (void)
290 {
291         return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_HANDLE));
292 }
293
294 static inline int _wapi_handle_trylock_shared_handles (void)
295 {
296         return(_wapi_shm_sem_trylock (_WAPI_SHARED_SEM_HANDLE));
297 }
298
299 static inline int _wapi_handle_unlock_shared_handles (void)
300 {
301         return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_HANDLE));
302 }
303
304 static inline int _wapi_namespace_lock (void)
305 {
306         return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_NAMESPACE));
307 }
308
309 /* This signature makes it easier to use in pthread cleanup handlers */
310 static inline int _wapi_namespace_unlock (gpointer data G_GNUC_UNUSED)
311 {
312         return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_NAMESPACE));
313 }
314
315 static inline void _wapi_handle_share_release (struct _WapiFileShare *info)
316 {
317         int thr_ret;
318         
319         g_assert (info->handle_refs > 0);
320         
321         /* Prevent new entries racing with us */
322         thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_SHARE);
323         g_assert(thr_ret == 0);
324
325         if (InterlockedDecrement (&info->handle_refs) == 0) {
326                 memset (info, '\0', sizeof(struct _WapiFileShare));
327         }
328
329         thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_SHARE);
330 }
331
332 #endif /* _WAPI_HANDLES_PRIVATE_H_ */