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