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 [];
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;
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),
50 gpointer *handle_specific,
51 gboolean search_shared);
52 extern gpointer _wapi_search_handle_namespace (WapiHandleType type,
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,
67 extern void _wapi_handle_ops_prewait (gpointer handle);
69 extern gboolean _wapi_handle_count_signalled_handles (guint32 numhandles,
74 extern void _wapi_handle_unlock_handles (guint32 numhandles,
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,
80 guint32 *old_sharemode,
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),
87 void _wapi_free_share_info (_WapiFileShare *share_info);
89 static inline WapiHandleType _wapi_handle_type (gpointer handle)
91 guint32 idx = GPOINTER_TO_UINT(handle);
93 if (!_WAPI_PRIVATE_VALID_SLOT (idx) || !_WAPI_PRIVATE_HAVE_SLOT (idx)) {
94 return(WAPI_HANDLE_UNUSED); /* An impossible type */
97 return(_WAPI_PRIVATE_HANDLES(idx).type);
100 static inline void _wapi_handle_set_signal_state (gpointer handle,
104 guint32 idx = GPOINTER_TO_UINT(handle);
105 struct _WapiHandleUnshared *handle_data;
108 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
112 handle_data = &_WAPI_PRIVATE_HANDLES(idx);
115 g_message ("%s: setting state of %p to %s (broadcast %s)", __func__,
116 handle, state?"TRUE":"FALSE", broadcast?"TRUE":"FALSE");
120 /* Tell everyone blocking on a single handle */
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.
125 thr_ret = mono_os_mutex_lock (_wapi_global_signal_mutex);
127 g_warning ("Bad call to mono_os_mutex_lock result %d for global signal mutex", thr_ret);
128 g_assert (thr_ret == 0);
130 /* This function _must_ be called with
131 * handle->signal_mutex locked
133 handle_data->signalled=state;
135 if (broadcast == TRUE) {
136 thr_ret = mono_os_cond_broadcast (&handle_data->signal_cond);
138 g_warning ("Bad call to mono_os_cond_broadcast result %d for handle %p", thr_ret, handle);
139 g_assert (thr_ret == 0);
141 thr_ret = mono_os_cond_signal (&handle_data->signal_cond);
143 g_warning ("Bad call to mono_os_cond_signal result %d for handle %p", thr_ret, handle);
144 g_assert (thr_ret == 0);
147 /* Tell everyone blocking on multiple handles that something
150 thr_ret = mono_os_cond_broadcast (_wapi_global_signal_cond);
152 g_warning ("Bad call to mono_os_cond_broadcast result %d for handle %p", thr_ret, handle);
153 g_assert (thr_ret == 0);
155 thr_ret = mono_os_mutex_unlock (_wapi_global_signal_mutex);
157 g_warning ("Bad call to mono_os_mutex_unlock result %d for global signal mutex", thr_ret);
158 g_assert (thr_ret == 0);
160 handle_data->signalled=state;
164 static inline gboolean _wapi_handle_issignalled (gpointer handle)
166 guint32 idx = GPOINTER_TO_UINT(handle);
168 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
172 return _WAPI_PRIVATE_HANDLES (idx).signalled;
175 static inline int _wapi_handle_lock_signal_mutex (void)
178 g_message ("%s: lock global signal mutex", __func__);
181 return(mono_os_mutex_lock (_wapi_global_signal_mutex));
184 /* the parameter makes it easier to call from a pthread cleanup handler */
185 static inline int _wapi_handle_unlock_signal_mutex (void *unused)
188 g_message ("%s: unlock global signal mutex", __func__);
191 return(mono_os_mutex_unlock (_wapi_global_signal_mutex));
194 static inline int _wapi_handle_lock_handle (gpointer handle)
196 guint32 idx = GPOINTER_TO_UINT(handle);
199 g_message ("%s: locking handle %p", __func__, handle);
202 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
206 _wapi_handle_ref (handle);
208 return(mono_os_mutex_lock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex));
211 static inline int _wapi_handle_trylock_handle (gpointer handle)
213 guint32 idx = GPOINTER_TO_UINT(handle);
217 g_message ("%s: locking handle %p", __func__, handle);
220 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
224 _wapi_handle_ref (handle);
226 ret = mono_os_mutex_trylock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
228 _wapi_handle_unref (handle);
234 static inline int _wapi_handle_unlock_handle (gpointer handle)
236 guint32 idx = GPOINTER_TO_UINT(handle);
240 g_message ("%s: unlocking handle %p", __func__, handle);
243 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
247 ret = mono_os_mutex_unlock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
249 _wapi_handle_unref (handle);
254 static inline void _wapi_handle_spin (guint32 ms)
256 struct timespec sleepytime;
258 g_assert (ms < 1000);
260 sleepytime.tv_sec = 0;
261 sleepytime.tv_nsec = ms * 1000000;
263 nanosleep (&sleepytime, NULL);
266 static inline int _wapi_namespace_lock (void)
268 return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_NAMESPACE));
271 /* This signature makes it easier to use in pthread cleanup handlers */
272 static inline int _wapi_namespace_unlock (gpointer data G_GNUC_UNUSED)
274 return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_NAMESPACE));
277 static inline void _wapi_handle_share_release (struct _WapiFileShare *info)
281 g_assert (info->handle_refs > 0);
283 /* Prevent new entries racing with us */
284 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
285 g_assert(thr_ret == 0);
287 if (InterlockedDecrement ((gint32 *)&info->handle_refs) == 0) {
288 _wapi_free_share_info (info);
291 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
294 #endif /* _WAPI_HANDLES_PRIVATE_H_ */