2 * handles-private.h: Internal operations on handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2006 Novell, Inc.
10 #ifndef _WAPI_HANDLES_PRIVATE_H_
11 #define _WAPI_HANDLES_PRIVATE_H_
17 #include <sys/types.h>
19 #include <mono/io-layer/wapi-private.h>
20 #include <mono/io-layer/shared.h>
21 #include <mono/utils/atomic.h>
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)
31 extern struct _WapiHandleUnshared *_wapi_private_handles [];
32 extern struct _WapiHandleSharedLayout *_wapi_shared_layout;
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 extern gboolean _wapi_has_shut_down;
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 gpointer _wapi_handle_new_from_offset (WapiHandleType type,
48 extern gboolean _wapi_lookup_handle (gpointer handle, WapiHandleType type,
49 gpointer *handle_specific);
50 extern gpointer _wapi_search_handle (WapiHandleType type,
51 gboolean (*check)(gpointer, gpointer),
53 gpointer *handle_specific,
54 gboolean search_shared);
55 extern gint32 _wapi_search_handle_namespace (WapiHandleType type,
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,
70 extern void _wapi_handle_ops_prewait (gpointer handle);
72 extern gboolean _wapi_handle_count_signalled_handles (guint32 numhandles,
77 extern void _wapi_handle_unlock_handles (guint32 numhandles,
79 extern int _wapi_handle_timedwait_signal (guint32 timeout, gboolean poll, gboolean *alerted);
80 extern int _wapi_handle_timedwait_signal_handle (gpointer handle, guint32 timeout, gboolean alertable, gboolean poll, gboolean *alerted);
81 extern gboolean _wapi_handle_get_or_set_share (guint64 device, guint64 inode,
82 guint32 new_sharemode,
84 guint32 *old_sharemode,
86 struct _WapiFileShare **info);
87 extern void _wapi_handle_check_share (struct _WapiFileShare *share_info,
89 extern void _wapi_handle_dump (void);
90 extern void _wapi_handle_foreach (WapiHandleType type,
91 gboolean (*on_each)(gpointer test, gpointer user),
93 void _wapi_free_share_info (_WapiFileShare *share_info);
95 /* This is OK to use for atomic writes of individual struct members, as they
98 #define WAPI_SHARED_HANDLE_DATA(handle) _wapi_shared_layout->handles[_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT((handle))).u.shared.offset]
100 #define WAPI_SHARED_HANDLE_TYPED_DATA(handle, type) _wapi_shared_layout->handles[_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT((handle))).u.shared.offset].u.type
102 static inline WapiHandleType _wapi_handle_type (gpointer handle)
104 guint32 idx = GPOINTER_TO_UINT(handle);
106 if (!_WAPI_PRIVATE_VALID_SLOT (idx) || !_WAPI_PRIVATE_HAVE_SLOT (idx)) {
107 return(WAPI_HANDLE_UNUSED); /* An impossible type */
110 return(_WAPI_PRIVATE_HANDLES(idx).type);
113 static inline void _wapi_handle_set_signal_state (gpointer handle,
117 guint32 idx = GPOINTER_TO_UINT(handle);
118 struct _WapiHandleUnshared *handle_data;
121 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
125 g_assert (!_WAPI_SHARED_HANDLE(_wapi_handle_type (handle)));
127 handle_data = &_WAPI_PRIVATE_HANDLES(idx);
130 g_message ("%s: setting state of %p to %s (broadcast %s)", __func__,
131 handle, state?"TRUE":"FALSE", broadcast?"TRUE":"FALSE");
135 /* Tell everyone blocking on a single handle */
137 /* The condition the global signal cond is waiting on is the signalling of
138 * _any_ handle. So lock it before setting the signalled state.
140 thr_ret = mono_os_mutex_lock (_wapi_global_signal_mutex);
142 g_warning ("Bad call to mono_os_mutex_lock result %d for global signal mutex", thr_ret);
143 g_assert (thr_ret == 0);
145 /* This function _must_ be called with
146 * handle->signal_mutex locked
148 handle_data->signalled=state;
150 if (broadcast == TRUE) {
151 thr_ret = pthread_cond_broadcast (&handle_data->signal_cond);
153 g_warning ("Bad call to pthread_cond_broadcast result %d for handle %p", thr_ret, handle);
154 g_assert (thr_ret == 0);
156 thr_ret = pthread_cond_signal (&handle_data->signal_cond);
158 g_warning ("Bad call to pthread_cond_signal result %d for handle %p", thr_ret, handle);
159 g_assert (thr_ret == 0);
162 /* Tell everyone blocking on multiple handles that something
165 thr_ret = pthread_cond_broadcast (_wapi_global_signal_cond);
167 g_warning ("Bad call to pthread_cond_broadcast result %d for handle %p", thr_ret, handle);
168 g_assert (thr_ret == 0);
170 thr_ret = mono_os_mutex_unlock (_wapi_global_signal_mutex);
172 g_warning ("Bad call to mono_os_mutex_unlock result %d for global signal mutex", thr_ret);
173 g_assert (thr_ret == 0);
175 handle_data->signalled=state;
179 static inline void _wapi_shared_handle_set_signal_state (gpointer handle,
182 guint32 idx = GPOINTER_TO_UINT(handle);
183 struct _WapiHandleUnshared *handle_data;
184 struct _WapiHandle_shared_ref *ref;
185 struct _WapiHandleShared *shared_data;
187 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
191 g_assert (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle)));
193 handle_data = &_WAPI_PRIVATE_HANDLES(idx);
195 ref = &handle_data->u.shared;
196 shared_data = &_wapi_shared_layout->handles[ref->offset];
197 shared_data->signalled = state;
200 g_message ("%s: signalled shared handle offset 0x%x", __func__,
205 static inline gboolean _wapi_handle_issignalled (gpointer handle)
207 guint32 idx = GPOINTER_TO_UINT(handle);
209 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
213 if (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle))) {
214 return(WAPI_SHARED_HANDLE_DATA(handle).signalled);
216 return(_WAPI_PRIVATE_HANDLES(idx).signalled);
220 static inline int _wapi_handle_lock_signal_mutex (void)
223 g_message ("%s: lock global signal mutex", __func__);
226 return(mono_os_mutex_lock (_wapi_global_signal_mutex));
229 /* the parameter makes it easier to call from a pthread cleanup handler */
230 static inline int _wapi_handle_unlock_signal_mutex (void *unused)
233 g_message ("%s: unlock global signal mutex", __func__);
236 return(mono_os_mutex_unlock (_wapi_global_signal_mutex));
239 static inline int _wapi_handle_lock_handle (gpointer handle)
241 guint32 idx = GPOINTER_TO_UINT(handle);
244 g_message ("%s: locking handle %p", __func__, handle);
247 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
251 _wapi_handle_ref (handle);
253 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
257 return(mono_os_mutex_lock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex));
260 static inline int _wapi_handle_trylock_handle (gpointer handle)
262 guint32 idx = GPOINTER_TO_UINT(handle);
266 g_message ("%s: locking handle %p", __func__, handle);
269 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
273 _wapi_handle_ref (handle);
275 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
279 ret = mono_os_mutex_trylock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
281 _wapi_handle_unref (handle);
287 static inline int _wapi_handle_unlock_handle (gpointer handle)
289 guint32 idx = GPOINTER_TO_UINT(handle);
293 g_message ("%s: unlocking handle %p", __func__, handle);
296 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
300 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
301 _wapi_handle_unref (handle);
305 ret = mono_os_mutex_unlock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
307 _wapi_handle_unref (handle);
312 static inline void _wapi_handle_spin (guint32 ms)
314 struct timespec sleepytime;
316 g_assert (ms < 1000);
318 sleepytime.tv_sec = 0;
319 sleepytime.tv_nsec = ms * 1000000;
321 nanosleep (&sleepytime, NULL);
324 static inline int _wapi_handle_lock_shared_handles (void)
326 return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_SHARED_HANDLES));
329 static inline int _wapi_handle_trylock_shared_handles (void)
331 return(_wapi_shm_sem_trylock (_WAPI_SHARED_SEM_SHARED_HANDLES));
334 static inline int _wapi_handle_unlock_shared_handles (void)
336 return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_SHARED_HANDLES));
339 static inline int _wapi_namespace_lock (void)
341 return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_NAMESPACE));
344 /* This signature makes it easier to use in pthread cleanup handlers */
345 static inline int _wapi_namespace_unlock (gpointer data G_GNUC_UNUSED)
347 return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_NAMESPACE));
350 static inline void _wapi_handle_share_release (struct _WapiFileShare *info)
354 g_assert (info->handle_refs > 0);
356 /* Prevent new entries racing with us */
357 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
358 g_assert(thr_ret == 0);
360 if (InterlockedDecrement ((gint32 *)&info->handle_refs) == 0) {
361 _wapi_free_share_info (info);
364 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
367 #endif /* _WAPI_HANDLES_PRIVATE_H_ */