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