2003-02-02 Piers Haken <piersh@friskit.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
19 #undef DEBUG
20
21 /* Shared threads dont seem to work yet */
22 #undef _POSIX_THREAD_PROCESS_SHARED
23
24 extern struct _WapiHandleShared_list *_wapi_shared_data;
25 extern struct _WapiHandlePrivate_list *_wapi_private_data;
26
27 extern guint32 _wapi_handle_new_internal (WapiHandleType type);
28 extern gpointer _wapi_handle_new (WapiHandleType type);
29 extern gboolean _wapi_lookup_handle (gpointer handle, WapiHandleType type,
30                                      gpointer *shared, gpointer *private);
31 extern gpointer _wapi_search_handle (WapiHandleType type,
32                                      gboolean (*check)(gpointer, gpointer),
33                                      gpointer user_data,
34                                      gpointer *shared, gpointer *private);
35 extern void _wapi_handle_ref (gpointer handle);
36 extern void _wapi_handle_unref (gpointer handle);
37 extern guint32 _wapi_handle_scratch_store_internal (guint32 bytes);
38 extern guint32 _wapi_handle_scratch_store (gconstpointer data, guint32 bytes);
39 extern guint32 _wapi_handle_scratch_store_string_array (gchar **data);
40 extern gconstpointer _wapi_handle_scratch_lookup (guint32 idx);
41 extern guchar *_wapi_handle_scratch_lookup_as_string (guint32 idx);
42 extern gchar **_wapi_handle_scratch_lookup_string_array (guint32 idx);
43 extern void _wapi_handle_scratch_delete_internal (guint32 idx);
44 extern void _wapi_handle_scratch_delete (guint32 idx);
45 extern void _wapi_handle_scratch_delete_string_array (guint32 idx);
46 extern void _wapi_handle_register_capabilities (WapiHandleType type,
47                                                 WapiHandleCapability caps);
48 extern gboolean _wapi_handle_test_capabilities (gpointer handle,
49                                                 WapiHandleCapability caps);
50 extern void _wapi_handle_ops_close_shared (gpointer handle);
51 extern void _wapi_handle_ops_close_private (gpointer handle);
52 extern void _wapi_handle_ops_signal (gpointer handle);
53 extern void _wapi_handle_ops_own (gpointer handle);
54 extern gboolean _wapi_handle_ops_isowned (gpointer handle);
55
56 extern gboolean _wapi_handle_count_signalled_handles (guint32 numhandles,
57                                                       gpointer *handles,
58                                                       gboolean waitall,
59                                                       guint32 *retcount,
60                                                       guint32 *lowest);
61 extern void _wapi_handle_unlock_handles (guint32 numhandles,
62                                          gpointer *handles);
63 extern int _wapi_handle_wait_signal (void);
64 extern int _wapi_handle_timedwait_signal (struct timespec *timeout);
65 extern int _wapi_handle_wait_signal_handle (gpointer handle);
66 extern int _wapi_handle_timedwait_signal_handle (gpointer handle,
67                                                  struct timespec *timeout);
68 extern gboolean _wapi_handle_process_fork (guint32 cmd, guint32 env,
69                                            guint32 dir, gboolean inherit,
70                                            guint32 flags,
71                                            gpointer stdin_handle,
72                                            gpointer stdout_handle,
73                                            gpointer stderr_handle,
74                                            gpointer *process_handle,
75                                            gpointer *thread_handle,
76                                            guint32 *pid, guint32 *tid);
77
78 static inline WapiHandleType _wapi_handle_type (gpointer handle)
79 {
80         guint32 idx=GPOINTER_TO_UINT (handle);
81         
82         return(_wapi_shared_data->handles[idx].type);
83 }
84
85 static inline void _wapi_handle_set_signal_state (gpointer handle,
86                                                   gboolean state,
87                                                   gboolean broadcast)
88 {
89         guint32 idx=GPOINTER_TO_UINT (handle);
90
91 #ifdef DEBUG
92         g_message (G_GNUC_PRETTY_FUNCTION ": setting state of %p to %s (broadcast %s)", handle, state?"TRUE":"FALSE", broadcast?"TRUE":"FALSE");
93 #endif
94
95         if(state==TRUE) {
96                 /* Tell everyone blocking on a single handle */
97
98                 /* This function _must_ be called with
99                  * handle->signal_mutex locked
100                  */
101                 _wapi_shared_data->handles[idx].signalled=state;
102                 
103                 if(broadcast==TRUE) {
104                         pthread_cond_broadcast (&_wapi_shared_data->handles[idx].signal_cond);
105                 } else {
106                         pthread_cond_signal (&_wapi_shared_data->handles[idx].signal_cond);
107                 }
108
109                 /* Tell everyone blocking on multiple handles that something
110                  * was signalled
111                  */
112 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
113                 mono_mutex_lock (&_wapi_shared_data->signal_mutex);
114                 pthread_cond_broadcast (&_wapi_shared_data->signal_cond);
115                 mono_mutex_unlock (&_wapi_shared_data->signal_mutex);
116 #else
117 #ifdef DEBUG
118                 g_message (G_GNUC_PRETTY_FUNCTION ": lock global signal mutex");
119 #endif
120
121                 mono_mutex_lock (&_wapi_private_data->signal_mutex);
122                 pthread_cond_broadcast (&_wapi_private_data->signal_cond);
123
124 #ifdef DEBUG
125                 g_message (G_GNUC_PRETTY_FUNCTION ": unlock global signal mutex");
126 #endif
127
128                 mono_mutex_unlock (&_wapi_private_data->signal_mutex);
129 #endif /* _POSIX_THREAD_PROCESS_SHARED */
130         } else {
131                 _wapi_shared_data->handles[idx].signalled=state;
132         }
133 }
134
135 static inline gboolean _wapi_handle_issignalled (gpointer handle)
136 {
137         guint32 idx=GPOINTER_TO_UINT (handle);
138
139         return(_wapi_shared_data->handles[idx].signalled);
140 }
141
142 static inline int _wapi_handle_lock_signal_mutex (void)
143 {
144 #ifdef DEBUG
145         g_message (G_GNUC_PRETTY_FUNCTION ": lock global signal mutex");
146 #endif
147 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
148         return(mono_mutex_lock (&_wapi_shared_data->signal_mutex));
149 #else
150         return(mono_mutex_lock (&_wapi_private_data->signal_mutex));
151 #endif /* _POSIX_THREAD_PROCESS_SHARED */
152 }
153
154 static inline int _wapi_handle_unlock_signal_mutex (void)
155 {
156 #ifdef DEBUG
157         g_message (G_GNUC_PRETTY_FUNCTION ": unlock global signal mutex");
158 #endif
159 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
160         return(mono_mutex_unlock (&_wapi_shared_data->signal_mutex));
161 #else
162         return(mono_mutex_unlock (&_wapi_private_data->signal_mutex));
163 #endif /* _POSIX_THREAD_PROCESS_SHARED */
164 }
165
166 static inline int _wapi_handle_lock_handle (gpointer handle)
167 {
168         guint32 idx=GPOINTER_TO_UINT (handle);
169
170 #ifdef DEBUG
171         g_message (G_GNUC_PRETTY_FUNCTION ": locking handle %p", handle);
172 #endif
173         
174         return(mono_mutex_lock (&_wapi_shared_data->handles[idx].signal_mutex));
175 }
176
177 static inline int _wapi_handle_unlock_handle (gpointer handle)
178 {
179         guint32 idx=GPOINTER_TO_UINT (handle);
180
181 #ifdef DEBUG
182         g_message (G_GNUC_PRETTY_FUNCTION ": unlocking handle %p", handle);
183 #endif
184         
185         return(mono_mutex_unlock (&_wapi_shared_data->handles[idx].signal_mutex));
186 }
187
188 #endif /* _WAPI_HANDLES_PRIVATE_H_ */