2003-02-25 Dick Porter <dick@ximian.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 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 /* Shared threads don't seem to work yet */
21 #undef _POSIX_THREAD_PROCESS_SHARED
22
23 /* Catch this here rather than corrupt the shared data at runtime */
24 #if MONO_SIZEOF_SUNPATH==0
25 #error configure failed to discover size of unix socket path
26 #endif
27
28 /* Increment this whenever an incompatible change is made to the
29  * shared handle structure.
30  */
31 #define _WAPI_HANDLE_VERSION 0
32
33 typedef enum {
34         WAPI_HANDLE_UNUSED=0,
35         WAPI_HANDLE_FILE,
36         WAPI_HANDLE_CONSOLE,
37         WAPI_HANDLE_THREAD,
38         WAPI_HANDLE_SEM,
39         WAPI_HANDLE_MUTEX,
40         WAPI_HANDLE_EVENT,
41         WAPI_HANDLE_SOCKET,
42         WAPI_HANDLE_FIND,
43         WAPI_HANDLE_PROCESS,
44         WAPI_HANDLE_PIPE,
45         WAPI_HANDLE_COUNT,
46 } WapiHandleType;
47
48 typedef enum {
49         WAPI_HANDLE_CAP_WAIT=0x01,
50         WAPI_HANDLE_CAP_SIGNAL=0x02,
51         WAPI_HANDLE_CAP_OWN=0x04,
52 } WapiHandleCapability;
53
54 struct _WapiHandleOps 
55 {
56         void (*close_shared)(gpointer handle);
57         void (*close_private)(gpointer handle);
58
59         /* SignalObjectAndWait */
60         void (*signal)(gpointer signal);
61
62         /* Called by WaitForSingleObject and WaitForMultipleObjects,
63          * with the handle locked
64          */
65         void (*own_handle)(gpointer handle);
66
67         /* Called by WaitForSingleObject and WaitForMultipleObjects, if the
68          * handle in question is "ownable" (ie mutexes), to see if the current
69          * thread already owns this handle
70          */
71         gboolean (*is_owned)(gpointer handle);
72 };
73
74 #include <mono/io-layer/event-private.h>
75 #include <mono/io-layer/io-private.h>
76 #include <mono/io-layer/mutex-private.h>
77 #include <mono/io-layer/semaphore-private.h>
78 #include <mono/io-layer/socket-private.h>
79 #include <mono/io-layer/thread-private.h>
80 #include <mono/io-layer/process-private.h>
81
82 struct _WapiHandleShared
83 {
84         WapiHandleType type;
85         guint ref;
86         gboolean signalled;
87         mono_mutex_t signal_mutex;
88         pthread_cond_t signal_cond;
89         
90         union 
91         {
92                 struct _WapiHandle_event event;
93                 struct _WapiHandle_file file;
94                 struct _WapiHandle_find find;
95                 struct _WapiHandle_mutex mutex;
96                 struct _WapiHandle_sem sem;
97                 struct _WapiHandle_socket sock;
98                 struct _WapiHandle_thread thread;
99                 struct _WapiHandle_process process;
100         } u;
101 };
102
103 #define _WAPI_MAX_HANDLES 4096
104 #define _WAPI_HANDLE_INVALID (gpointer)-1
105
106 /*
107  * This is the layout of the shared memory segment
108  */
109 struct _WapiHandleShared_list
110 {
111         guchar daemon[MONO_SIZEOF_SUNPATH];
112         _wapi_daemon_status daemon_running;
113         
114 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
115         mono_mutex_t signal_mutex;
116         pthread_cond_t signal_cond;
117 #endif
118         struct _WapiHandleShared handles[_WAPI_MAX_HANDLES];
119         guchar scratch_base[0];
120 };
121
122 struct _WapiHandlePrivate
123 {
124         union 
125         {
126                 struct _WapiHandlePrivate_event event;
127                 struct _WapiHandlePrivate_file file;
128                 struct _WapiHandlePrivate_find find;
129                 struct _WapiHandlePrivate_mutex mutex;
130                 struct _WapiHandlePrivate_sem sem;
131                 struct _WapiHandlePrivate_socket sock;
132                 struct _WapiHandlePrivate_thread thread;
133                 struct _WapiHandlePrivate_process process;
134         } u;
135 };
136
137 /* Per-process handle info. For lookup convenience, each index matches
138  * the corresponding shared data.
139  */
140 struct _WapiHandlePrivate_list
141 {
142 #if !defined(_POSIX_THREAD_PROCESS_SHARED) || _POSIX_THREAD_PROCESS_SHARED == -1
143         mono_mutex_t signal_mutex;
144         pthread_cond_t signal_cond;
145 #endif
146         struct _WapiHandlePrivate handles[_WAPI_MAX_HANDLES];
147 };
148
149
150 #endif /* _WAPI_PRIVATE_H_ */