2 * handles-private.h: Internal operations on handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
10 #ifndef _WAPI_HANDLES_PRIVATE_H_
11 #define _WAPI_HANDLES_PRIVATE_H_
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>
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)
30 extern struct _WapiHandleUnshared *_wapi_private_handles [];
31 extern struct _WapiHandleSharedLayout *_wapi_shared_layout;
32 extern struct _WapiFileShareLayout *_wapi_fileshare_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;
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,
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),
54 gpointer *handle_specific);
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 gboolean _wapi_handle_count_signalled_handles (guint32 numhandles,
75 extern void _wapi_handle_unlock_handles (guint32 numhandles,
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,
87 guint32 *old_sharemode,
89 struct _WapiFileShare **info);
90 extern void _wapi_handle_check_share (struct _WapiFileShare *share_info,
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),
98 /* This is OK to use for atomic writes of individual struct members, as they
101 #define WAPI_SHARED_HANDLE_METADATA(handle) _wapi_shared_layout->metadata[_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT((handle))).u.shared.offset]
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.
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
110 static inline WapiHandleType _wapi_handle_type (gpointer handle)
112 guint32 idx = GPOINTER_TO_UINT(handle);
114 return(_WAPI_PRIVATE_HANDLES(idx).type);
117 static inline void _wapi_handle_set_signal_state (gpointer handle,
121 guint32 idx = GPOINTER_TO_UINT(handle);
122 struct _WapiHandleUnshared *handle_data;
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 /* This function _must_ be called with
138 * handle->signal_mutex locked
140 handle_data->signalled=state;
142 if (broadcast == TRUE) {
143 thr_ret = pthread_cond_broadcast (&handle_data->signal_cond);
144 g_assert (thr_ret == 0);
146 thr_ret = pthread_cond_signal (&handle_data->signal_cond);
147 g_assert (thr_ret == 0);
150 /* Tell everyone blocking on multiple handles that something
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);
157 thr_ret = pthread_cond_broadcast (&_wapi_global_signal_cond);
158 g_assert (thr_ret == 0);
160 thr_ret = mono_mutex_unlock (&_wapi_global_signal_mutex);
161 g_assert (thr_ret == 0);
162 pthread_cleanup_pop (0);
164 handle_data->signalled=state;
168 static inline void _wapi_shared_handle_set_signal_state (gpointer handle,
171 guint32 idx = GPOINTER_TO_UINT(handle);
172 struct _WapiHandleUnshared *handle_data;
173 struct _WapiHandle_shared_ref *ref;
174 struct _WapiHandleSharedMetadata *shared_meta;
176 g_assert (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle)));
178 handle_data = &_WAPI_PRIVATE_HANDLES(idx);
180 ref = &handle_data->u.shared;
181 shared_meta = &_wapi_shared_layout->metadata[ref->offset];
182 shared_meta->signalled = state;
185 g_message ("%s: signalled shared handle offset 0x%x", __func__,
189 InterlockedIncrement (&_wapi_shared_layout->signal_count);
192 static inline gboolean _wapi_handle_issignalled (gpointer handle)
194 guint32 idx = GPOINTER_TO_UINT(handle);
196 if (_WAPI_SHARED_HANDLE(_wapi_handle_type (handle))) {
197 return(WAPI_SHARED_HANDLE_METADATA(handle).signalled);
199 return(_WAPI_PRIVATE_HANDLES(idx).signalled);
203 static inline int _wapi_handle_lock_signal_mutex (void)
206 g_message ("%s: lock global signal mutex", __func__);
209 return(mono_mutex_lock (&_wapi_global_signal_mutex));
212 /* the parameter makes it easier to call from a pthread cleanup handler */
213 static inline int _wapi_handle_unlock_signal_mutex (void *unused)
216 g_message ("%s: unlock global signal mutex", __func__);
219 return(mono_mutex_unlock (&_wapi_global_signal_mutex));
222 static inline int _wapi_handle_lock_handle (gpointer handle)
224 guint32 idx = GPOINTER_TO_UINT(handle);
227 g_message ("%s: locking handle %p", __func__, handle);
230 _wapi_handle_ref (handle);
232 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
236 return(mono_mutex_lock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex));
239 static inline int _wapi_handle_trylock_handle (gpointer handle)
241 guint32 idx = GPOINTER_TO_UINT(handle);
244 g_message ("%s: locking handle %p", __func__, handle);
247 _wapi_handle_ref (handle);
249 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
253 return(mono_mutex_trylock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex));
256 static inline int _wapi_handle_unlock_handle (gpointer handle)
258 guint32 idx = GPOINTER_TO_UINT(handle);
262 g_message ("%s: unlocking handle %p", __func__, handle);
265 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
266 _wapi_handle_unref (handle);
270 ret = mono_mutex_unlock (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
272 _wapi_handle_unref (handle);
277 static inline void _wapi_handle_spin (guint32 ms)
279 struct timespec sleepytime;
281 g_assert (ms < 1000);
283 sleepytime.tv_sec = 0;
284 sleepytime.tv_nsec = ms * 1000000;
286 nanosleep (&sleepytime, NULL);
289 static inline int _wapi_handle_lock_shared_handles (void)
291 return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_HANDLE));
294 static inline int _wapi_handle_trylock_shared_handles (void)
296 return(_wapi_shm_sem_trylock (_WAPI_SHARED_SEM_HANDLE));
299 static inline int _wapi_handle_unlock_shared_handles (void)
301 return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_HANDLE));
304 static inline int _wapi_namespace_lock (void)
306 return(_wapi_shm_sem_lock (_WAPI_SHARED_SEM_NAMESPACE));
309 /* This signature makes it easier to use in pthread cleanup handlers */
310 static inline int _wapi_namespace_unlock (gpointer data G_GNUC_UNUSED)
312 return(_wapi_shm_sem_unlock (_WAPI_SHARED_SEM_NAMESPACE));
315 static inline void _wapi_handle_share_release (struct _WapiFileShare *info)
319 g_assert (info->handle_refs > 0);
321 /* Prevent new entries racing with us */
322 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_SHARE);
323 g_assert(thr_ret == 0);
325 if (InterlockedDecrement (&info->handle_refs) == 0) {
326 memset (info, '\0', sizeof(struct _WapiFileShare));
329 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_SHARE);
332 #endif /* _WAPI_HANDLES_PRIVATE_H_ */