e5b0c6ac83345d87ff0d0efa4f77903c8ee0aaeb
[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 /* 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 4
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_COUNT
44 } WapiHandleType;
45
46 extern const char *_wapi_handle_typename[];
47
48 #define _WAPI_SHARED_HANDLE(type) (type == WAPI_HANDLE_THREAD || \
49                                    type == WAPI_HANDLE_PROCESS || \
50                                    type == WAPI_HANDLE_NAMEDMUTEX)
51
52 #define _WAPI_FD_HANDLE(type) (type == WAPI_HANDLE_FILE || \
53                                type == WAPI_HANDLE_CONSOLE || \
54                                type == WAPI_HANDLE_SOCKET || \
55                                type == WAPI_HANDLE_PIPE)
56
57 #define _WAPI_SHARED_NAMESPACE(type) (type == WAPI_HANDLE_NAMEDMUTEX)
58
59 typedef struct 
60 {
61         gchar name[MAX_PATH + 1];
62 } WapiSharedNamespace;
63
64 typedef enum {
65         WAPI_HANDLE_CAP_WAIT=0x01,
66         WAPI_HANDLE_CAP_SIGNAL=0x02,
67         WAPI_HANDLE_CAP_OWN=0x04,
68         WAPI_HANDLE_CAP_SPECIAL_WAIT=0x08
69 } WapiHandleCapability;
70
71 struct _WapiHandleOps 
72 {
73         void (*close)(gpointer handle);
74
75         /* SignalObjectAndWait */
76         void (*signal)(gpointer signal);
77
78         /* Called by WaitForSingleObject and WaitForMultipleObjects,
79          * with the handle locked (shared handles aren't locked.)
80          * Returns TRUE if ownership was established, false otherwise.
81          */
82         gboolean (*own_handle)(gpointer handle);
83
84         /* Called by WaitForSingleObject and WaitForMultipleObjects, if the
85          * handle in question is "ownable" (ie mutexes), to see if the current
86          * thread already owns this handle
87          */
88         gboolean (*is_owned)(gpointer handle);
89
90         /* Called by WaitForSingleObject, if the handle in question
91          * needs a special wait function instead of using the normal
92          * handle signal mechanism.  Returns the WaitForSingleObject
93          * return code.
94          */
95         guint32 (*special_wait)(gpointer handle, guint32 timeout);
96 };
97
98 #include <mono/io-layer/event-private.h>
99 #include <mono/io-layer/io-private.h>
100 #include <mono/io-layer/mutex-private.h>
101 #include <mono/io-layer/semaphore-private.h>
102 #include <mono/io-layer/socket-private.h>
103 #include <mono/io-layer/thread-private.h>
104 #include <mono/io-layer/process-private.h>
105
106 struct _WapiHandle_shared_ref
107 {
108         /* This will be split 16:16 with the shared file segment in
109          * the top half, when I implement space increases
110          */
111         guint32 offset;
112 };
113
114 #define _WAPI_HANDLE_INITIAL_COUNT 4096
115 #define _WAPI_HEADROOM 16
116
117 struct _WapiHandleUnshared
118 {
119         WapiHandleType type;
120         guint ref;
121         gboolean signalled;
122         mono_mutex_t signal_mutex;
123         pthread_cond_t signal_cond;
124         
125         union 
126         {
127                 struct _WapiHandle_event event;
128                 struct _WapiHandle_file file;
129                 struct _WapiHandle_find find;
130                 struct _WapiHandle_mutex mutex;
131                 struct _WapiHandle_sem sem;
132                 struct _WapiHandle_socket sock;
133                 struct _WapiHandle_shared_ref shared;
134         } u;
135 };
136
137 struct _WapiHandleSharedMetadata
138 {
139         volatile guint32 offset;
140         guint32 timestamp;
141         volatile gboolean signalled;
142         volatile guint32 checking;
143 };
144
145 struct _WapiHandleShared
146 {
147         WapiHandleType type;
148         gboolean stale;
149         
150         union
151         {
152                 struct _WapiHandle_thread thread;
153                 struct _WapiHandle_process process;
154                 struct _WapiHandle_namedmutex namedmutex;
155         } u;
156 };
157
158 struct _WapiHandleSharedLayout
159 {
160         guint32 namespace_check;
161         volatile guint32 signal_count;
162
163         guint32 master_timestamp;
164         volatile guint32 collection_count;
165         
166         struct _WapiHandleSharedMetadata metadata[_WAPI_HANDLE_INITIAL_COUNT];
167         struct _WapiHandleShared handles[_WAPI_HANDLE_INITIAL_COUNT];
168 };
169
170 #define _WAPI_FILESHARE_SIZE 102400
171
172 struct _WapiFileShare
173 {
174         dev_t device;
175         ino_t inode;
176         guint32 sharemode;
177         guint32 access;
178         guint32 handle_refs;
179         guint32 timestamp;
180 };
181
182 struct _WapiFileShareLayout
183 {
184         guint32 share_check;
185         guint32 hwm;
186         
187         struct _WapiFileShare share_info[_WAPI_FILESHARE_SIZE];
188 };
189
190
191
192 #define _WAPI_HANDLE_INVALID (gpointer)-1
193
194 #endif /* _WAPI_PRIVATE_H_ */