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