2009-02-10 Zoltan Varga <vargaz@gmail.com>
[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-2006 Novell, Inc.
8  */
9
10 #ifndef _WAPI_PRIVATE_H_
11 #define _WAPI_PRIVATE_H_
12
13 #include <config.h>
14 #include <glib.h>
15 #include <sys/stat.h>
16
17 #include <mono/io-layer/handles.h>
18 #include <mono/io-layer/io.h>
19
20 /* Catch this here rather than corrupt the shared data at runtime */
21 #if MONO_SIZEOF_SUNPATH==0
22 #error configure failed to discover size of unix socket path
23 #endif
24
25 /* Increment this whenever an incompatible change is made to the
26  * shared handle structure.
27  */
28 #define _WAPI_HANDLE_VERSION 12
29
30 typedef enum {
31         WAPI_HANDLE_UNUSED=0,
32         WAPI_HANDLE_FILE,
33         WAPI_HANDLE_CONSOLE,
34         WAPI_HANDLE_THREAD,
35         WAPI_HANDLE_SEM,
36         WAPI_HANDLE_MUTEX,
37         WAPI_HANDLE_EVENT,
38         WAPI_HANDLE_SOCKET,
39         WAPI_HANDLE_FIND,
40         WAPI_HANDLE_PROCESS,
41         WAPI_HANDLE_PIPE,
42         WAPI_HANDLE_NAMEDMUTEX,
43         WAPI_HANDLE_NAMEDSEM,
44         WAPI_HANDLE_NAMEDEVENT,
45         WAPI_HANDLE_COUNT
46 } WapiHandleType;
47
48 extern const char *_wapi_handle_typename[];
49
50 #define _WAPI_SHARED_HANDLE(type) (type == WAPI_HANDLE_PROCESS || \
51                                    type == WAPI_HANDLE_NAMEDMUTEX || \
52                                    type == WAPI_HANDLE_NAMEDSEM || \
53                                    type == WAPI_HANDLE_NAMEDEVENT)
54
55 #define _WAPI_FD_HANDLE(type) (type == WAPI_HANDLE_FILE || \
56                                type == WAPI_HANDLE_CONSOLE || \
57                                type == WAPI_HANDLE_SOCKET || \
58                                type == WAPI_HANDLE_PIPE)
59
60 #define _WAPI_SHARED_NAMESPACE(type) (type == WAPI_HANDLE_NAMEDMUTEX || \
61                                       type == WAPI_HANDLE_NAMEDSEM || \
62                                       type == WAPI_HANDLE_NAMEDEVENT)
63
64 typedef struct 
65 {
66         gchar name[MAX_PATH + 1];
67 } WapiSharedNamespace;
68
69 typedef enum {
70         WAPI_HANDLE_CAP_WAIT=0x01,
71         WAPI_HANDLE_CAP_SIGNAL=0x02,
72         WAPI_HANDLE_CAP_OWN=0x04,
73         WAPI_HANDLE_CAP_SPECIAL_WAIT=0x08
74 } WapiHandleCapability;
75
76 struct _WapiHandleOps 
77 {
78         void (*close)(gpointer handle, gpointer data);
79
80         /* SignalObjectAndWait */
81         void (*signal)(gpointer signal);
82
83         /* Called by WaitForSingleObject and WaitForMultipleObjects,
84          * with the handle locked (shared handles aren't locked.)
85          * Returns TRUE if ownership was established, false otherwise.
86          */
87         gboolean (*own_handle)(gpointer handle);
88
89         /* Called by WaitForSingleObject and WaitForMultipleObjects, if the
90          * handle in question is "ownable" (ie mutexes), to see if the current
91          * thread already owns this handle
92          */
93         gboolean (*is_owned)(gpointer handle);
94
95         /* Called by WaitForSingleObject and WaitForMultipleObjects,
96          * if the handle in question needs a special wait function
97          * instead of using the normal handle signal mechanism.
98          * Returns the WaitForSingleObject return code.
99          */
100         guint32 (*special_wait)(gpointer handle, guint32 timeout);
101
102         /* Called by WaitForSingleObject and WaitForMultipleObjects,
103          * if the handle in question needs some preprocessing before the
104          * signal wait.
105          */
106         void (*prewait)(gpointer handle);
107 };
108
109 #include <mono/io-layer/event-private.h>
110 #include <mono/io-layer/io-private.h>
111 #include <mono/io-layer/mutex-private.h>
112 #include <mono/io-layer/semaphore-private.h>
113 #include <mono/io-layer/socket-private.h>
114 #include <mono/io-layer/thread-private.h>
115 #include <mono/io-layer/process-private.h>
116
117 struct _WapiHandle_shared_ref
118 {
119         /* This will be split 16:16 with the shared file segment in
120          * the top half, when I implement space increases
121          */
122         guint32 offset;
123 };
124
125 #define _WAPI_HANDLE_INITIAL_COUNT 256
126
127 struct _WapiHandleUnshared
128 {
129         WapiHandleType type;
130         guint ref;
131         gboolean signalled;
132         mono_mutex_t signal_mutex;
133         pthread_cond_t signal_cond;
134         
135         union 
136         {
137                 struct _WapiHandle_event event;
138                 struct _WapiHandle_file file;
139                 struct _WapiHandle_find find;
140                 struct _WapiHandle_mutex mutex;
141                 struct _WapiHandle_sem sem;
142                 struct _WapiHandle_socket sock;
143                 struct _WapiHandle_thread thread;
144                 struct _WapiHandle_shared_ref shared;
145         } u;
146 };
147
148 struct _WapiHandleShared
149 {
150         WapiHandleType type;
151         guint32 timestamp;
152         guint32 handle_refs;
153         volatile gboolean signalled;
154         
155         union
156         {
157                 struct _WapiHandle_process process;
158                 struct _WapiHandle_namedmutex namedmutex;
159                 struct _WapiHandle_namedsem namedsem;
160                 struct _WapiHandle_namedevent namedevent;
161         } u;
162 };
163
164 #define _WAPI_SHARED_SEM_NAMESPACE 0
165 /*#define _WAPI_SHARED_SEM_COLLECTION 1*/
166 #define _WAPI_SHARED_SEM_FILESHARE 2
167 #define _WAPI_SHARED_SEM_SHARED_HANDLES 3
168 #define _WAPI_SHARED_SEM_PROCESS_COUNT_LOCK 6
169 #define _WAPI_SHARED_SEM_PROCESS_COUNT 7
170 #define _WAPI_SHARED_SEM_COUNT 8        /* Leave some future expansion space */
171
172 struct _WapiHandleSharedLayout
173 {
174         volatile guint32 collection_count;
175         volatile key_t sem_key;
176         
177         struct _WapiHandleShared handles[_WAPI_HANDLE_INITIAL_COUNT];
178 };
179
180 #define _WAPI_FILESHARE_SIZE 102400
181
182 struct _WapiFileShare
183 {
184 #ifdef WAPI_FILE_SHARE_PLATFORM_EXTRA_DATA
185         WAPI_FILE_SHARE_PLATFORM_EXTRA_DATA
186 #endif
187         dev_t device;
188         ino_t inode;
189         pid_t opened_by_pid;
190         guint32 sharemode;
191         guint32 access;
192         guint32 handle_refs;
193         guint32 timestamp;
194 };
195
196 struct _WapiFileShareLayout
197 {
198         guint32 hwm;
199         
200         struct _WapiFileShare share_info[_WAPI_FILESHARE_SIZE];
201 };
202
203
204
205 #define _WAPI_HANDLE_INVALID (gpointer)-1
206
207 #endif /* _WAPI_PRIVATE_H_ */