2002-04-30 Dick Porter <dick@ximian.com>
[mono.git] / mono / io-layer / wapi-private.h
1 #ifndef _WAPI_PRIVATE_H_
2 #define _WAPI_PRIVATE_H_
3
4 #include <config.h>
5 #include <glib.h>
6
7 #include "mono/io-layer/handles.h"
8 #include "mono/io-layer/io.h"
9
10 /* Increment this whenever an incompatible change is made to the
11  * shared handle structure.
12  *
13  * If this ever reaches 255, we have problems :-(
14  */
15 #define _WAPI_HANDLE_VERSION 1
16
17 typedef enum {
18         WAPI_HANDLE_UNUSED=0,
19         WAPI_HANDLE_FILE,
20         WAPI_HANDLE_CONSOLE,
21         WAPI_HANDLE_THREAD,
22         WAPI_HANDLE_SEM,
23         WAPI_HANDLE_MUTEX,
24         WAPI_HANDLE_EVENT,
25         WAPI_HANDLE_SOCKET,
26         WAPI_HANDLE_FIND,
27         WAPI_HANDLE_PROCESS,
28         WAPI_HANDLE_COUNT,
29 } WapiHandleType;
30
31 typedef enum {
32         WAPI_HANDLE_CAP_WAIT=0x01,
33         WAPI_HANDLE_CAP_SIGNAL=0x02,
34         WAPI_HANDLE_CAP_OWN=0x04,
35 } WapiHandleCapability;
36
37 struct _WapiHandleOps 
38 {
39         /* All handle types */
40         void (*close)(gpointer handle);
41
42         /* File, console and pipe handles */
43         WapiFileType (*getfiletype)(void);
44         
45         /* File and console handles */
46         gboolean (*readfile)(gpointer handle, gpointer buffer,
47                              guint32 numbytes, guint32 *bytesread,
48                              WapiOverlapped *overlapped);
49         gboolean (*writefile)(gpointer handle, gconstpointer buffer,
50                               guint32 numbytes, guint32 *byteswritten,
51                               WapiOverlapped *overlapped);
52         gboolean (*flushfile)(gpointer handle);
53         
54         /* File handles */
55         guint32 (*seek)(gpointer handle, gint32 movedistance,
56                         gint32 *highmovedistance, WapiSeekMethod method);
57         gboolean (*setendoffile)(gpointer handle);
58         guint32 (*getfilesize)(gpointer handle, guint32 *highsize);
59         gboolean (*getfiletime)(gpointer handle, WapiFileTime *create_time,
60                                 WapiFileTime *last_access,
61                                 WapiFileTime *last_write);
62         gboolean (*setfiletime)(gpointer handle,
63                                 const WapiFileTime *create_time,
64                                 const WapiFileTime *last_access,
65                                 const WapiFileTime *last_write);
66         
67         /* SignalObjectAndWait */
68         void (*signal)(gpointer signal);
69
70         /* Called by WaitForSingleObject and WaitForMultipleObjects,
71          * with the handle locked
72          */
73         void (*own_handle)(gpointer handle);
74
75         /* Called by WaitForSingleObject and WaitForMultipleObjects, if the
76          * handle in question is "ownable" (ie mutexes), to see if the current
77          * thread already owns this handle
78          */
79         gboolean (*is_owned)(gpointer handle);
80 };
81
82 #include <mono/io-layer/event-private.h>
83 #include <mono/io-layer/io-private.h>
84 #include <mono/io-layer/mutex-private.h>
85 #include <mono/io-layer/semaphore-private.h>
86 #include <mono/io-layer/socket-private.h>
87 #include <mono/io-layer/thread-private.h>
88 #include <mono/io-layer/process-private.h>
89
90 struct _WapiHandleShared
91 {
92         WapiHandleType type;
93         guint ref;
94         gboolean signalled;
95         mono_mutex_t signal_mutex;
96         pthread_cond_t signal_cond;
97         
98         union 
99         {
100                 struct _WapiHandle_event event;
101                 struct _WapiHandle_file file;
102                 struct _WapiHandle_find find;
103                 struct _WapiHandle_mutex mutex;
104                 struct _WapiHandle_sem sem;
105                 struct _WapiHandle_socket sock;
106                 struct _WapiHandle_thread thread;
107                 struct _WapiHandle_process process;
108         } u;
109 };
110
111 #define _WAPI_MAX_HANDLES 4096
112 #define _WAPI_HANDLE_INVALID (gpointer)-1
113
114 /*
115  * This is the layout of the shared memory segment
116  */
117 struct _WapiHandleShared_list
118 {
119 #ifdef _POSIX_THREAD_PROCESS_SHARED
120         mono_mutex_t signal_mutex;
121         pthread_cond_t signal_cond;
122 #endif
123         guint32 lock;
124         struct _WapiHandleShared handles[_WAPI_MAX_HANDLES];
125         guchar scratch_base[0];
126 };
127
128 struct _WapiHandlePrivate
129 {
130         union 
131         {
132                 struct _WapiHandlePrivate_event event;
133                 struct _WapiHandlePrivate_file file;
134                 struct _WapiHandlePrivate_find find;
135                 struct _WapiHandlePrivate_mutex mutex;
136                 struct _WapiHandlePrivate_sem sem;
137                 struct _WapiHandlePrivate_socket sock;
138                 struct _WapiHandlePrivate_thread thread;
139                 struct _WapiHandlePrivate_process process;
140         } u;
141 };
142
143 /* Per-process handle info. For lookup convenience, each index matches
144  * the corresponding shared data.
145  */
146 struct _WapiHandlePrivate_list
147 {
148 #ifndef _POSIX_THREAD_PROCESS_SHARED
149         mono_mutex_t signal_mutex;
150         pthread_cond_t signal_cond;
151 #endif
152         struct _WapiHandlePrivate handles[_WAPI_MAX_HANDLES];
153 };
154
155
156 #endif /* _WAPI_PRIVATE_H_ */