forgot this
[mono.git] / mono / io-layer / wapi-private.h
1 /*
2  * wapi-private.h:  internal definitions of handles and shared memory layout
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *
7  * (C) 2002 Ximian, Inc.
8  */
9
10 #ifndef _WAPI_PRIVATE_H_
11 #define _WAPI_PRIVATE_H_
12
13 #include <config.h>
14 #include <glib.h>
15
16 #include <mono/io-layer/handles.h>
17 #include <mono/io-layer/io.h>
18 #include <mono/io-layer/daemon-private.h>
19
20 /* for non-GCC compilers where Glib gives an empty pretty function 
21  * macro create one that gives file & line number instead
22  */
23 #ifndef __GNUC__
24 #undef G_GNUC_PRETTY_FUNCTION
25 #define STRINGIZE_HELPER(exp) #exp
26 #define STRINGIZE(exp) STRINGIZE_HELPER(exp)
27 #define G_GNUC_PRETTY_FUNCTION __FILE__ "(" STRINGIZE(__LINE__) ")"
28 #endif
29
30 /* Catch this here rather than corrupt the shared data at runtime */
31 #if MONO_SIZEOF_SUNPATH==0
32 #error configure failed to discover size of unix socket path
33 #endif
34
35 /* Increment this whenever an incompatible change is made to the
36  * shared handle structure.
37  */
38 #define _WAPI_HANDLE_VERSION 3
39
40 typedef enum {
41         WAPI_HANDLE_UNUSED=0,
42         WAPI_HANDLE_FILE,
43         WAPI_HANDLE_CONSOLE,
44         WAPI_HANDLE_THREAD,
45         WAPI_HANDLE_SEM,
46         WAPI_HANDLE_MUTEX,
47         WAPI_HANDLE_EVENT,
48         WAPI_HANDLE_SOCKET,
49         WAPI_HANDLE_FIND,
50         WAPI_HANDLE_PROCESS,
51         WAPI_HANDLE_PIPE,
52         WAPI_HANDLE_COUNT
53 } WapiHandleType;
54
55 #define _WAPI_SHARED_NAMESPACE(type) (type==WAPI_HANDLE_MUTEX)
56
57 typedef struct 
58 {
59         guint32 name;
60 } WapiSharedNamespace;
61
62 /* The boolean is for distinguishing between a zeroed struct being not
63  * as yet assigned, and one containing a valid fd 0.  It's also used
64  * to signal that a previously-good fd has been reused behind our
65  * back, so we need to invalidate the handle that thought it owned the
66  * fd.
67  */
68 typedef struct 
69 {
70         int fd;
71         gboolean assigned;
72 } WapiFDMapped;
73
74
75 typedef enum {
76         WAPI_HANDLE_CAP_WAIT=0x01,
77         WAPI_HANDLE_CAP_SIGNAL=0x02,
78         WAPI_HANDLE_CAP_OWN=0x04
79 } WapiHandleCapability;
80
81 struct _WapiHandleOps 
82 {
83         void (*close_shared)(gpointer handle);
84         void (*close_private)(gpointer handle);
85
86         /* SignalObjectAndWait */
87         void (*signal)(gpointer signal);
88
89         /* Called by WaitForSingleObject and WaitForMultipleObjects,
90          * with the handle locked
91          */
92         void (*own_handle)(gpointer handle);
93
94         /* Called by WaitForSingleObject and WaitForMultipleObjects, if the
95          * handle in question is "ownable" (ie mutexes), to see if the current
96          * thread already owns this handle
97          */
98         gboolean (*is_owned)(gpointer handle);
99 };
100
101 #include <mono/io-layer/event-private.h>
102 #include <mono/io-layer/io-private.h>
103 #include <mono/io-layer/mutex-private.h>
104 #include <mono/io-layer/semaphore-private.h>
105 #include <mono/io-layer/socket-private.h>
106 #include <mono/io-layer/thread-private.h>
107 #include <mono/io-layer/process-private.h>
108
109 /* Shared threads don't seem to work yet */
110 #undef _POSIX_THREAD_PROCESS_SHARED
111
112 struct _WapiHandleShared
113 {
114         WapiHandleType type;
115         guint ref;
116         gboolean signalled;
117         mono_mutex_t signal_mutex;
118         pthread_cond_t signal_cond;
119         
120         union 
121         {
122                 struct _WapiHandle_event event;
123                 struct _WapiHandle_file file;
124                 struct _WapiHandle_find find;
125                 struct _WapiHandle_mutex mutex;
126                 struct _WapiHandle_sem sem;
127                 struct _WapiHandle_socket sock;
128                 struct _WapiHandle_thread thread;
129                 struct _WapiHandle_process process;
130         } u;
131 };
132
133 #define _WAPI_HANDLES_PER_SEGMENT 4096
134 #define _WAPI_HANDLE_INVALID (gpointer)-1
135
136 #define _WAPI_SHM_SCRATCH_SIZE 512000
137
138 /*
139  * This is the layout of the shared scratch data.  When the data array
140  * is filled, it will be expanded by _WAPI_SHM_SCRATCH_SIZE
141  * bytes. (scratch data is always copied out of the shared memory, so
142  * it doesn't matter that the mapping will move around.)
143  */
144 struct _WapiHandleScratch
145 {
146         guint32 data_len;
147
148         /* This is set to TRUE by the daemon.  It determines whether a
149          * resize will go via mremap() or just realloc().
150          */
151         gboolean is_shared;
152         guchar scratch_data[MONO_ZERO_ARRAY_LENGTH];
153 };
154
155 /*
156  * This is the layout of the shared memory segments.  When the handles
157  * array is filled, another shared memory segment will be allocated
158  * with the same structure.  This is to avoid having the shared memory
159  * potentially move if it is resized and remapped.
160  *
161  * Note that the additional segments have the same structure, but only
162  * the handle array is used.
163  */
164 struct _WapiHandleShared_list
165 {
166         guchar daemon[MONO_SIZEOF_SUNPATH];
167         _wapi_daemon_status daemon_running;
168         guint32 fd_offset_table_size;
169         
170 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
171         mono_mutex_t signal_mutex;
172         pthread_cond_t signal_cond;
173 #endif
174
175         /* This holds the number of segments */
176         guint32 num_segments;
177         struct _WapiHandleShared handles[_WAPI_HANDLES_PER_SEGMENT];
178 };
179
180 struct _WapiHandlePrivate
181 {
182         WapiHandleType type;
183
184         union 
185         {
186                 struct _WapiHandlePrivate_event event;
187                 struct _WapiHandlePrivate_file file;
188                 struct _WapiHandlePrivate_find find;
189                 struct _WapiHandlePrivate_mutex mutex;
190                 struct _WapiHandlePrivate_sem sem;
191                 struct _WapiHandlePrivate_socket sock;
192                 struct _WapiHandlePrivate_thread thread;
193                 struct _WapiHandlePrivate_process process;
194         } u;
195 };
196
197 /* Per-process handle info. For lookup convenience, each segment and
198  * index matches the corresponding shared data.
199  *
200  * Note that the additional segments have the same structure, but only
201  * the handle array is used.
202  */
203 struct _WapiHandlePrivate_list
204 {
205 #if !defined(_POSIX_THREAD_PROCESS_SHARED) || _POSIX_THREAD_PROCESS_SHARED == -1
206         mono_mutex_t signal_mutex;
207         pthread_cond_t signal_cond;
208 #endif
209         
210         struct _WapiHandlePrivate handles[_WAPI_HANDLES_PER_SEGMENT];
211 };
212
213
214 #endif /* _WAPI_PRIVATE_H_ */