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