2003-06-05 Dick Porter <dick@ximian.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
16 #include <mono/io-layer/wapi-private.h>
17 #include <mono/io-layer/shared.h>
18 #include <mono/io-layer/misc-private.h>
19
20 #undef DEBUG
21
22 /* Shared threads dont seem to work yet */
23 #undef _POSIX_THREAD_PROCESS_SHARED
24
25 extern struct _WapiHandleShared_list **_wapi_shared_data;
26 extern struct _WapiHandleScratch *_wapi_shared_scratch;
27 extern struct _WapiHandlePrivate_list **_wapi_private_data;
28 extern pthread_mutex_t _wapi_shared_mutex;
29 extern guint32 _wapi_shm_mapped_segments;
30
31 extern guint32 _wapi_handle_new_internal (WapiHandleType type);
32 extern gpointer _wapi_handle_new (WapiHandleType type);
33 extern gboolean _wapi_lookup_handle (gpointer handle, WapiHandleType type,
34                                      gpointer *shared, gpointer *private);
35 extern gpointer _wapi_search_handle (WapiHandleType type,
36                                      gboolean (*check)(gpointer, gpointer),
37                                      gpointer user_data,
38                                      gpointer *shared, gpointer *private);
39 extern void _wapi_handle_ref (gpointer handle);
40 extern void _wapi_handle_unref (gpointer handle);
41 extern guint32 _wapi_handle_scratch_store_internal (guint32 bytes,
42                                                     gboolean *remap);
43 extern guint32 _wapi_handle_scratch_store (gconstpointer data, guint32 bytes);
44 extern guint32 _wapi_handle_scratch_store_string_array (gchar **data);
45 extern gpointer _wapi_handle_scratch_lookup (guint32 idx);
46 extern gchar **_wapi_handle_scratch_lookup_string_array (guint32 idx);
47 extern void _wapi_handle_scratch_delete_internal (guint32 idx);
48 extern void _wapi_handle_scratch_delete (guint32 idx);
49 extern void _wapi_handle_scratch_delete_string_array (guint32 idx);
50 extern void _wapi_handle_register_capabilities (WapiHandleType type,
51                                                 WapiHandleCapability caps);
52 extern gboolean _wapi_handle_test_capabilities (gpointer handle,
53                                                 WapiHandleCapability caps);
54 extern void _wapi_handle_ops_close_shared (gpointer handle);
55 extern void _wapi_handle_ops_close_private (gpointer handle);
56 extern void _wapi_handle_ops_signal (gpointer handle);
57 extern void _wapi_handle_ops_own (gpointer handle);
58 extern gboolean _wapi_handle_ops_isowned (gpointer handle);
59
60 extern gboolean _wapi_handle_count_signalled_handles (guint32 numhandles,
61                                                       gpointer *handles,
62                                                       gboolean waitall,
63                                                       guint32 *retcount,
64                                                       guint32 *lowest);
65 extern void _wapi_handle_unlock_handles (guint32 numhandles,
66                                          gpointer *handles);
67 extern int _wapi_handle_wait_signal (void);
68 extern int _wapi_handle_timedwait_signal (struct timespec *timeout);
69 extern int _wapi_handle_wait_signal_handle (gpointer handle);
70 extern int _wapi_handle_timedwait_signal_handle (gpointer handle,
71                                                  struct timespec *timeout);
72 extern gboolean _wapi_handle_process_fork (guint32 cmd, guint32 env,
73                                            guint32 dir, gboolean inherit,
74                                            guint32 flags,
75                                            gpointer stdin_handle,
76                                            gpointer stdout_handle,
77                                            gpointer stderr_handle,
78                                            gpointer *process_handle,
79                                            gpointer *thread_handle,
80                                            guint32 *pid, guint32 *tid);
81
82 static inline struct _WapiHandleShared_list *_wapi_handle_get_shared_segment (guint32 segment)
83 {
84         struct _WapiHandleShared_list *shared;
85         
86         pthread_mutex_lock (&_wapi_shared_mutex);
87         shared=_wapi_shared_data[segment];
88         pthread_mutex_unlock (&_wapi_shared_mutex);
89
90         return(shared);
91 }
92
93 static inline struct _WapiHandlePrivate_list *_wapi_handle_get_private_segment (guint32 segment)
94 {
95         struct _WapiHandlePrivate_list *priv;
96         
97         pthread_mutex_lock (&_wapi_shared_mutex);
98         priv=_wapi_private_data[segment];
99         pthread_mutex_unlock (&_wapi_shared_mutex);
100         
101         return(priv);
102 }
103
104 static inline void _wapi_handle_ensure_mapped (guint32 segment)
105 {
106 #ifdef DEBUG
107         g_message (G_GNUC_PRETTY_FUNCTION ": checking segment %d is mapped",
108                    segment);
109         g_message (G_GNUC_PRETTY_FUNCTION ": _wapi_shm_mapped_segments: %d",
110                    _wapi_shm_mapped_segments);
111         if(segment<_wapi_shm_mapped_segments) {
112                 g_message (G_GNUC_PRETTY_FUNCTION ": _wapi_handle_get_shared_segment(segment): %p", _wapi_handle_get_shared_segment (segment));
113         }
114 #endif
115
116         if(segment<_wapi_shm_mapped_segments &&
117            _wapi_handle_get_shared_segment (segment)!=NULL) {
118                 /* Got it already */
119                 return;
120         }
121         
122         pthread_mutex_lock (&_wapi_shared_mutex);
123         
124         if(segment>=_wapi_shm_mapped_segments) {
125                 /* Need to extend the arrays.  We can't use g_renew
126                  * here, because the unmapped segments must be NULL,
127                  * and g_renew doesn't initialise the memory it
128                  * returns
129                  */
130                 gulong old_len, new_len;
131                 
132                 old_len=_wapi_shm_mapped_segments;
133                 new_len=segment+1;
134                 
135 #ifdef DEBUG
136                 g_message (G_GNUC_PRETTY_FUNCTION
137                            ": extending shared array: mapped_segments is %d",
138                            _wapi_shm_mapped_segments);
139 #endif
140                 
141                 _wapi_shared_data=_wapi_g_renew0 (_wapi_shared_data, sizeof(struct _WapiHandleShared_list *) * old_len, sizeof(struct _WapiHandleShared_list *) * new_len);
142
143                 if(_wapi_private_data!=NULL) {
144                         /* the daemon doesn't deal with private data */
145                         _wapi_private_data=_wapi_g_renew0 (_wapi_private_data, sizeof(struct _WapiHandlePrivate_list *) * old_len, sizeof(struct _WapiHandlePrivate_list *) * new_len);
146                 }
147                 
148                 _wapi_shm_mapped_segments=segment+1;
149         }
150         
151         if(_wapi_shared_data[segment]==NULL) {
152                 /* Need to map it too */
153 #ifdef DEBUG
154                 g_message (G_GNUC_PRETTY_FUNCTION ": mapping segment %d",
155                            segment);
156 #endif
157
158                 _wapi_shared_data[segment]=_wapi_shm_file_map (WAPI_SHM_DATA,
159                                                                segment, NULL);
160                 if(_wapi_private_data!=NULL) {
161                         /* the daemon doesn't deal with private data */
162                         _wapi_private_data[segment]=g_new0 (struct _WapiHandlePrivate_list, 1);
163                 }
164         }
165
166         pthread_mutex_unlock (&_wapi_shared_mutex);
167 }
168
169 static inline void _wapi_handle_segment (gpointer handle, guint32 *segment,
170                                          guint32 *idx)
171 {
172         guint32 h=GPOINTER_TO_UINT (handle);
173         div_t divvy;
174
175         divvy=div (h, _WAPI_HANDLES_PER_SEGMENT);
176         *segment=divvy.quot;
177         *idx=divvy.rem;
178 }
179
180 static inline guint32 _wapi_handle_index (guint32 segment, guint32 idx)
181 {
182         return((segment*_WAPI_HANDLES_PER_SEGMENT)+idx);
183 }
184
185 static inline WapiHandleType _wapi_handle_type (gpointer handle)
186 {
187         guint32 idx;
188         guint32 segment;
189         
190         _wapi_handle_segment (handle, &segment, &idx);
191         
192         return(_wapi_handle_get_shared_segment (segment)->handles[idx].type);
193 }
194
195 static inline void _wapi_handle_set_signal_state (gpointer handle,
196                                                   gboolean state,
197                                                   gboolean broadcast)
198 {
199         guint32 idx;
200         guint32 segment;
201         struct _WapiHandleShared *shared_handle;
202         
203         _wapi_handle_segment (handle, &segment, &idx);
204         shared_handle=&_wapi_handle_get_shared_segment (segment)->handles[idx];
205         
206 #ifdef DEBUG
207         g_message (G_GNUC_PRETTY_FUNCTION ": setting state of %p to %s (broadcast %s)", handle, state?"TRUE":"FALSE", broadcast?"TRUE":"FALSE");
208 #endif
209
210         if(state==TRUE) {
211                 /* Tell everyone blocking on a single handle */
212
213                 /* This function _must_ be called with
214                  * handle->signal_mutex locked
215                  */
216                 shared_handle->signalled=state;
217                 
218                 if(broadcast==TRUE) {
219                         pthread_cond_broadcast (&shared_handle->signal_cond);
220                 } else {
221                         pthread_cond_signal (&shared_handle->signal_cond);
222                 }
223
224                 /* Tell everyone blocking on multiple handles that something
225                  * was signalled
226                  */
227 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
228                 {
229                         struct _WapiHandleShared_list *segment0=_wapi_handle_get_shared_segment (0);
230                         mono_mutex_lock (&segment0->signal_mutex);
231                         pthread_cond_broadcast (&segment0->signal_cond);
232                         mono_mutex_unlock (&segment0->signal_mutex);
233                 }
234 #else
235                 {
236                         struct _WapiHandlePrivate_list *segment0=_wapi_handle_get_private_segment (0);
237                         
238 #ifdef DEBUG
239                         g_message (G_GNUC_PRETTY_FUNCTION ": lock global signal mutex");
240 #endif
241
242                         mono_mutex_lock (&segment0->signal_mutex);
243                         pthread_cond_broadcast (&segment0->signal_cond);
244
245 #ifdef DEBUG
246                         g_message (G_GNUC_PRETTY_FUNCTION ": unlock global signal mutex");
247 #endif
248
249                         mono_mutex_unlock (&segment0->signal_mutex);
250                 }
251 #endif /* _POSIX_THREAD_PROCESS_SHARED */
252         } else {
253                 shared_handle->signalled=state;
254         }
255 }
256
257 static inline gboolean _wapi_handle_issignalled (gpointer handle)
258 {
259         guint32 idx;
260         guint32 segment;
261         
262         _wapi_handle_segment (handle, &segment, &idx);
263         
264         return(_wapi_handle_get_shared_segment (segment)->handles[idx].signalled);
265 }
266
267 static inline int _wapi_handle_lock_signal_mutex (void)
268 {
269 #ifdef DEBUG
270         g_message (G_GNUC_PRETTY_FUNCTION ": lock global signal mutex");
271 #endif
272 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
273         return(mono_mutex_lock (&_wapi_handle_get_shared_segment (0)->signal_mutex));
274 #else
275         return(mono_mutex_lock (&_wapi_handle_get_private_segment (0)->signal_mutex));
276 #endif /* _POSIX_THREAD_PROCESS_SHARED */
277 }
278
279 static inline int _wapi_handle_unlock_signal_mutex (void)
280 {
281 #ifdef DEBUG
282         g_message (G_GNUC_PRETTY_FUNCTION ": unlock global signal mutex");
283 #endif
284 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
285         return(mono_mutex_unlock (&_wapi_handle_get_shared_segment (0)->signal_mutex));
286 #else
287         return(mono_mutex_unlock (&_wapi_handle_get_private_segment (0)->signal_mutex));
288 #endif /* _POSIX_THREAD_PROCESS_SHARED */
289 }
290
291 static inline int _wapi_handle_lock_handle (gpointer handle)
292 {
293         guint32 idx;
294         guint32 segment;
295         
296 #ifdef DEBUG
297         g_message (G_GNUC_PRETTY_FUNCTION ": locking handle %p", handle);
298 #endif
299
300         _wapi_handle_segment (handle, &segment, &idx);
301         
302         return(mono_mutex_lock (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_mutex));
303 }
304
305 static inline int _wapi_handle_unlock_handle (gpointer handle)
306 {
307         guint32 idx;
308         guint32 segment;
309         
310 #ifdef DEBUG
311         g_message (G_GNUC_PRETTY_FUNCTION ": unlocking handle %p", handle);
312 #endif
313
314         _wapi_handle_segment (handle, &segment, &idx);
315         
316         return(mono_mutex_unlock (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_mutex));
317 }
318
319 #endif /* _WAPI_HANDLES_PRIVATE_H_ */