RTS tidyup sweep, first phase
[ghc-hetmet.git] / rts / win32 / IOManager.h
1 /* IOManager.h
2  *
3  * Non-blocking / asynchronous I/O for Win32.
4  *
5  * (c) sof, 2002-2003
6  */
7
8 #ifndef WIN32_IOMANAGER_H
9 #define WIN32_IOMANAGER_H
10
11 #include <windows.h>
12
13 /*
14  The IOManager subsystem provides a non-blocking view
15  of I/O operations. It lets one (or more) OS thread(s)
16  issue multiple I/O requests, which the IOManager then 
17  handles independently of/concurrent to the thread(s)
18  that issued the request. Upon completion, the issuing
19  thread can inspect the result of the I/O operation &
20  take appropriate action.
21
22  The IOManager is intended used with the GHC RTS to
23  implement non-blocking I/O in Concurrent Haskell.
24  */
25
26 /*
27  * Our WorkQueue holds WorkItems, encoding IO and
28  * delay requests.
29  *
30  */
31 typedef void (*CompletionProc)(unsigned int requestID,
32                                int   fd,
33                                int   len,
34                                void* buf,
35                                int   errCode);
36
37 /* 
38  * Asynchronous procedure calls executed by a worker thread
39  * take a generic state argument pointer and return an int by 
40  * default. 
41  */
42 typedef int (*DoProcProc)(void *param);
43
44 typedef union workData {
45     struct {
46         int   fd;
47         int   len;
48         char *buf; 
49     } ioData;
50     struct { 
51         int   msecs;
52     } delayData;
53     struct { 
54         DoProcProc proc;
55         void* param;
56     } procData;
57 } WorkData;
58
59 typedef struct WorkItem {
60   unsigned int     workKind;
61   WorkData         workData;
62   unsigned int     requestID;
63   CompletionProc   onCompletion;
64   unsigned int     abandonOp;
65   struct WorkItem  *link;
66 } WorkItem;
67
68 extern CompletionProc onComplete;
69
70 /* the kind of operations supported; you could easily imagine
71  * that instead of passing a tag describing the work to be performed,
72  * a function pointer is passed instead. Maybe later.
73  */
74 #define WORKER_READ        1
75 #define WORKER_WRITE       2
76 #define WORKER_DELAY       4
77 #define WORKER_FOR_SOCKET  8
78 #define WORKER_DO_PROC    16
79
80 /*
81  * Starting up and shutting down. 
82  */ 
83 extern BOOL StartIOManager     ( void );
84 extern void ShutdownIOManager  ( rtsBool wait_threads );
85
86 /*
87  * Adding I/O and delay requests. With each request a
88  * completion routine is supplied, which the worker thread
89  * will invoke upon completion.
90  */
91 extern int AddDelayRequest ( unsigned int   msecs,
92                              CompletionProc onCompletion);
93
94 extern int AddIORequest ( int            fd,
95                           BOOL           forWriting,
96                           BOOL           isSocket,
97                           int            len,
98                           char*          buffer,
99                           CompletionProc onCompletion);
100
101 extern int AddProcRequest ( void*          proc,
102                             void*          data,
103                             CompletionProc onCompletion);
104
105 extern void abandonWorkRequest ( int reqID );
106
107 #endif /* WIN32_IOMANAGER_H */